Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
CLI::Formatter Class Reference

#include <CLI11.hpp>

Inheritance diagram for CLI::Formatter:
Collaboration diagram for CLI::Formatter:

Public Member Functions

 Formatter ()=default
 
 Formatter (const Formatter &)=default
 
 Formatter (Formatter &&)=default
 
Overridables
virtual std::string make_group (std::string group, bool is_positional, std::vector< const Option * > opts) const
 
virtual std::string make_positionals (const App *app) const
 This prints out just the positionals "group".
 
std::string make_groups (const App *app, AppFormatMode mode) const
 This prints out all the groups of options.
 
virtual std::string make_subcommands (const App *app, AppFormatMode mode) const
 This prints out all the subcommands.
 
virtual std::string make_subcommand (const App *sub) const
 This prints out a subcommand.
 
virtual std::string make_expanded (const App *sub) const
 This prints out a subcommand in help-all.
 
virtual std::string make_footer (const App *app) const
 This prints out all the groups of options.
 
virtual std::string make_description (const App *app) const
 This displays the description line.
 
virtual std::string make_usage (const App *app, std::string name) const
 This displays the usage line.
 
std::string make_help (const App *, std::string, AppFormatMode) const override
 This puts everything together.
 
Options
virtual std::string make_option (const Option *opt, bool is_positional) const
 This prints out an option help line, either positional or optional form.
 
virtual std::string make_option_name (const Option *, bool) const
 This is the name part of an option, Default: left column.
 
virtual std::string make_option_opts (const Option *) const
 This is the options part of the name, Default: combined into left column.
 
virtual std::string make_option_desc (const Option *) const
 This is the description. Default: Right column, on new line if left column too large.
 
virtual std::string make_option_usage (const Option *opt) const
 This is used to print the name on the USAGE line.
 
- Public Member Functions inherited from CLI::FormatterBase
 FormatterBase ()=default
 
 FormatterBase (const FormatterBase &)=default
 
 FormatterBase (FormatterBase &&)=default
 
virtual ~FormatterBase () noexcept
 Adding a destructor in this form to work around bug in GCC 4.7.
 
void label (std::string key, std::string val)
 Set the "REQUIRED" label.
 
void column_width (std::size_t val)
 Set the column width.
 
std::string get_label (std::string key) const
 Get the current value of a name (REQUIRED, etc.)
 
std::size_t get_column_width () const
 Get the current column width.
 

Additional Inherited Members

- Protected Attributes inherited from CLI::FormatterBase
std::size_t column_width_ {30}
 The width of the first column.
 
std::map< std::string, std::string > labels_ {}
 The required help printout labels (user changeable) Values are Needs, Excludes, etc.
 

Detailed Description

This is the default Formatter for CLI11. It pretty prints help output, and is broken into quite a few overridable methods, to be highly customizable with minimal effort.

Definition at line 3254 of file CLI11.hpp.

Constructor & Destructor Documentation

◆ Formatter() [1/3]

CLI::Formatter::Formatter ( )
default

◆ Formatter() [2/3]

CLI::Formatter::Formatter ( const Formatter & )
default

◆ Formatter() [3/3]

CLI::Formatter::Formatter ( Formatter && )
default

Member Function Documentation

◆ make_description()

std::string CLI::Formatter::make_description ( const App * app) const
inlinevirtual

Definition at line 8005 of file CLI11.hpp.

8005 {
8006 std::string desc = app->get_description();
8007 auto min_options = app->get_require_option_min();
8008 auto max_options = app->get_require_option_max();
8009 if(app->get_required()) {
8010 desc += " REQUIRED ";
8011 }
8012 if((max_options == min_options) && (min_options > 0)) {
8013 if(min_options == 1) {
8014 desc += " \n[Exactly 1 of the following options is required]";
8015 } else {
8016 desc += " \n[Exactly " + std::to_string(min_options) + "options from the following list are required]";
8017 }
8018 } else if(max_options > 0) {
8019 if(min_options > 0) {
8020 desc += " \n[Between " + std::to_string(min_options) + " and " + std::to_string(max_options) +
8021 " of the follow options are required]";
8022 } else {
8023 desc += " \n[At most " + std::to_string(max_options) + " of the following options are allowed]";
8024 }
8025 } else if(min_options > 0) {
8026 desc += " \n[At least " + std::to_string(min_options) + " of the following options are required]";
8027 }
8028 return (!desc.empty()) ? desc + "\n" : std::string{};
8029}
application & app()
Here is the caller graph for this function:

