CLI11  2.3.2
FormatterFwd.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2023, 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 // [CLI11:public_includes:set]
10 #include <functional>
11 #include <map>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 // [CLI11:public_includes:end]
16 
17 #include "StringTools.hpp"
18 
19 namespace CLI {
20 // [CLI11:formatter_fwd_hpp:verbatim]
21 
22 class Option;
23 class App;
24 
29 
30 enum class AppFormatMode {
31  Normal,
32  All,
33  Sub,
34 };
35 
41  protected:
44 
46  std::size_t column_width_{30};
47 
50  std::map<std::string, std::string> labels_{};
51 
55 
56  public:
57  FormatterBase() = default;
58  FormatterBase(const FormatterBase &) = default;
59  FormatterBase(FormatterBase &&) = default;
60  FormatterBase &operator=(const FormatterBase &) = default;
61  FormatterBase &operator=(FormatterBase &&) = default;
62 
64  virtual ~FormatterBase() noexcept {} // NOLINT(modernize-use-equals-default)
65 
67  virtual std::string make_help(const App *, std::string, AppFormatMode) const = 0;
68 
72 
74  void label(std::string key, std::string val) { labels_[key] = val; }
75 
77  void column_width(std::size_t val) { column_width_ = val; }
78 
82 
84  CLI11_NODISCARD std::string get_label(std::string key) const {
85  if(labels_.find(key) == labels_.end())
86  return key;
87  return labels_.at(key);
88  }
89 
91  CLI11_NODISCARD std::size_t get_column_width() const { return column_width_; }
92 
94 };
95 
97 class FormatterLambda final : public FormatterBase {
98  using funct_t = std::function<std::string(const App *, std::string, AppFormatMode)>;
99 
101  funct_t lambda_;
102 
103  public:
105  explicit FormatterLambda(funct_t funct) : lambda_(std::move(funct)) {}
106 
108  ~FormatterLambda() noexcept override {} // NOLINT(modernize-use-equals-default)
109 
111  std::string make_help(const App *app, std::string name, AppFormatMode mode) const override {
112  return lambda_(app, name, mode);
113  }
114 };
115 
118 class Formatter : public FormatterBase {
119  public:
120  Formatter() = default;
121  Formatter(const Formatter &) = default;
122  Formatter(Formatter &&) = default;
123  Formatter &operator=(const Formatter &) = default;
124  Formatter &operator=(Formatter &&) = default;
125 
128 
131  CLI11_NODISCARD virtual std::string
132  make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
133 
135  virtual std::string make_positionals(const App *app) const;
136 
138  std::string make_groups(const App *app, AppFormatMode mode) const;
139 
141  virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
142 
144  virtual std::string make_subcommand(const App *sub) const;
145 
147  virtual std::string make_expanded(const App *sub) const;
148 
150  virtual std::string make_footer(const App *app) const;
151 
153  virtual std::string make_description(const App *app) const;
154 
156  virtual std::string make_usage(const App *app, std::string name) const;
157 
159  std::string make_help(const App * /*app*/, std::string, AppFormatMode) const override;
160 
164 
166  virtual std::string make_option(const Option *opt, bool is_positional) const {
167  std::stringstream out;
169  out, make_option_name(opt, is_positional) + make_option_opts(opt), make_option_desc(opt), column_width_);
170  return out.str();
171  }
172 
174  virtual std::string make_option_name(const Option *, bool) const;
175 
177  virtual std::string make_option_opts(const Option *) const;
178 
180  virtual std::string make_option_desc(const Option *) const;
181 
183  virtual std::string make_option_usage(const Option *opt) const;
184 
186 };
187 
188 // [CLI11:formatter_fwd_hpp:end]
189 } // namespace CLI
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:166
virtual ~FormatterBase() noexcept
Adding a destructor in this form to work around bug in GCC 4.7.
Definition: FormatterFwd.hpp:64
CLI11_NODISCARD std::size_t get_column_width() const
Get the current column width.
Definition: FormatterFwd.hpp:91
CLI11_INLINE std::ostream & format_help(std::ostream &out, std::string name, const std::string &description, std::size_t wid)
Print a two part "help" string.
~FormatterLambda() noexcept override
Adding a destructor (mostly to make GCC 4.7 happy)
Definition: FormatterFwd.hpp:108
Definition: App.hpp:34
Formatter()=default
Formatter & operator=(const Formatter &)=default
virtual std::string make_option_opts(const Option *) const
This is the options part of the name, Default: combined into left column.
FormatterBase & operator=(const FormatterBase &)=default
A fully expanded help.
Definition: Option.hpp:228
STL namespace.
virtual std::string make_option_desc(const Option *) const
This is the description. Default: Right column, on new line if left column too large.
std::map< std::string, std::string > labels_
The required help printout labels (user changeable) Values are Needs, Excludes, etc.
Definition: FormatterFwd.hpp:50
virtual std::string make_description(const App *app) const
This displays the description line.
void column_width(std::size_t val)
Set the column width.
Definition: FormatterFwd.hpp:77
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.
FormatterLambda(funct_t funct)
Create a FormatterLambda with a lambda function.
Definition: FormatterFwd.hpp:105
virtual std::string make_option_usage(const Option *opt) const
This is used to print the name on the USAGE line.
virtual std::string make_help(const App *, std::string, AppFormatMode) const =0
This is the key method that puts together help.
FormatterBase()=default
AppFormatMode
Definition: FormatterFwd.hpp:30
void label(std::string key, std::string val)
Set the "REQUIRED" label.
Definition: FormatterFwd.hpp:74
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
Definition: FormatterFwd.hpp:40
virtual std::string make_option_name(const Option *, bool) const
This is the name part of an option, Default: left column.
The normal, detailed help.
std::size_t column_width_
The width of the first column.
Definition: FormatterFwd.hpp:46
This is a specialty override for lambda functions.
Definition: FormatterFwd.hpp:97
#define CLI11_NODISCARD
Definition: Macros.hpp:47
virtual std::string make_subcommand(const App *sub) const
This prints out a subcommand.
Definition: FormatterFwd.hpp:118
std::string make_help(const App *app, std::string name, AppFormatMode mode) const override
This will simply call the lambda function.
Definition: FormatterFwd.hpp:111
Creates a command line program, with very few defaults.
Definition: App.hpp:88
std::string make_help(const App *, std::string, AppFormatMode) const override
This puts everything together.
Used when printed as part of expanded subcommand.
virtual std::string make_positionals(const App *app) const
This prints out just the positionals "group".
virtual std::string make_usage(const App *app, std::string name) const
This displays the usage line.
CLI11_NODISCARD std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition: FormatterFwd.hpp:84
virtual std::string make_expanded(const App *sub) const
This prints out a subcommand in help-all.
virtual CLI11_NODISCARD std::string make_group(std::string group, bool is_positional, std::vector< const Option *> opts) const