12#include "../Formatter.hpp"
24CLI11_INLINE std::string
26 std::stringstream out;
28 out <<
"\n" << group <<
":\n";
29 for(
const Option *opt : opts) {
37 std::vector<const Option *> opts =
47 std::stringstream out;
48 std::vector<std::string> groups = app->
get_groups();
51 for(
const std::string &group : groups) {
52 std::vector<const Option *> opts = app->
get_options([app, mode, &group](
const Option *opt) {
55 && (mode != AppFormatMode::Sub
59 if(!group.empty() && !opts.empty()) {
77 desc +=
" " +
get_label(
"REQUIRED") +
" ";
81 if(max_options == min_options) {
82 desc +=
" \n[Exactly " + std::to_string(min_options) +
" of the following options are required]";
83 }
else if(max_options > 0) {
84 desc +=
" \n[Between " + std::to_string(min_options) +
" and " + std::to_string(max_options) +
85 " of the following options are required]";
87 desc +=
" \n[At least " + std::to_string(min_options) +
" of the following options are required]";
89 }
else if(max_options > 0) {
90 desc +=
" \n[At most " + std::to_string(max_options) +
" of the following options are allowed]";
93 return (!desc.empty()) ? desc +
"\n\n" : std::string{};
99 return usage +
"\n\n";
102 std::stringstream out;
110 std::vector<std::string> groups = app->
get_groups();
113 std::vector<const Option *> non_pos_options =
115 if(!non_pos_options.empty())
116 out <<
" [" <<
get_label(
"OPTIONS") <<
"]";
122 if(!positionals.empty()) {
124 std::vector<std::string> positional_names(positionals.size());
125 std::transform(positionals.begin(), positionals.end(), positional_names.begin(), [
this](
const Option *opt) {
126 return make_option_usage(opt);
129 out <<
" " << detail::join(positional_names,
" ");
134 [](
const CLI::App *subc) { return ((!subc->get_disabled()) && (!subc->get_name().empty())); })
149 return std::string{};
151 return '\n' + footer +
'\n';
157 if(mode == AppFormatMode::Sub)
160 std::stringstream out;
167 detail::streamOutAsParagraph(
181 out << footer_string;
188 std::stringstream out;
193 std::vector<std::string> subcmd_groups_seen;
194 for(
const App *com : subcommands) {
195 if(com->get_name().empty()) {
196 if(!com->get_group().empty() && com->get_group().front() !=
'+') {
201 std::string group_key = com->get_group();
202 if(!group_key.empty() &&
203 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key](std::string a) {
204 return detail::to_lower(a) == detail::to_lower(group_key);
205 }) == subcmd_groups_seen.end())
206 subcmd_groups_seen.push_back(group_key);
210 for(
const std::string &group : subcmd_groups_seen) {
211 out <<
'\n' << group <<
":\n";
213 [&group](
const App *sub_app) {
return detail::to_lower(sub_app->
get_group()) == detail::to_lower(group); });
214 for(
const App *new_com : subcommands_group) {
215 if(new_com->get_name().empty())
217 if(mode != AppFormatMode::All) {
220 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
230 std::stringstream out;
233 out << std::setw(static_cast<int>(
column_width_)) << std::left << name;
234 detail::streamOutAsParagraph(
241 std::stringstream out;
245 detail::streamOutAsParagraph(
260 if(mode == AppFormatMode::Sub && !footer_string.empty()) {
263 if(footer_string == parent_footer) {
267 if(!footer_string.empty()) {
271 out << footer_string;
278 std::stringstream out;
282 out << std::setw(static_cast<int>(
column_width_)) << std::left << left;
285 bool skipFirstLinePrefix =
true;
288 skipFirstLinePrefix =
false;
290 detail::streamOutAsParagraph(
299 const auto names = detail::split(namesCombined,
',');
300 std::vector<std::string> vshortNames;
301 std::vector<std::string> vlongNames;
302 std::for_each(names.begin(), names.end(), [&vshortNames, &vlongNames](
const std::string &name) {
303 if(name.find(
"--", 0) != std::string::npos)
304 vlongNames.push_back(name);
306 vshortNames.push_back(name);
310 std::string shortNames = detail::join(vshortNames,
", ");
311 std::string longNames = detail::join(vlongNames,
", ");
315 const auto shortNamesColumnWidth =
317 const auto longNamesColumnWidth =
static_cast<int>(
column_width_) - shortNamesColumnWidth;
318 int shortNamesOverSize = 0;
321 if(!shortNames.empty()) {
322 shortNames =
" " + shortNames;
323 if(longNames.empty() && !opts.empty())
325 if(!longNames.empty())
327 if(
static_cast<int>(shortNames.length()) >= shortNamesColumnWidth) {
329 shortNamesOverSize =
static_cast<int>(shortNames.length()) - shortNamesColumnWidth;
331 out << std::setw(shortNamesColumnWidth) << std::left << shortNames;
333 out << std::setw(shortNamesColumnWidth) << std::left <<
"";
338 (std::min)(shortNamesOverSize, longNamesColumnWidth);
339 const auto adjustedLongNamesColumnWidth = longNamesColumnWidth - shortNamesOverSize;
342 if(!longNames.empty()) {
345 if(
static_cast<int>(longNames.length()) >= adjustedLongNamesColumnWidth)
348 out << std::setw(adjustedLongNamesColumnWidth) << std::left << longNames;
350 out << std::setw(adjustedLongNamesColumnWidth) << std::left <<
"";
354 bool skipFirstLinePrefix =
true;
357 skipFirstLinePrefix =
false;
359 detail::streamOutAsParagraph(
372 return opt->
get_name(
false,
true, !enable_default_flag_values_);
376 std::stringstream out;
378 const auto print_option_set = [&out](
const std::set<Option *> &options) {
379 std::vector<const Option *> sorted(options.begin(), options.end());
380 std::sort(sorted.begin(), sorted.end(), [](
const Option *lhs,
const Option *rhs) {
381 return lhs->get_name() < rhs->get_name();
383 for(
const Option *op : sorted)
384 out <<
" " << op->get_name();
387 if(!opt->get_option_text().empty()) {
388 out <<
" " << opt->get_option_text();
391 if(enable_option_type_names_) {
414 out <<
" " <<
get_label(
"Excludes") <<
":";
425 std::stringstream out;
432 return opt->
get_required() ? out.str() :
"[" + out.str() +
"]";
Creates a command line program, with very few defaults.
Definition App.hpp:115
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition App.hpp:1179
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition App.hpp:1192
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1246
CLI11_NODISCARD std::string get_footer() const
Generate and return the footer.
Definition App.hpp:1184
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const)
Definition App.hpp:1252
App * get_parent()
Get the parent of this subcommand (or nullptr if main app)
Definition App.hpp:1267
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:970
CLI11_NODISCARD std::size_t get_require_option_max() const
Get the required max option value.
Definition App.hpp:1198
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition App.hpp:1213
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition App_inl.hpp:1029
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition App.hpp:1276
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition App.hpp:1195
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition App.hpp:1176
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:977
std::vector< const Option * > get_options(const std::function< bool(const Option *)> filter={}) const
Get the list of options (user facing function, so returns raw pointers), has optional filter function...
Definition App_inl.hpp:832
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1122
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition App.hpp:1189
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1273
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:137
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:134
Definition Option.hpp:259
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:628
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:573
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:538
CLI11_NODISCARD std::string get_name(bool positional=false, bool all_options=false, bool disable_default_flag_values=false) const
Gets a comma separated list of names. Will include / prefer the positional name if positional is true...
Definition Option_inl.hpp:258
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing)
Definition Option.hpp:582
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:631
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:579
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:576
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:637
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:609
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:612
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:614
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:562