◆ make_expanded()

std::string CLI::Formatter::make_expanded ( const App * sub) const
inlinevirtual

Definition at line 8153 of file CLI11.hpp.

8153 {
8154 std::stringstream out;
8155 out << sub->get_display_name() << "\n";
8156
8157 out << make_description(sub);
8158 out << make_positionals(sub);
8161
8162 // Drop blank spaces
8163 std::string tmp = detail::find_and_replace(out.str(), "\n\n", "\n");
8164 tmp = tmp.substr(0, tmp.size() - 1); // Remove the final '\n'
8165
8166 // Indent all but the first line (the name)
8167 return detail::find_and_replace(tmp, "\n", "\n ") + "\n";
8168}
virtual std::string make_description(const App *app) const
This displays the description line.
Definition CLI11.hpp:8005
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const
This prints out all the subcommands.
Definition CLI11.hpp:8105
virtual std::string make_positionals(const App *app) const
This prints out just the positionals "group".
Definition CLI11.hpp:7971
std::string make_groups(const App *app, AppFormatMode mode) const
This prints out all the groups of options.
Definition CLI11.hpp:7981
std::string find_and_replace(std::string str, std::string from, std::string to)
Find and replace a substring with another substring.
Definition CLI11.hpp:350
void sub(const Operand &op, uint32 imm)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_footer()

std::string CLI::Formatter::make_footer ( const App * app) const
inlinevirtual

Definition at line 8073 of file CLI11.hpp.

8073 {
8074 std::string footer = app->get_footer();
8075 if(footer.empty()) {
8076 return std::string{};
8077 }
8078 return footer + "\n";
8079}
Here is the caller graph for this function:

◆ make_group()

std::string CLI::Formatter::make_group ( std::string group,
bool is_positional,
std::vector< const Option * > opts ) const
inlinevirtual

This prints out a group of options with title

Definition at line 7960 of file CLI11.hpp.

7960 {
7961 std::stringstream out;
7962
7963 out << "\n" << group << ":\n";
7964 for(const Option *opt : opts) {
7965 out << make_option(opt, is_positional);
7966 }
7967
7968 return out.str();
7969}
virtual std::string make_option(const Option *opt, bool is_positional) const
This prints out an option help line, either positional or optional form.
Definition CLI11.hpp:3299
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_groups()

std::string CLI::Formatter::make_groups ( const App * app,
AppFormatMode mode ) const
inline

Definition at line 7981 of file CLI11.hpp.

7981 {
7982 std::stringstream out;
7983 std::vector<std::string> groups = app->get_groups();
7984
7985 // Options
7986 for(const std::string &group : groups) {
7987 std::vector<const Option *> opts = app->get_options([app, mode, &group](const Option *opt) {
7988 return opt->get_group() == group // Must be in the right group
7989 && opt->nonpositional() // Must not be a positional
7990 && (mode != AppFormatMode::Sub // If mode is Sub, then
7991 || (app->get_help_ptr() != opt // Ignore help pointer
7992 && app->get_help_all_ptr() != opt)); // Ignore help all pointer
7993 });
7994 if(!group.empty() && !opts.empty()) {
7995 out << make_group(group, false, opts);
7996
7997 if(group != groups.back())
7998 out << "\n";
7999 }
8000 }
8001
8002 return out.str();
8003}
virtual std::string make_group(std::string group, bool is_positional, std::vector< const Option * > opts) const
Definition CLI11.hpp:7960
const bpo::variables_map & get_options() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_help()

std::string CLI::Formatter::make_help ( const App * app,
std::string name,
AppFormatMode mode ) const
inlineoverridevirtual

Implements CLI::FormatterBase.

Definition at line 8081 of file CLI11.hpp.

