CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
Formatter_inl.hpp
1// Copyright (c) 2017-2026, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// This include is only needed for IDEs to discover symbols
12#include "../Formatter.hpp"
13
14// [CLI11:public_includes:set]
15#include <algorithm>
16#include <set>
17#include <string>
18#include <utility>
19#include <vector>
20// [CLI11:public_includes:end]
21
22namespace CLI {
23// [CLI11:formatter_inl_hpp:verbatim]
24namespace detail {
25CLI11_INLINE std::string indent_block(const std::string &input, const std::string &indent) {
26 std::stringstream out;
27 bool line_start = true;
28
29 for(char ch : input) {
30 if(line_start && ch != '\n') {
31 out << indent;
32 }
33 out << ch;
34 line_start = (ch == '\n');
35 }
36
37 return out.str();
38}
39} // namespace detail
40
41CLI11_INLINE std::string
42Formatter::make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const {
43 std::stringstream out;
44
45 out << "\n" << group << ":\n";
46 for(const Option *opt : opts) {
47 out << make_option(opt, is_positional);
48 }
49
50 return out.str();
51}
52
53CLI11_INLINE std::string Formatter::make_positionals(const App *app) const {
54 std::vector<const Option *> opts =
55 app->get_options([](const Option *opt) { return !opt->get_group().empty() && opt->get_positional(); });
56
57 if(opts.empty())
58 return {};
59
60 return make_group(get_label("POSITIONALS"), true, opts);
61}
62
63CLI11_INLINE std::string Formatter::make_groups(const App *app, AppFormatMode mode) const {
64 std::stringstream out;
65 std::vector<std::string> groups = app->get_groups();
66
67 // Options
68 for(const std::string &group : groups) {
69 std::vector<const Option *> opts = app->get_options([app, mode, &group](const Option *opt) {
70 return opt->get_group() == group // Must be in the right group
71 && opt->nonpositional() // Must not be a positional
72 && (mode != AppFormatMode::Sub // If mode is Sub, then
73 || (app->get_help_ptr() != opt // Ignore help pointer
74 && app->get_help_all_ptr() != opt)); // Ignore help all pointer
75 });
76 if(!group.empty() && !opts.empty()) {
77 out << make_group(group, false, opts);
78
79 // Removed double newline between groups for consistency of help text
80 // if(group != groups.back())
81 // out << "\n";
82 }
83 }
84
85 return out.str();
86}
87
88CLI11_INLINE std::string Formatter::make_description(const App *app) const {
89 std::string desc = app->get_description();
90 auto min_options = app->get_require_option_min();
91 auto max_options = app->get_require_option_max();
92
93 if(app->get_required()) {
94 desc += " " + get_label("REQUIRED") + " ";
95 }
96
97 if(min_options > 0) {
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]";
103 } else {
104 desc += " \n[At least " + std::to_string(min_options) + " of the following options are required]";
105 }
106 } else if(max_options > 0) {
107 desc += " \n[At most " + std::to_string(max_options) + " of the following options are allowed]";
108 }
109
110 return (!desc.empty()) ? desc + "\n\n" : std::string{};
111}
112
113CLI11_INLINE std::string Formatter::make_usage(const App *app, std::string name) const {
114 std::string usage = app->get_usage();
115 if(!usage.empty()) {
116 return usage + "\n\n";
117 }
118
119 std::stringstream out;
120 out << '\n';
121
122 if(name.empty())
123 out << get_label("Usage") << ':';
124 else
125 out << name;
126
127 std::vector<std::string> groups = app->get_groups();
128
129 // Print an Options badge if any options exist
130 std::vector<const Option *> non_pos_options =
131 app->get_options([](const Option *opt) { return opt->nonpositional(); });
132 if(!non_pos_options.empty())
133 out << " [" << get_label("OPTIONS") << "]";
134
135 // Positionals need to be listed here
136 std::vector<const Option *> positionals = app->get_options([](const Option *opt) { return opt->get_positional(); });
137
138 // Print out positionals if any are left
139 if(!positionals.empty()) {
140 // Convert to help names
141 std::vector<std::string> positional_names;
142 positional_names.reserve(positionals.size());
143 for(const auto *opt : positionals) {
144 positional_names.push_back(make_option_usage(opt));
145 }
146
147 out << " " << detail::join(positional_names, " ");
148 }
149
150 // Add a marker if subcommands are expected or optional
151 if(!app->get_subcommands(
152 [](const CLI::App *subc) { return ((!subc->get_disabled()) && (!subc->get_name().empty())); })
153 .empty()) {
154 out << ' ' << (app->get_require_subcommand_min() == 0 ? "[" : "")
155 << get_label(app->get_require_subcommand_max() == 1 ? "SUBCOMMAND" : "SUBCOMMANDS")
156 << (app->get_require_subcommand_min() == 0 ? "]" : "");
157 }
158
159 out << "\n\n";
160
161 return out.str();
162}
163
164CLI11_INLINE std::string Formatter::make_footer(const App *app) const {
165 std::string footer = app->get_footer();
166 if(footer.empty()) {
167 return std::string{};
168 }
169 return '\n' + footer + '\n';
170}
171
172CLI11_INLINE std::string Formatter::make_help(const App *app, std::string name, AppFormatMode mode) const {
173 // This immediately forwards to the make_expanded method. This is done this way so that subcommands can
174 // have overridden formatters
175 if(mode == AppFormatMode::Sub)
176 return make_expanded(app, mode);
177
178 std::stringstream out;
179 if((app->get_name().empty()) && (app->get_parent() != nullptr)) {
180 if(app->get_group() != "SUBCOMMANDS") {
181 out << app->get_group() << ':';
182 }
183 }
185 detail::streamOutAsParagraph(
186 out, make_description(app), description_paragraph_width_, ""); // Format description as paragraph
187 } else {
188 out << make_description(app) << '\n';
189 }
190 out << make_usage(app, name);
191 out << make_positionals(app);
192 out << make_groups(app, mode);
193 out << make_subcommands(app, mode);
194 std::string footer_string = make_footer(app);
195
197 detail::streamOutAsParagraph(out, footer_string, footer_paragraph_width_); // Format footer as paragraph
198 } else {
199 out << footer_string;
200 }
201
202 return out.str();
203}
204
205CLI11_INLINE std::string Formatter::make_subcommands(const App *app, AppFormatMode mode) const {
206 std::stringstream out;
207
208 std::vector<const App *> subcommands = app->get_subcommands({});
209
210 // Make a list in definition order of the groups seen
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() != '+') {
215 auto expanded = make_expanded(com, mode);
216 // add expansion in one place so each group has subgroups beneath it
217 out << expanded;
218 }
219 continue;
220 }
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);
227 }
228
229 // For each group, filter out and print subcommands
230 for(const std::string &group : subcmd_groups_seen) {
231 out << '\n' << group << ":\n";
232 std::vector<const App *> subcommands_group = app->get_subcommands(
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())
236 continue;
237 if(mode != AppFormatMode::All) {
238 out << make_subcommand(new_com);
239 } else {
240 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
241 out << '\n';
242 }
243 }
244 }
245
246 return out.str();
247}
248
249CLI11_INLINE std::string Formatter::make_subcommand(const App *sub) const {
250 std::stringstream out;
251 std::string name = " " + sub->get_display_name(true) + (sub->get_required() ? " " + get_label("REQUIRED") : "");
252
253 out << std::setw(static_cast<int>(column_width_)) << std::left << name;
254 detail::streamOutAsParagraph(
255 out, sub->get_description(), right_column_width_, std::string(column_width_, ' '), true);
256 out << '\n';
257 return out.str();
258}
259
260CLI11_INLINE std::string Formatter::make_expanded(const App *sub, AppFormatMode mode) const {
261 std::stringstream out;
262 const bool is_option_group = sub->get_name().empty();
263 const std::string body_indent = is_option_group ? " " : "";
264
265 out << sub->get_display_name(true) << '\n';
266
268 detail::streamOutAsParagraph(
269 out, make_description(sub), description_paragraph_width_, body_indent); // Format description as paragraph
270 } else {
271 out << detail::indent_block(make_description(sub), body_indent) << '\n';
272 }
273
274 if(sub->get_name().empty() && !sub->get_aliases().empty()) {
275 detail::format_aliases(out, sub->get_aliases(), column_width_ + 2);
276 }
277
278 out << detail::indent_block(make_positionals(sub), body_indent);
279 out << detail::indent_block(make_groups(sub, mode), body_indent);
280 out << detail::indent_block(make_subcommands(sub, mode), body_indent);
281 std::string footer_string = make_footer(sub);
282
283 if(mode == AppFormatMode::Sub && !footer_string.empty()) {
284 const auto *parent = sub->get_parent();
285 std::string parent_footer = (parent != nullptr) ? make_footer(sub->get_parent()) : std::string{};
286 if(footer_string == parent_footer) {
287 footer_string = "";
288 }
289 }
290 if(!footer_string.empty()) {
292 detail::streamOutAsParagraph(out, footer_string, footer_paragraph_width_); // Format footer as paragraph
293 } else {
294 out << footer_string;
295 }
296 }
297 return out.str();
298}
299
300CLI11_INLINE std::string Formatter::make_option(const Option *opt, bool is_positional) const {
301 std::stringstream out;
302 if(is_positional) {
303 const std::string left = " " + make_option_name(opt, true) + make_option_opts(opt);
304 const std::string desc = make_option_desc(opt);
305 out << std::setw(static_cast<int>(column_width_)) << std::left << left;
306
307 if(!desc.empty()) {
308 bool skipFirstLinePrefix = true;
309 if(left.length() >= column_width_) {
310 out << '\n';
311 skipFirstLinePrefix = false;
312 }
313 detail::streamOutAsParagraph(
314 out, desc, right_column_width_, std::string(column_width_, ' '), skipFirstLinePrefix);
315 }
316 } else {
317 const std::string namesCombined = make_option_name(opt, false);
318 const std::string opts = make_option_opts(opt);
319 const std::string desc = make_option_desc(opt);
320
321 // Split all names at comma and sort them into short names and long names
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);
328 else
329 vshortNames.push_back(name);
330 });
331
332 // Assemble short and long names
333 std::string shortNames = detail::join(vshortNames, ", ");
334 std::string longNames = detail::join(vlongNames, ", ");
335
336 // Calculate setw sizes
337 // Short names take enough width to align long names at the desired ratio
338 const auto shortNamesColumnWidth =
339 static_cast<int>(static_cast<float>(column_width_) * long_option_alignment_ratio_);
340 const auto longNamesColumnWidth = static_cast<int>(column_width_) - shortNamesColumnWidth;
341 int shortNamesOverSize = 0;
342
343 // Print short names
344 if(!shortNames.empty()) {
345 shortNames = " " + shortNames; // Indent
346 if(longNames.empty() && !opts.empty())
347 shortNames += opts; // Add opts if only short names and no long names
348 if(!longNames.empty())
349 shortNames += ",";
350 if(static_cast<int>(shortNames.length()) >= shortNamesColumnWidth) {
351 shortNames += " ";
352 shortNamesOverSize = static_cast<int>(shortNames.length()) - shortNamesColumnWidth;
353 }
354 out << std::setw(shortNamesColumnWidth) << std::left << shortNames;
355 } else {
356 out << std::setw(shortNamesColumnWidth) << std::left << "";
357 }
358
359 // Adjust long name column width in case of short names column reaching into long names column
360 shortNamesOverSize =
361 (std::min)(shortNamesOverSize, longNamesColumnWidth); // Prevent negative result with unsigned integers
362 const auto adjustedLongNamesColumnWidth = longNamesColumnWidth - shortNamesOverSize;
363
364 // Print long names
365 if(!longNames.empty()) {
366 if(!opts.empty())
367 longNames += opts;
368 if(static_cast<int>(longNames.length()) >= adjustedLongNamesColumnWidth)
369 longNames += " ";
370
371 out << std::setw(adjustedLongNamesColumnWidth) << std::left << longNames;
372 } else {
373 out << std::setw(adjustedLongNamesColumnWidth) << std::left << "";
374 }
375
376 if(!desc.empty()) {
377 bool skipFirstLinePrefix = true;
378 if(out.str().length() > column_width_) {
379 out << '\n';
380 skipFirstLinePrefix = false;
381 }
382 detail::streamOutAsParagraph(
383 out, desc, right_column_width_, std::string(column_width_, ' '), skipFirstLinePrefix);
384 }
385 }
386
387 out << '\n';
388 return out.str();
389}
390
391CLI11_INLINE std::string Formatter::make_option_name(const Option *opt, bool is_positional) const {
392 if(is_positional)
393 return opt->get_name(true, false);
394
395 return opt->get_name(false, true, !enable_default_flag_values_);
396}
397
398CLI11_INLINE std::string Formatter::make_option_opts(const Option *opt) const {
399 std::stringstream out;
400 // Help output should be stable across runs, so sort pointer-based sets by option name before printing.
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();
405 });
406 for(const Option *op : sorted)
407 out << " " << op->get_name();
408 };
409
410 if(!opt->get_option_text().empty()) {
411 out << " " << opt->get_option_text();
412 } else {
413 if(opt->get_type_size() != 0) {
414 if(enable_option_type_names_) {
415 if(!opt->get_type_name().empty())
416 out << " " << get_label(opt->get_type_name());
417 }
419 if(!opt->get_default_str().empty())
420 out << " [" << opt->get_default_str() << "] ";
421 }
422 if(opt->get_expected_max() == detail::expected_max_vector_size)
423 out << " ...";
424 else if(opt->get_expected_min() > 1)
425 out << " x " << opt->get_expected();
426
427 if(opt->get_required())
428 out << " " << get_label("REQUIRED");
429 }
430 if(!opt->get_envname().empty())
431 out << " (" << get_label("Env") << ":" << opt->get_envname() << ")";
432 if(!opt->get_needs().empty()) {
433 out << " " << get_label("Needs") << ":";
434 print_option_set(opt->get_needs());
435 }
436 if(!opt->get_excludes().empty()) {
437 out << " " << get_label("Excludes") << ":";
438 print_option_set(opt->get_excludes());
439 }
440 }
441 return out.str();
442}
443
444CLI11_INLINE std::string Formatter::make_option_desc(const Option *opt) const { return opt->get_description(); }
445
446CLI11_INLINE std::string Formatter::make_option_usage(const Option *opt) const {
447 // Note that these are positionals usages
448 std::stringstream out;
449 out << make_option_name(opt, true);
450 if(opt->get_expected_max() >= detail::expected_max_vector_size)
451 out << "...";
452 else if(opt->get_expected_max() > 1)
453 out << "(" << opt->get_expected() << "x)";
454
455 return opt->get_required() ? out.str() : "[" + out.str() + "]";
456}
457// [CLI11:formatter_inl_hpp:end]
458} // namespace CLI
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
std::size_t column_width_
The width of the left column (options/flags/subcommands)
Definition FormatterFwd.hpp:48
std::size_t footer_paragraph_width_
The width of the footer paragraph.
Definition FormatterFwd.hpp:60
bool enable_option_defaults_
options controlling formatting of options
Definition FormatterFwd.hpp:67
CLI11_NODISCARD std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition FormatterFwd.hpp:135
CLI11_NODISCARD bool is_description_paragraph_formatting_enabled() const
Get the current status of description paragraph formatting.
Definition FormatterFwd.hpp:157
std::size_t right_column_width_
The width of the right column (description of options/flags/subcommands)
Definition FormatterFwd.hpp:54
float long_option_alignment_ratio_
The alignment ratio for long options within the left column.
Definition FormatterFwd.hpp:51
std::size_t description_paragraph_width_
The width of the description paragraph at the top of help.
Definition FormatterFwd.hpp:57
CLI11_NODISCARD bool is_footer_paragraph_formatting_enabled() const
Get the current status of whether footer paragraph formatting is enabled.
Definition FormatterFwd.hpp:160
std::string make_groups(const App *app, AppFormatMode mode) const
This prints out all the groups of options.
Definition Formatter_inl.hpp:63
virtual std::string make_usage(const App *app, std::string name) const
This displays the usage line.
Definition Formatter_inl.hpp:113
virtual std::string make_option_name(const Option *, bool) const
This is the name part of an option, Default: left column.
Definition Formatter_inl.hpp:391
virtual std::string make_expanded(const App *sub, AppFormatMode mode) const
This prints out a subcommand in help-all.
Definition Formatter_inl.hpp:260
std::string make_help(const App *app, std::string, AppFormatMode mode) const override
This puts everything together.
Definition Formatter_inl.hpp:172
virtual CLI11_NODISCARD std::string make_group(std::string group, bool is_positional, std::vector< const Option * > opts) const
Definition Formatter_inl.hpp:42
virtual std::string make_option(const Option *, bool) const
This prints out an option help line, either positional or optional form.
Definition Formatter_inl.hpp:300
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
Definition Formatter_inl.hpp:164
virtual std::string make_option_usage(const Option *opt) const
This is used to print the name on the USAGE line.
Definition Formatter_inl.hpp:446
virtual std::string make_subcommand(const App *sub) const
This prints out a subcommand.
Definition Formatter_inl.hpp:249
virtual std::string make_positionals(const App *app) const
This prints out just the positionals "group".
Definition Formatter_inl.hpp:53
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const
This prints out all the subcommands.
Definition Formatter_inl.hpp:205
virtual std::string make_description(const App *app) const
This displays the description line.
Definition Formatter_inl.hpp:88
virtual std::string make_option_desc(const Option *) const
This is the description. Default: Right column, on new line if left column too large.
Definition Formatter_inl.hpp:444
virtual std::string make_option_opts(const Option *) const
This is the options part of the name, Default: combined into left column.
Definition Formatter_inl.hpp:398
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