CLI11
C++11 Command Line Interface Parser
FormatterFwd.hpp
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  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(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  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 *, std::string, AppFormatMode) const override;
148 
152 
154  virtual std::string make_option(const Option *opt, bool is_positional) const {
155  std::stringstream out;
156  detail::format_help(
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
virtual std::string make_option_usage(const Option *opt) const
This is used to print the name on the USAGE line.
Definition: Formatter.hpp:261
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:259
virtual std::string make_help(const App *, std::string, AppFormatMode) const =0
This is the key method that puts together help.
Definition: FormatterFwd.hpp:109
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
void label(std::string key, std::string val)
Set the "REQUIRED" label.
Definition: FormatterFwd.hpp:64
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const
This prints out all the subcommands.
Definition: Formatter.hpp:159
std::string make_groups(const App *app, AppFormatMode mode) const
This prints out all the groups of options.
Definition: Formatter.hpp:35
virtual ~FormatterBase() noexcept
Adding a destructor in this form to work around bug in GCC 4.7.
Definition: FormatterFwd.hpp:54
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:229
virtual std::string make_description(const App *app) const
This displays the description line.
Definition: Formatter.hpp:59
virtual std::string make_option_name(const Option *, bool) const
This is the name part of an option, Default: left column.
Definition: Formatter.hpp:222
std::map< std::string, std::string > labels_
The required help printout labels (user changeable) Values are Needs, Excludes, etc.
Definition: FormatterFwd.hpp:42
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
Definition: Formatter.hpp:127
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
Creates a command line program, with very few defaults.
Definition: App.hpp:59
size_t get_column_width() const
Get the current column width.
Definition: FormatterFwd.hpp:82
std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition: FormatterFwd.hpp:74
This is a specialty override for lambda functions.
Definition: FormatterFwd.hpp:88
virtual std::string make_positionals(const App *app) const
This prints out just the positionals "group".
Definition: Formatter.hpp:25
FormatterLambda(funct_t funct)
Create a FormatterLambda with a lambda function.
Definition: FormatterFwd.hpp:96
virtual std::string make_expanded(const App *sub) const
This prints out a subcommand in help-all.
Definition: Formatter.hpp:205
size_t column_width_
The width of the first column.
Definition: FormatterFwd.hpp:38
~FormatterLambda() noexcept override
Adding a destructor (mostly to make GCC 4.7 happy)
Definition: FormatterFwd.hpp:99
std::string make_help(const App *, std::string, AppFormatMode) const override
This puts everything together.
Definition: Formatter.hpp:135
virtual std::string make_usage(const App *app, std::string name) const
This displays the usage line.
Definition: Formatter.hpp:85
Definition: FormatterFwd.hpp:32
Definition: Option.hpp:206
virtual std::string make_subcommand(const App *sub) const
This prints out a subcommand.
Definition: Formatter.hpp:199
void column_width(size_t val)
Set the column width.
Definition: FormatterFwd.hpp:67
virtual std::string make_group(std::string group, bool is_positional, std::vector< const Option * > opts) const
Definition: Formatter.hpp:14