8081 {
8082
8083 // This immediately forwards to the make_expanded method. This is done this way so that subcommands can
8084 // have overridden formatters
8085 if(mode == AppFormatMode::Sub)
8086 return make_expanded(app);
8087
8088 std::stringstream out;
8089 if((app->get_name().empty()) && (app->get_parent() != nullptr)) {
8090 if(app->get_group() != "Subcommands") {
8091 out << app->get_group() << ':';
8092 }
8093 }
8094
8095 out << make_description(app);
8096 out << make_usage(app, name);
8097 out << make_positionals(app);
8098 out << make_groups(app, mode);
8099 out << make_subcommands(app, mode);
8100 out << '\n' << make_footer(app);
8101
8102 return out.str();
8103}
std::string name
virtual std::string make_usage(const App *app, std::string name) const
This displays the usage line.
Definition CLI11.hpp:8031
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
Definition CLI11.hpp:8073
virtual std::string make_expanded(const App *sub) const
This prints out a subcommand in help-all.
Definition CLI11.hpp:8153
Here is the call graph for this function:

◆ make_option()

virtual std::string CLI::Formatter::make_option ( const Option * opt,
bool is_positional ) const
inlinevirtual

Definition at line 3299 of file CLI11.hpp.

3299 {
3300 std::stringstream out;
3302 out, make_option_name(opt, is_positional) + make_option_opts(opt), make_option_desc(opt), column_width_);
3303 return out.str();
3304 }
std::size_t column_width_
The width of the first column.
Definition CLI11.hpp:3183
virtual std::string make_option_opts(const Option *) const
This is the options part of the name, Default: combined into left column.
Definition CLI11.hpp:8177
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 CLI11.hpp:8208
virtual std::string make_option_name(const Option *, bool) const
This is the name part of an option, Default: left column.
Definition CLI11.hpp:8170
std::ostream & format_help(std::ostream &out, std::string name, std::string description, std::size_t wid)
Print a two part "help" string.
Definition CLI11.hpp:295
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_option_desc()

std::string CLI::Formatter::make_option_desc ( const Option * opt) const
inlinevirtual

Definition at line 8208 of file CLI11.hpp.

8208{ return opt->get_description(); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_option_name()

std::string CLI::Formatter::make_option_name ( const Option * opt,
bool is_positional ) const
inlinevirtual

Definition at line 8170 of file CLI11.hpp.

8170 {
8171 if(is_positional)
8172 return opt->get_name(true, false);
8173
8174 return opt->get_name(false, true);
8175}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_option_opts()

std::string CLI::Formatter::make_option_opts ( const Option * opt) const
inlinevirtual

Definition at line 8177 of file CLI11.hpp.

8177 {
8178 std::stringstream out;
8179
8180 if(opt->get_type_size() != 0) {
8181 if(!opt->get_type_name().empty())
8182 out << " " << get_label(opt->get_type_name());
8183 if(!opt->get_default_str().empty())
8184 out << "=" << opt->get_default_str();
8185 if(opt->get_expected_max() == detail::expected_max_vector_size)
8186 out << " ...";
8187 else if(opt->get_expected_min() > 1)
8188 out << " x " << opt->get_expected();
8189
8190 if(opt->get_required())
8191 out << " " << get_label("REQUIRED");
8192 }
8193 if(!opt->get_envname().empty())
8194 out << " (" << get_label("Env") << ":" << opt->get_envname() << ")";
8195 if(!opt->get_needs().empty()) {
8196 out << " " << get_label("Needs") << ":";
8197 for(const Option *op : opt->get_needs())
8198 out << " " << op->get_name();
8199 }
8200 if(!opt->get_excludes().empty()) {
8201 out << " " << get_label("Excludes") << ":";
8202 for(const Option *op : opt->get_excludes())
8203 out << " " << op->get_name();
8204 }
8205 return out.str();
8206}
std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition CLI11.hpp:3219
constexpr int expected_max_vector_size
Definition CLI11.hpp:176
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_option_usage()

std::string CLI::Formatter::make_option_usage ( const Option * opt) const
inlinevirtual

Definition at line 8210 of file CLI11.hpp.

8210 {
8211 // Note that these are positionals usages
8212 std::stringstream out;
8213 out << make_option_name(opt, true);
8214 if(opt->get_expected_max() >= detail::expected_max_vector_size)
8215 out << "...";
8216 else if(opt->get_expected_max() > 1)
8217 out << "(" << opt->get_expected() << "x)";
8218
8219 return opt->get_required() ? out.str() : "[" + out.str() + "]";
8220}
Here is the call graph for this function:

◆ make_positionals()

std::string CLI::Formatter::make_positionals ( const App * app) const
inlinevirtual

Definition at line 7971 of file CLI11.hpp.

7971 {
7972 std::vector<const Option *> opts =
7973 app->get_options([](const Option *opt) { return !opt->get_group().empty() && opt->get_positional(); });
7974
7975 if(opts.empty())
7976 return std::string();
7977
7978 return make_group(get_label("Positionals"), true, opts);
7979}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_subcommand()

std::string CLI::Formatter::make_subcommand ( const App * sub) const
inlinevirtual

Definition at line 8147 of file CLI11.hpp.

8147 {
8148 std::stringstream out;
8149 detail::format_help(out, sub->get_name(), sub->get_description(), column_width_);
8150 return out.str();
8151}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_subcommands()

std::string CLI::Formatter::make_subcommands ( const App * app,
AppFormatMode mode ) const
inlinevirtual

Definition at line 8105 of file CLI11.hpp.

8105 {
8106 std::stringstream out;
8107
8108 std::vector<const App *> subcommands = app->get_subcommands({});
8109
8110 // Make a list in definition order of the groups seen
8111 std::vector<std::string> subcmd_groups_seen;
8112 for(const App *com : subcommands) {
8113 if(com->get_name().empty()) {
8114 if(!com->get_group().empty()) {
8115 out << make_expanded(com);
8116 }
8117 continue;
8118 }
8119 std::string group_key = com->get_group();
8120 if(!group_key.empty() &&
8121 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key](std::string a) {
8122 return detail::to_lower(a) == detail::to_lower(group_key);
8123 }) == subcmd_groups_seen.end())
8124 subcmd_groups_seen.push_back(group_key);
8125 }
8126
8127 // For each group, filter out and print subcommands
8128 for(const std::string &group : subcmd_groups_seen) {
8129 out << "\n" << group << ":\n";
8130 std::vector<const App *> subcommands_group = app->get_subcommands(
8131 [&group](const App *sub_app) { return detail::to_lower(sub_app->get_group()) == detail::to_lower(group); });
8132 for(const App *new_com : subcommands_group) {
8133 if(new_com->get_name().empty())
8134 continue;
8135 if(mode != AppFormatMode::All) {
8136 out << make_subcommand(new_com);
8137 } else {
8138 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
8139 out << "\n";
8140 }
8141 }
8142 }
8143
8144 return out.str();
8145}
virtual std::string make_subcommand(const App *sub) const
This prints out a subcommand.
Definition CLI11.hpp:8147
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition CLI11.hpp:336
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_usage()

