12#include "../Formatter.hpp"
23CLI11_INLINE std::string
25 std::stringstream out;
27 out <<
"\n" << group <<
":\n";
28 for(
const Option *opt : opts) {
36 std::vector<const Option *> opts =
46 std::stringstream out;
47 std::vector<std::string> groups = app->
get_groups();
50 for(
const std::string &group : groups) {
51 std::vector<const Option *> opts = app->
get_options([app, mode, &group](
const Option *opt) {
54 && (mode != AppFormatMode::Sub
58 if(!group.empty() && !opts.empty()) {
76 desc +=
" " +
get_label(
"REQUIRED") +
" ";
80 if(max_options == min_options) {
81 desc +=
" \n[Exactly " + std::to_string(min_options) +
" of the following options are required]";
82 }
else if(max_options > 0) {
83 desc +=
" \n[Between " + std::to_string(min_options) +
" and " + std::to_string(max_options) +
84 " of the following options are required]";
86 desc +=
" \n[At least " + std::to_string(min_options) +
" of the following options are required]";
88 }
else if(max_options > 0) {
89 desc +=
" \n[At most " + std::to_string(max_options) +
" of the following options are allowed]";
92 return (!desc.empty()) ? desc +
"\n\n" : std::string{};
98 return usage +
"\n\n";
101 std::stringstream out;
109 std::vector<std::string> groups = app->
get_groups();
112 std::vector<const Option *> non_pos_options =
114 if(!non_pos_options.empty())
115 out <<
" [" <<
get_label(
"OPTIONS") <<
"]";
121 if(!positionals.empty()) {
123 std::vector<std::string> positional_names(positionals.size());
124 std::transform(positionals.begin(), positionals.end(), positional_names.begin(), [
this](
const Option *opt) {
125 return make_option_usage(opt);
128 out <<
" " << detail::join(positional_names,
" ");
133 [](
const CLI::App *subc) { return ((!subc->get_disabled()) && (!subc->get_name().empty())); })
149 return std::string{};
151 return '\n' + footer +
"\n\n";
157 if(mode == AppFormatMode::Sub)
160 std::stringstream out;
167 detail::streamOutAsParagraph(
186 std::stringstream out;
191 std::vector<std::string> subcmd_groups_seen;
192 for(
const App *com : subcommands) {
193 if(com->get_name().empty()) {
194 if(!com->get_group().empty() && com->get_group().front() !=
'+') {
199 std::string group_key = com->get_group();
200 if(!group_key.empty() &&
201 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key](std::string a) {
202 return detail::to_lower(a) == detail::to_lower(group_key);
203 }) == subcmd_groups_seen.end())
204 subcmd_groups_seen.push_back(group_key);
208 for(
const std::string &group : subcmd_groups_seen) {
209 out <<
'\n' << group <<
":\n";
211 [&group](
const App *sub_app) {
return detail::to_lower(sub_app->
get_group()) == detail::to_lower(group); });
212 for(
const App *new_com : subcommands_group) {
213 if(new_com->get_name().empty())
215 if(mode != AppFormatMode::All) {
218 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
228 std::stringstream out;
231 out << std::setw(static_cast<int>(
column_width_)) << std::left << name;
232 detail::streamOutAsParagraph(
239 std::stringstream out;
243 detail::streamOutAsParagraph(
266 std::stringstream out;
270 out << std::setw(static_cast<int>(
column_width_)) << std::left << left;
273 bool skipFirstLinePrefix =
true;
276 skipFirstLinePrefix =
false;
278 detail::streamOutAsParagraph(
287 const auto names = detail::split(namesCombined,
',');
288 std::vector<std::string> vshortNames;
289 std::vector<std::string> vlongNames;
290 std::for_each(names.begin(), names.end(), [&vshortNames, &vlongNames](
const std::string &name) {
291 if(name.find(
"--", 0) != std::string::npos)
292 vlongNames.push_back(name);
294 vshortNames.push_back(name);
298 std::string shortNames = detail::join(vshortNames,
", ");
299 std::string longNames = detail::join(vlongNames,
", ");
302 const auto shortNamesColumnWidth =
static_cast<int>(
column_width_ / 3);
303 const auto longNamesColumnWidth =
static_cast<int>(std::ceil(
305 int shortNamesOverSize = 0;
308 if(!shortNames.empty()) {
309 shortNames =
" " + shortNames;
310 if(longNames.empty() && !opts.empty())
312 if(!longNames.empty())
314 if(
static_cast<int>(shortNames.length()) >= shortNamesColumnWidth) {
316 shortNamesOverSize =
static_cast<int>(shortNames.length()) - shortNamesColumnWidth;
318 out << std::setw(shortNamesColumnWidth) << std::left << shortNames;
320 out << std::setw(shortNamesColumnWidth) << std::left <<
"";
325 (std::min)(shortNamesOverSize, longNamesColumnWidth);
326 const auto adjustedLongNamesColumnWidth = longNamesColumnWidth - shortNamesOverSize;
329 if(!longNames.empty()) {
332 if(
static_cast<int>(longNames.length()) >= adjustedLongNamesColumnWidth)
335 out << std::setw(adjustedLongNamesColumnWidth) << std::left << longNames;
337 out << std::setw(adjustedLongNamesColumnWidth) << std::left <<
"";
341 bool skipFirstLinePrefix =
true;
344 skipFirstLinePrefix =
false;
346 detail::streamOutAsParagraph(
363 std::stringstream out;
365 if(!opt->get_option_text().empty()) {
366 out <<
" " << opt->get_option_text();
386 out <<
" " << op->get_name();
389 out <<
" " <<
get_label(
"Excludes") <<
":";
391 out <<
" " << op->get_name();
401 std::stringstream out;
408 return opt->
get_required() ? out.str() :
"[" + out.str() +
"]";
Creates a command line program, with very few defaults.
Definition App.hpp:98
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition App.hpp:1141
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition App.hpp:1154
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1200
CLI11_NODISCARD std::string get_footer() const
Generate and return the footer.
Definition App.hpp:1146
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const)
Definition App.hpp:1206
App * get_parent()
Get the parent of this subcommand (or nullptr if main app)
Definition App.hpp:1221
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:889
CLI11_NODISCARD std::size_t get_require_option_max() const
Get the required max option value.
Definition App.hpp:1160
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition App.hpp:1169
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition App_inl.hpp:948
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition App.hpp:1230
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition App.hpp:1157
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition App.hpp:1138
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:938
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:811
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1072
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition App.hpp:1151
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1227
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:118
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:115
Definition Option.hpp:231
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:587
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:532
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:510
CLI11_NODISCARD std::string get_name(bool positional=false, bool all_options=false) const
Gets a comma separated list of names. Will include / prefer the positional name if positional is true...
Definition Option_inl.hpp:233
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing)
Definition Option.hpp:541
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:590
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:538
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:535
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:596
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:568
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:571
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:573
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:521