12#include "../Formatter.hpp"
25CLI11_INLINE std::string indent_block(
const std::string &input,
const std::string &indent) {
26 std::stringstream out;
27 bool line_start =
true;
29 for(
char ch : input) {
30 if(line_start && ch !=
'\n') {
34 line_start = (ch ==
'\n');
41CLI11_INLINE std::string
43 std::stringstream out;
45 out <<
"\n" << group <<
":\n";
46 for(
const Option *opt : opts) {
54 std::vector<const Option *> opts =
64 std::stringstream out;
65 std::vector<std::string> groups = app->
get_groups();
68 for(
const std::string &group : groups) {
69 std::vector<const Option *> opts = app->
get_options([app, mode, &group](
const Option *opt) {
72 && (mode != AppFormatMode::Sub
76 if(!group.empty() && !opts.empty()) {
94 desc +=
" " +
get_label(
"REQUIRED") +
" ";
98 if(max_options == min_options) {
99 desc +=
" \n[Exactly " + std::to_string(min_options) +
" of the following options are required]";
100 }
else if(max_options > 0) {
101 desc +=
" \n[Between " + std::to_string(min_options) +
" and " + std::to_string(max_options) +
102 " of the following options are required]";
104 desc +=
" \n[At least " + std::to_string(min_options) +
" of the following options are required]";
106 }
else if(max_options > 0) {
107 desc +=
" \n[At most " + std::to_string(max_options) +
" of the following options are allowed]";
110 return (!desc.empty()) ? desc +
"\n\n" : std::string{};
116 return usage +
"\n\n";
119 std::stringstream out;
127 std::vector<std::string> groups = app->
get_groups();
130 std::vector<const Option *> non_pos_options =
132 if(!non_pos_options.empty())
133 out <<
" [" <<
get_label(
"OPTIONS") <<
"]";
136 std::vector<const Option *> positionals =
140 if(!positionals.empty()) {
142 std::vector<std::string> positional_names;
143 positional_names.reserve(positionals.size());
144 for(
const auto *opt : positionals) {
148 out <<
" " << detail::join(positional_names,
" ");
153 [](
const CLI::App *subc) { return ((!subc->get_disabled()) && (!subc->get_name().empty())); })
168 return std::string{};
170 return '\n' + footer +
'\n';
176 if(mode == AppFormatMode::Sub)
179 std::stringstream out;
186 detail::streamOutAsParagraph(
200 out << footer_string;
207 std::stringstream out;
212 std::vector<std::string> subcmd_groups_seen;
213 for(
const App *com : subcommands) {
214 if(com->get_name().empty()) {
215 if(!com->get_group().empty() && com->get_group().front() !=
'+') {
222 std::string group_key = com->get_group();
223 std::string group_key_lower = detail::to_lower(group_key);
224 if(!group_key.empty() &&
225 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key_lower](
const std::string &a) {
226 return detail::to_lower(a) == group_key_lower;
227 }) == subcmd_groups_seen.end())
228 subcmd_groups_seen.push_back(group_key);
232 for(
const std::string &group : subcmd_groups_seen) {
233 out <<
'\n' << group <<
":\n";
235 [&group](
const App *sub_app) {
return detail::to_lower(sub_app->
get_group()) == detail::to_lower(group); });
236 for(
const App *new_com : subcommands_group) {
237 if(new_com->get_name().empty())
239 if(mode != AppFormatMode::All) {
242 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
252 std::stringstream out;
255 out << std::setw(static_cast<int>(
column_width_)) << std::left << name;
259 bool skipFirstLinePrefix =
true;
262 skipFirstLinePrefix =
false;
264 detail::streamOutAsParagraph(
272 std::stringstream out;
273 const bool is_option_group = sub->
get_name().empty();
274 const std::string body_indent = is_option_group ?
" " :
"";
279 detail::streamOutAsParagraph(
290 out << detail::indent_block(
make_groups(sub, mode), body_indent);
294 if(mode == AppFormatMode::Sub && !footer_string.empty()) {
297 if(footer_string == parent_footer) {
301 if(!footer_string.empty()) {
305 out << footer_string;
312 std::stringstream out;
316 out << std::setw(static_cast<int>(
column_width_)) << std::left << left;
319 bool skipFirstLinePrefix =
true;
322 skipFirstLinePrefix =
false;
324 detail::streamOutAsParagraph(
333 const auto names = detail::split(namesCombined,
',');
334 std::vector<std::string> vshortNames;
335 std::vector<std::string> vlongNames;
336 std::for_each(names.begin(), names.end(), [&vshortNames, &vlongNames](
const std::string &name) {
337 if(name.find(
"--", 0) != std::string::npos)
338 vlongNames.push_back(name);
340 vshortNames.push_back(name);
344 std::string shortNames = detail::join(vshortNames,
", ");
345 std::string longNames = detail::join(vlongNames,
", ");
349 const auto shortNamesColumnWidth =
351 const auto longNamesColumnWidth =
static_cast<int>(
column_width_) - shortNamesColumnWidth;
352 int shortNamesOverSize = 0;
355 if(!shortNames.empty()) {
356 shortNames =
" " + shortNames;
357 if(longNames.empty() && !opts.empty())
359 if(!longNames.empty())
361 if(
static_cast<int>(shortNames.length()) >= shortNamesColumnWidth) {
363 shortNamesOverSize =
static_cast<int>(shortNames.length()) - shortNamesColumnWidth;
365 out << std::setw(shortNamesColumnWidth) << std::left << shortNames;
367 out << std::setw(shortNamesColumnWidth) << std::left <<
"";
372 (std::min)(shortNamesOverSize, longNamesColumnWidth);
373 const auto adjustedLongNamesColumnWidth = longNamesColumnWidth - shortNamesOverSize;
376 if(!longNames.empty()) {
379 if(
static_cast<int>(longNames.length()) >= adjustedLongNamesColumnWidth)
382 out << std::setw(adjustedLongNamesColumnWidth) << std::left << longNames;
384 out << std::setw(adjustedLongNamesColumnWidth) << std::left <<
"";
388 bool skipFirstLinePrefix =
true;
391 skipFirstLinePrefix =
false;
393 detail::streamOutAsParagraph(
406 return opt->
get_name(
false,
true, !enable_default_flag_values_);
410 std::stringstream out;
412 const auto print_option_set = [&out](
const std::set<Option *> &options) {
413 std::vector<const Option *> sorted(options.begin(), options.end());
414 std::sort(sorted.begin(), sorted.end(), [](
const Option *lhs,
const Option *rhs) {
415 return lhs->get_name() < rhs->get_name();
417 for(
const Option *op : sorted)
418 out <<
" " << op->get_name();
421 if(!opt->get_option_text().empty()) {
422 out <<
" " << opt->get_option_text();
425 if(enable_option_type_names_) {
448 out <<
" " <<
get_label(
"Excludes") <<
":";
459 std::stringstream out;
466 return opt->
get_required() ? out.str() :
"[" + out.str() +
"]";
Creates a command line program, with very few defaults.
Definition App.hpp:114
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition App.hpp:1178
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition App.hpp:1191
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1245
CLI11_NODISCARD std::string get_footer() const
Generate and return the footer.
Definition App.hpp:1183
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const)
Definition App.hpp:1251
App * get_parent()
Get the parent of this subcommand (or nullptr if main app)
Definition App.hpp:1266
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:960
CLI11_NODISCARD std::size_t get_require_option_max() const
Get the required max option value.
Definition App.hpp:1197
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition App.hpp:1212
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition App_inl.hpp:1019
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition App.hpp:1275
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition App.hpp:1194
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition App.hpp:1175
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:976
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:822
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1121
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition App.hpp:1188
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1272
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:632
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:577
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:549
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:269
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing)
Definition Option.hpp:586
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:635
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:583
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:580
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:641
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:613
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:616
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:618
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:566