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") <<
"]";
139 if(!positionals.empty()) {
141 std::vector<std::string> positional_names;
142 positional_names.reserve(positionals.size());
143 for(
const auto *opt : positionals) {
147 out <<
" " << detail::join(positional_names,
" ");
152 [](
const CLI::App *subc) { return ((!subc->get_disabled()) && (!subc->get_name().empty())); })
167 return std::string{};
169 return '\n' + footer +
'\n';
175 if(mode == AppFormatMode::Sub)
178 std::stringstream out;
185 detail::streamOutAsParagraph(
199 out << footer_string;
206 std::stringstream out;
211 std::vector<std::string> subcmd_groups_seen;
212 for(
const App *com : subcommands) {
213 if(com->get_name().empty()) {
214 if(!com->get_group().empty() && com->get_group().front() !=
'+') {
221 std::string group_key = com->get_group();
222 if(!group_key.empty() &&
223 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key](std::string a) {
224 return detail::to_lower(a) == detail::to_lower(group_key);
225 }) == subcmd_groups_seen.end())
226 subcmd_groups_seen.push_back(group_key);
230 for(
const std::string &group : subcmd_groups_seen) {
231 out <<
'\n' << group <<
":\n";
233 [&group](
const App *sub_app) {
return detail::to_lower(sub_app->
get_group()) == detail::to_lower(group); });
234 for(
const App *new_com : subcommands_group) {
235 if(new_com->get_name().empty())
237 if(mode != AppFormatMode::All) {
240 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
250 std::stringstream out;
253 out << std::setw(static_cast<int>(
column_width_)) << std::left << name;
254 detail::streamOutAsParagraph(
261 std::stringstream out;
262 const bool is_option_group = sub->
get_name().empty();
263 const std::string body_indent = is_option_group ?
" " :
"";
268 detail::streamOutAsParagraph(
279 out << detail::indent_block(
make_groups(sub, mode), body_indent);
283 if(mode == AppFormatMode::Sub && !footer_string.empty()) {
286 if(footer_string == parent_footer) {
290 if(!footer_string.empty()) {
294 out << footer_string;
301 std::stringstream out;
305 out << std::setw(static_cast<int>(
column_width_)) << std::left << left;
308 bool skipFirstLinePrefix =
true;
311 skipFirstLinePrefix =
false;
313 detail::streamOutAsParagraph(
322 const auto names = detail::split(namesCombined,
',');
323 std::vector<std::string> vshortNames;
324 std::vector<std::string> vlongNames;
325 std::for_each(names.begin(), names.end(), [&vshortNames, &vlongNames](
const std::string &name) {
326 if(name.find(
"--", 0) != std::string::npos)
327 vlongNames.push_back(name);
329 vshortNames.push_back(name);
333 std::string shortNames = detail::join(vshortNames,
", ");
334 std::string longNames = detail::join(vlongNames,
", ");
338 const auto shortNamesColumnWidth =
340 const auto longNamesColumnWidth =
static_cast<int>(
column_width_) - shortNamesColumnWidth;
341 int shortNamesOverSize = 0;
344 if(!shortNames.empty()) {
345 shortNames =
" " + shortNames;
346 if(longNames.empty() && !opts.empty())
348 if(!longNames.empty())
350 if(
static_cast<int>(shortNames.length()) >= shortNamesColumnWidth) {
352 shortNamesOverSize =
static_cast<int>(shortNames.length()) - shortNamesColumnWidth;
354 out << std::setw(shortNamesColumnWidth) << std::left << shortNames;
356 out << std::setw(shortNamesColumnWidth) << std::left <<
"";
361 (std::min)(shortNamesOverSize, longNamesColumnWidth);
362 const auto adjustedLongNamesColumnWidth = longNamesColumnWidth - shortNamesOverSize;
365 if(!longNames.empty()) {
368 if(
static_cast<int>(longNames.length()) >= adjustedLongNamesColumnWidth)
371 out << std::setw(adjustedLongNamesColumnWidth) << std::left << longNames;
373 out << std::setw(adjustedLongNamesColumnWidth) << std::left <<
"";
377 bool skipFirstLinePrefix =
true;
380 skipFirstLinePrefix =
false;
382 detail::streamOutAsParagraph(
395 return opt->
get_name(
false,
true, !enable_default_flag_values_);
399 std::stringstream out;
401 const auto print_option_set = [&out](
const std::set<Option *> &options) {
402 std::vector<const Option *> sorted(options.begin(), options.end());
403 std::sort(sorted.begin(), sorted.end(), [](
const Option *lhs,
const Option *rhs) {
404 return lhs->get_name() < rhs->get_name();
406 for(
const Option *op : sorted)
407 out <<
" " << op->get_name();
410 if(!opt->get_option_text().empty()) {
411 out <<
" " << opt->get_option_text();
414 if(enable_option_type_names_) {
437 out <<
" " <<
get_label(
"Excludes") <<
":";
448 std::stringstream out;
455 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:631
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:576
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:585
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:634
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:582
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:579
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:640
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:612
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:615
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:617
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:565