CLI11  1.9.0
FormatterFwd.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // Distributed under the 3-Clause BSD License. See accompanying
4 // file LICENSE or https://github.com/CLIUtils/CLI11 for details.
5 
6 #include <map>
7 #include <string>
8 #include <utility>
9 
10 #include "CLI/StringTools.hpp"
11 
12 namespace CLI {
13 
14 class Option;
15 class App;
16 
21 
22 enum class AppFormatMode {
23  Normal, //< The normal, detailed help
24  All, //< A fully expanded help
25  Sub, //< Used when printed as part of expanded subcommand
26 };
27 
33  protected:
36 
38  std::size_t column_width_{30};
39 
42  std::map<std::string, std::string> labels_{};
43 
47 
48  public:
49  FormatterBase() = default;
50  FormatterBase(const FormatterBase &) = default;
51  FormatterBase(FormatterBase &&) = default;
52 
54  virtual ~FormatterBase() noexcept {} // NOLINT(modernize-use-equals-default)
55 
57  virtual std::string make_help(const App *, std::string, AppFormatMode) const = 0;
58 
62 
64  void label(std::string key, std::string val) { labels_[key] = val; }
65 
67  void column_width(std::size_t val) { column_width_ = val; }
68 
72 
74  std::string get_label(std::string key) const {
75  if(labels_.find(key) == labels_.end())
76  return key;
77  else
78  return labels_.at(key);
79  }
80 
82  std::size_t get_column_width() const { return column_width_; }
83 
85 };
86 
88 class FormatterLambda final : public FormatterBase {
89  using funct_t = std::function<std::string(const App *, std::string, AppFormatMode)>;
90 
92  funct_t lambda_;
93 
94  public:
96  explicit FormatterLambda(funct_t funct) : lambda_(std::move(funct)) {}
97 
99  ~FormatterLambda() noexcept override {} // NOLINT(modernize-use-equals-default)
100 
102  std::string make_help(const App *app, std::string name, AppFormatMode mode) const override {
103  return lambda_(app, name, mode);
104  }
105 };
106 
109 class Formatter : public FormatterBase {
110  public:
111  Formatter() = default;
112  Formatter(const Formatter &) = default;
113  Formatter(Formatter &&) = default;
114 
117 
120  virtual std::string make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
121 
123  virtual std::string make_positionals(const App *app) const;
124 
126  std::string make_groups(const App *app, AppFormatMode mode) const;
127 
129  virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
130 
132  virtual std::string make_subcommand(const App *sub) const;
133 
135  virtual std::string make_expanded(const App *sub) const;
136 
138  virtual std::string make_footer(const App *app) const;
139 
141  virtual std::string make_description(const App *app) const;
142 
144  virtual std::string make_usage(const App *app, std::string name) const;
145 
147  std::string make_help(const App * /*app*/, std::string, AppFormatMode) const override;
148 
152 
154  virtual std::string make_option(const Option *opt, bool is_positional) const {
155  std::stringstream out;
157  out, make_option_name(opt, is_positional) + make_option_opts(opt), make_option_desc(opt), column_width_);
158  return out.str();
159  }
160 
162  virtual std::string make_option_name(const Option *, bool) const;
163 
165  virtual std::string make_option_opts(const Option *) const;
166 
168  virtual std::string make_option_desc(const Option *) const;
169 
171  virtual std::string make_option_usage(const Option *opt) const;
172 
174 };
175 
176 } // namespace CLI
CLI::FormatterBase
Definition: FormatterFwd.hpp:32
CLI::FormatterBase::label
void label(std::string key, std::string val)
Set the "REQUIRED" label.
Definition: FormatterFwd.hpp:64
CLI::Formatter::make_usage
virtual std::string make_usage(const App *app, std::string name) const
This displays the usage line.
Definition: Formatter.hpp:85
CLI::FormatterBase::get_label
std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition: FormatterFwd.hpp:74
CLI::FormatterBase::labels_
std::map< std::string, std::string > labels_
The required help printout labels (user changeable) Values are Needs, Excludes, etc.
Definition: FormatterFwd.hpp:42
CLI::AppFormatMode::Normal
@ Normal
CLI::Formatter::make_option_usage
virtual std::string make_option_usage(const Option *opt) const
This is used to print the name on the USAGE line.
Definition: Formatter.hpp:264
CLI::FormatterBase::column_width_
std::size_t column_width_
The width of the first column.
Definition: FormatterFwd.hpp:38
CLI::FormatterBase::column_width
void column_width(std::size_t val)
Set the column width.
Definition: FormatterFwd.hpp:67
CLI::FormatterLambda::make_help
std::string make_help(const App *app, std::string name, AppFormatMode mode) const override
This will simply call the lambda function.
Definition: FormatterFwd.hpp:102
CLI::Formatter::make_help
std::string make_help(const App *, std::string, AppFormatMode) const override
This puts everything together.
Definition: Formatter.hpp:135
CLI::FormatterBase::FormatterBase
FormatterBase()=default
CLI
Definition: App.hpp:27
CLI::Formatter::make_option_desc
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.hpp:262
CLI::AppFormatMode::Sub
@ Sub
CLI::Formatter::make_group
virtual std::string make_group(std::string group, bool is_positional, std::vector< const Option * > opts) const
Definition: Formatter.hpp:14
CLI::Formatter::make_option
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: FormatterFwd.hpp:154
CLI::Formatter::make_positionals
virtual std::string make_positionals(const App *app) const
This prints out just the positionals "group".
Definition: Formatter.hpp:25
CLI::FormatterLambda::~FormatterLambda
~FormatterLambda() noexcept override
Adding a destructor (mostly to make GCC 4.7 happy)
Definition: FormatterFwd.hpp:99
CLI::Formatter::make_subcommands
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const
This prints out all the subcommands.
Definition: Formatter.hpp:159
CLI::Formatter::make_expanded
virtual std::string make_expanded(const App *sub) const
This prints out a subcommand in help-all.
Definition: Formatter.hpp:207
CLI::Formatter::make_option_name
virtual std::string make_option_name(const Option *, bool) const
This is the name part of an option, Default: left column.
Definition: Formatter.hpp:224
CLI::Formatter::make_footer
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
Definition: Formatter.hpp:127
CLI::Formatter::make_subcommand
virtual std::string make_subcommand(const App *sub) const
This prints out a subcommand.
Definition: Formatter.hpp:201
CLI::detail::format_help
std::ostream & format_help(std::ostream &out, std::string name, std::string description, std::size_t wid)
Print a two part "help" string.
Definition: StringTools.hpp:155
CLI::Option
Definition: Option.hpp:228
CLI::Formatter
Definition: FormatterFwd.hpp:109
CLI::FormatterBase::~FormatterBase
virtual ~FormatterBase() noexcept
Adding a destructor in this form to work around bug in GCC 4.7.
Definition: FormatterFwd.hpp:54
CLI::Formatter::Formatter
Formatter()=default
CLI::AppFormatMode
AppFormatMode
Definition: FormatterFwd.hpp:22
StringTools.hpp
CLI::Formatter::make_description
virtual std::string make_description(const App *app) const
This displays the description line.
Definition: Formatter.hpp:59
CLI::App
Creates a command line program, with very few defaults.
Definition: App.hpp:61
CLI::AppFormatMode::All
@ All
CLI::Formatter::make_option_opts
virtual std::string make_option_opts(const Option *) const
This is the options part of the name, Default: combined into left column.
Definition: Formatter.hpp:231
CLI::FormatterBase::make_help
virtual std::string make_help(const App *, std::string, AppFormatMode) const =0
This is the key method that puts together help.
CLI::Formatter::make_groups
std::string make_groups(const App *app, AppFormatMode mode) const
This prints out all the groups of options.
Definition: Formatter.hpp:35
CLI::FormatterLambda::FormatterLambda
FormatterLambda(funct_t funct)
Create a FormatterLambda with a lambda function.
Definition: FormatterFwd.hpp:96
CLI::FormatterLambda
This is a specialty override for lambda functions.
Definition: FormatterFwd.hpp:88
CLI::FormatterBase::get_column_width
std::size_t get_column_width() const
Get the current column width.
Definition: FormatterFwd.hpp:82