12#include "../Formatter.hpp"
29CLI11_INLINE std::string indent_block(
const std::string &input,
const std::string &indent) {
30 std::stringstream out;
31 bool line_start =
true;
33 for(
char ch : input) {
34 if(line_start && ch !=
'\n') {
38 line_start = (ch ==
'\n');
47 (ratio >= 0.0f) ? ((ratio <= 1.0f) ? ratio : 1.0f / ratio) : ((ratio < -1.0f) ? 1.0f / (-ratio) : -ratio);
58 return lambda_(app, name, mode);
61CLI11_INLINE std::string
63 std::stringstream out;
65 out <<
"\n" << group <<
":\n";
66 for(
const Option *opt : opts) {
74 std::vector<const Option *> opts =
84 std::stringstream out;
85 std::vector<std::string> groups = app->
get_groups();
88 for(
const std::string &group : groups) {
89 std::vector<const Option *> opts = app->
get_options([app, mode, &group](
const Option *opt) {
92 && (mode != AppFormatMode::Sub
96 if(!group.empty() && !opts.empty()) {
114 desc +=
" " +
get_label(
"REQUIRED") +
" ";
117 if(min_options > 0) {
118 if(max_options == min_options) {
119 desc +=
" \n[Exactly " + std::to_string(min_options) +
" of the following options are required]";
120 }
else if(max_options > 0) {
121 desc +=
" \n[Between " + std::to_string(min_options) +
" and " + std::to_string(max_options) +
122 " of the following options are required]";
124 desc +=
" \n[At least " + std::to_string(min_options) +
" of the following options are required]";
126 }
else if(max_options > 0) {
127 desc +=
" \n[At most " + std::to_string(max_options) +
" of the following options are allowed]";
130 return (!desc.empty()) ? desc +
"\n\n" : std::string{};
136 return usage +
"\n\n";
139 std::stringstream out;
147 std::vector<std::string> groups = app->
get_groups();
150 std::vector<const Option *> non_pos_options =
152 if(!non_pos_options.empty())
153 out <<
" [" <<
get_label(
"OPTIONS") <<
"]";
156 std::vector<const Option *> positionals =
160 if(!positionals.empty()) {
162 std::vector<std::string> positional_names;
163 positional_names.reserve(positionals.size());
164 for(
const auto *opt : positionals) {
168 out <<
" " << detail::join(positional_names,
" ");
173 [](
const CLI::App *subc) { return ((!subc->get_disabled()) && (!subc->get_name().empty())); })
188 return std::string{};
190 return '\n' + footer +
'\n';
196 if(mode == AppFormatMode::Sub)
199 std::stringstream out;
206 detail::streamOutAsParagraph(
220 out << footer_string;
227 std::stringstream out;
232 std::vector<std::string> subcmd_groups_seen;
233 for(
const App *com : subcommands) {
234 if(com->get_name().empty()) {
235 if(!com->get_group().empty() && com->get_group().front() !=
'+') {
242 std::string group_key = com->get_group();
243 std::string group_key_lower = detail::to_lower(group_key);
244 if(!group_key.empty() &&
245 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key_lower](
const std::string &a) {
246 return detail::to_lower(a) == group_key_lower;
247 }) == subcmd_groups_seen.end())
248 subcmd_groups_seen.push_back(group_key);
252 for(
const std::string &group : subcmd_groups_seen) {
253 out <<
'\n' << group <<
":\n";
255 [&group](
const App *sub_app) {
return detail::to_lower(sub_app->
get_group()) == detail::to_lower(group); });
256 for(
const App *new_com : subcommands_group) {
257 if(new_com->get_name().empty())
259 if(mode != AppFormatMode::All) {
262 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
272 std::stringstream out;
275 out << std::setw(static_cast<int>(
column_width_)) << std::left << name;
279 bool skipFirstLinePrefix =
true;
282 skipFirstLinePrefix =
false;
284 detail::streamOutAsParagraph(
292 std::stringstream out;
293 const bool is_option_group = sub->
get_name().empty();
294 const std::string body_indent = is_option_group ?
" " :
"";
299 detail::streamOutAsParagraph(
310 out << detail::indent_block(
make_groups(sub, mode), body_indent);
314 if(mode == AppFormatMode::Sub && !footer_string.empty()) {
317 if(footer_string == parent_footer) {
321 if(!footer_string.empty()) {
325 out << footer_string;
332 std::stringstream out;
336 out << std::setw(static_cast<int>(
column_width_)) << std::left << left;
339 bool skipFirstLinePrefix =
true;
342 skipFirstLinePrefix =
false;
344 detail::streamOutAsParagraph(
353 const auto names = detail::split(namesCombined,
',');
354 std::vector<std::string> vshortNames;
355 std::vector<std::string> vlongNames;
356 std::for_each(names.begin(), names.end(), [&vshortNames, &vlongNames](
const std::string &name) {
357 if(name.find(
"--", 0) != std::string::npos)
358 vlongNames.push_back(name);
360 vshortNames.push_back(name);
364 std::string shortNames = detail::join(vshortNames,
", ");
365 std::string longNames = detail::join(vlongNames,
", ");
369 const auto shortNamesColumnWidth =
371 const auto longNamesColumnWidth =
static_cast<int>(
column_width_) - shortNamesColumnWidth;
372 int shortNamesOverSize = 0;
375 if(!shortNames.empty()) {
376 shortNames =
" " + shortNames;
377 if(longNames.empty() && !opts.empty())
379 if(!longNames.empty())
381 if(
static_cast<int>(shortNames.length()) >= shortNamesColumnWidth) {
383 shortNamesOverSize =
static_cast<int>(shortNames.length()) - shortNamesColumnWidth;
385 out << std::setw(shortNamesColumnWidth) << std::left << shortNames;
387 out << std::setw(shortNamesColumnWidth) << std::left <<
"";
392 (std::min)(shortNamesOverSize, longNamesColumnWidth);
393 const auto adjustedLongNamesColumnWidth = longNamesColumnWidth - shortNamesOverSize;
396 if(!longNames.empty()) {
399 if(
static_cast<int>(longNames.length()) >= adjustedLongNamesColumnWidth)
402 out << std::setw(adjustedLongNamesColumnWidth) << std::left << longNames;
404 out << std::setw(adjustedLongNamesColumnWidth) << std::left <<
"";
408 bool skipFirstLinePrefix =
true;
411 skipFirstLinePrefix =
false;
413 detail::streamOutAsParagraph(
426 return opt->
get_name(
false,
true, !enable_default_flag_values_);
430 std::stringstream out;
432 const auto print_option_set = [&out](
const std::set<Option *> &options) {
433 std::vector<const Option *> sorted(options.begin(), options.end());
434 std::sort(sorted.begin(), sorted.end(), [](
const Option *lhs,
const Option *rhs) {
435 return lhs->get_name() < rhs->get_name();
437 for(
const Option *op : sorted)
438 out <<
" " << op->get_name();
441 if(!opt->get_option_text().empty()) {
442 out <<
" " << opt->get_option_text();
445 if(enable_option_type_names_) {
468 out <<
" " <<
get_label(
"Excludes") <<
":";
479 std::stringstream out;
486 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_footer() const
Generate and return the footer.
Definition App_inl.hpp:946
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition App.hpp:1098
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition App_inl.hpp:942
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1152
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const).
Definition App.hpp:1158
App * get_parent()
Get the parent of this subcommand (or nullptr if main app).
Definition App.hpp:1173
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:1121
CLI11_NODISCARD std::size_t get_require_option_max() const
Get the required max option value.
Definition App.hpp:1104
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition App.hpp:1119
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order).
Definition App_inl.hpp:1180
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition App.hpp:1182
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition App.hpp:1101
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition App.hpp:1086
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:932
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:982
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1032
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition App.hpp:1095
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1179
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:139
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:136
Definition Option.hpp:261
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:611
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:567
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:599
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:294
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing).
Definition Option.hpp:576
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:614
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:573
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:570
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:620
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:592
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:595
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:597
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:556