Formatter.hpp
5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
// Distributed under the 3-Clause BSD License. See accompanying
// file LICENSE or https://github.com/CLIUtils/CLI11 for details.
#include <string>
#include <map>
#include "CLI/StringTools.hpp"
namespace CLI {
class Option;
class App;
/// This enum signifies what situation the option is beig printed in.
///
/// This is passed in as part of the built in formatter in App; it is
/// possible that a custom App formatter could avoid using it, however.
enum class OptionFormatMode {
Usage, //< In the program usage line
Positional, //< In the positionals
Optional //< In the normal optionals
};
/// This enum signifies the type of help requested
///
/// This is passed in by App; all user classes must accept this as
/// the second argument.
enum class AppFormatMode {
Normal, //< The normal, detailed help
All, //< A fully expanded help
Sub, //< Used when printed as part of expanded subcommand
};
/// This is an example formatter (and also the default formatter)
/// For option help.
class OptionFormatter {
protected:
/// @name Options
///@{
/// @brief The required help printout labels (user changeable)
/// Values are REQUIRED, NEEDS, EXCLUDES
std::map<std::string, std::string> labels_{{"REQUIRED", "(REQUIRED)"}};
/// The width of the first column
size_t column_width_{30};
///@}
/// @name Basic
///@{
public:
OptionFormatter() = default;
OptionFormatter(const OptionFormatter &) = default;
OptionFormatter(OptionFormatter &&) = default;
///@}
/// @name Setters
///@{
/// Set the "REQUIRED" label
void label(std::string key, std::string val) { labels_[key] = val; }
/// Set the column width
void column_width(size_t val) { column_width_ = val; }
///@}
/// @name Getters
///@{
/// Get the current value of a name (REQUIRED, etc.)
std::string get_label(std::string key) const {
if(labels_.find(key) == labels_.end())
return key;
else
return labels_.at(key);
}
/// Get the current column width
size_t get_column_width() const { return column_width_; }
///@}
/// @name Overridables
///@{
/// @brief This is the name part of an option, Default: left column
virtual std::string make_name(const Option *, OptionFormatMode) const;
/// @brief This is the options part of the name, Default: combined into left column
virtual std::string make_opts(const Option *) const;
/// @brief This is the description. Default: Right column, on new line if left column too large
virtual std::string make_desc(const Option *) const;
/// @brief This is used to print the name on the USAGE line (by App formatter)
virtual std::string make_usage(const Option *opt) const;
/// @brief This is the standard help combiner that does the "default" thing.
virtual std::string operator()(const Option *opt, OptionFormatMode mode) const {
std::stringstream out;
if(mode == OptionFormatMode::Usage)
out << make_usage(opt);
else
detail::format_help(out, make_name(opt, mode) + make_opts(opt), make_desc(opt), column_width_);
return out.str();
}
///@}
};
class AppFormatter {
/// @name Options
///@{
/// The width of the first column
size_t column_width_{30};
/// @brief The required help printout labels (user changeable)
/// Values are Needs, Excludes, etc.
std::map<std::string, std::string> labels_;
///@}
/// @name Basic
///@{
public:
AppFormatter() = default;
AppFormatter(const AppFormatter &) = default;
AppFormatter(AppFormatter &&) = default;
///@}
/// @name Setters
///@{
/// Set the "REQUIRED" label
void label(std::string key, std::string val) { labels_[key] = val; }
/// Set the column width
void column_width(size_t val) { column_width_ = val; }
///@}
/// @name Getters
///@{
/// Get the current value of a name (REQUIRED, etc.)
std::string get_label(std::string key) const {
if(labels_.find(key) == labels_.end())
return key;
else
return labels_.at(key);
}
/// Get the current column width
size_t get_column_width() const { return column_width_; }
/// @name Overridables
///@{
/// This prints out a group of options
virtual std::string make_group(std::string group, std::vector<const Option *> opts, OptionFormatMode mode) const;
/// This prints out all the groups of options
virtual std::string make_groups(const App *app, AppFormatMode mode) const;
/// This prints out all the subcommands
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
/// This prints out a subcommand
virtual std::string make_subcommand(const App *sub) const;
/// This prints out a subcommand in help-all
virtual std::string make_expanded(const App *sub) const;
/// This prints out all the groups of options
virtual std::string make_footer(const App *app) const;
/// This displays the description line
virtual std::string make_description(const App *app) const;
/// This displays the usage line
virtual std::string make_usage(const App *app, std::string name) const;
/// This puts everything together
virtual std::string operator()(const App *, std::string, AppFormatMode) const;
///@}
};
} // namespace CLI