std::string CLI::Formatter::make_usage ( const App * app,
std::string name ) const
inlinevirtual

Definition at line 8031 of file CLI11.hpp.

8031 {
8032 std::stringstream out;
8033
8034 out << get_label("Usage") << ":" << (name.empty() ? "" : " ") << name;
8035
8036 std::vector<std::string> groups = app->get_groups();
8037
8038 // Print an Options badge if any options exist
8039 std::vector<const Option *> non_pos_options =
8040 app->get_options([](const Option *opt) { return opt->nonpositional(); });
8041 if(!non_pos_options.empty())
8042 out << " [" << get_label("OPTIONS") << "]";
8043
8044 // Positionals need to be listed here
8045 std::vector<const Option *> positionals = app->get_options([](const Option *opt) { return opt->get_positional(); });
8046
8047 // Print out positionals if any are left
8048 if(!positionals.empty()) {
8049 // Convert to help names
8050 std::vector<std::string> positional_names(positionals.size());
8051 std::transform(positionals.begin(), positionals.end(), positional_names.begin(), [this](const Option *opt) {
8052 return make_option_usage(opt);
8053 });
8054
8055 out << " " << detail::join(positional_names, " ");
8056 }
8057
8058 // Add a marker if subcommands are expected or optional
8059 if(!app->get_subcommands(
8060 [](const CLI::App *subc) { return ((!subc->get_disabled()) && (!subc->get_name().empty())); })
8061 .empty()) {
8062 out << " " << (app->get_require_subcommand_min() == 0 ? "[" : "")
8063 << get_label(app->get_require_subcommand_max() < 2 || app->get_require_subcommand_min() > 1 ? "SUBCOMMAND"
8064 : "SUBCOMMANDS")
8065 << (app->get_require_subcommand_min() == 0 ? "]" : "");
8066 }
8067
8068 out << std::endl;
8069
8070 return out.str();
8071}
Creates a command line program, with very few defaults.
Definition CLI11.hpp:4614
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition CLI11.hpp:196
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: