CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
FormatterFwd.hpp
1// Copyright (c) 2017-2026, 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// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// [CLI11:public_includes:set]
12#include <functional>
13#include <map>
14#include <string>
15#include <utility>
16#include <vector>
17// [CLI11:public_includes:end]
18
19#include "StringTools.hpp"
20
21namespace CLI {
22// [CLI11:formatter_fwd_hpp:verbatim]
23
24class Option;
25class App;
26
31
32enum class AppFormatMode : std::uint8_t {
33 Normal,
34 All,
35 Sub,
36};
37
43 protected:
46
48 std::size_t column_width_{30};
49
52
54 std::size_t right_column_width_{65};
55
58
60 std::size_t footer_paragraph_width_{80};
61
64 bool enable_footer_formatting_{true};
65
68 bool enable_option_type_names_{true};
69 bool enable_default_flag_values_{true};
72 std::map<std::string, std::string> labels_{};
73
75 static std::string default_label(const std::string &key) { return key; }
76
80
81 public:
82 FormatterBase() = default;
83 FormatterBase(const FormatterBase &) = default;
84 FormatterBase(FormatterBase &&) = default;
85 FormatterBase &operator=(const FormatterBase &) = default;
86 FormatterBase &operator=(FormatterBase &&) = default;
87
89 virtual ~FormatterBase() noexcept {} // NOLINT(modernize-use-equals-default)
90
92 virtual std::string make_help(const App *, std::string, AppFormatMode) const = 0;
93
97
99 void label(std::string key, std::string val) { labels_[key] = val; }
100
102 void column_width(std::size_t val) { column_width_ = val; }
103
106 void long_option_alignment_ratio(float ratio) {
108 (ratio >= 0.0f) ? ((ratio <= 1.0f) ? ratio : 1.0f / ratio) : ((ratio < -1.0f) ? 1.0f / (-ratio) : -ratio);
109 }
110
112 void right_column_width(std::size_t val) { right_column_width_ = val; }
113
116
118 void footer_paragraph_width(std::size_t val) { footer_paragraph_width_ = val; }
122 void enable_footer_formatting(bool value = true) { enable_footer_formatting_ = value; }
123
125 void enable_option_defaults(bool value = true) { enable_option_defaults_ = value; }
127 void enable_option_type_names(bool value = true) { enable_option_type_names_ = value; }
129 void enable_default_flag_values(bool value = true) { enable_default_flag_values_ = value; }
133
135 CLI11_NODISCARD std::string get_label(std::string key) const {
136 auto it = labels_.find(key);
137 return it != labels_.end() ? it->second : default_label(key);
138 }
139
141 CLI11_NODISCARD std::size_t get_column_width() const { return column_width_; }
142
144 CLI11_NODISCARD std::size_t get_right_column_width() const { return right_column_width_; }
145
147 CLI11_NODISCARD std::size_t get_description_paragraph_width() const { return description_paragraph_width_; }
148
150 CLI11_NODISCARD std::size_t get_footer_paragraph_width() const { return footer_paragraph_width_; }
151
154 CLI11_NODISCARD float get_long_option_alignment_ratio() const { return long_option_alignment_ratio_; }
155
158
160 CLI11_NODISCARD bool is_footer_paragraph_formatting_enabled() const { return enable_footer_formatting_; }
161
163 CLI11_NODISCARD bool is_option_defaults_enabled() const { return enable_option_defaults_; }
164
166 CLI11_NODISCARD bool is_option_type_names_enabled() const { return enable_option_type_names_; }
167
169 CLI11_NODISCARD bool is_default_flag_values_enabled() const { return enable_default_flag_values_; }
170
172};
173
175class FormatterLambda final : public FormatterBase {
176 using funct_t = std::function<std::string(const App *, std::string, AppFormatMode)>;
177
179 funct_t lambda_;
180
181 public:
183 explicit FormatterLambda(funct_t funct) : lambda_(std::move(funct)) {}
184
186 ~FormatterLambda() noexcept override {} // NOLINT(modernize-use-equals-default)
187
189 std::string make_help(const App *app, std::string name, AppFormatMode mode) const override {
190 return lambda_(app, name, mode);
191 }
192};
193
196class Formatter : public FormatterBase {
197 public:
198 Formatter() = default;
199 Formatter(const Formatter &) = default;
200 Formatter(Formatter &&) = default;
201 Formatter &operator=(const Formatter &) = default;
202 Formatter &operator=(Formatter &&) = default;
203
206
209 CLI11_NODISCARD virtual std::string
210 make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
211
213 virtual std::string make_positionals(const App *app) const;
214
216 std::string make_groups(const App *app, AppFormatMode mode) const;
217
219 virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
220
222 virtual std::string make_subcommand(const App *sub) const;
223
225 virtual std::string make_expanded(const App *sub, AppFormatMode mode) const;
226
228 virtual std::string make_footer(const App *app) const;
229
231 virtual std::string make_description(const App *app) const;
232
234 virtual std::string make_usage(const App *app, std::string name) const;
235
237 std::string make_help(const App *app, std::string, AppFormatMode mode) const override;
238
242
244 virtual std::string make_option(const Option *, bool) const;
245
247 virtual std::string make_option_name(const Option *, bool) const;
248
250 virtual std::string make_option_opts(const Option *) const;
251
253 virtual std::string make_option_desc(const Option *) const;
254
256 virtual std::string make_option_usage(const Option *opt) const;
257
259};
260
261// [CLI11:formatter_fwd_hpp:end]
262} // namespace CLI
Creates a command line program, with very few defaults.
Definition App.hpp:115
Definition FormatterFwd.hpp:42
void footer_paragraph_width(std::size_t val)
Set the footer paragraph width.
Definition FormatterFwd.hpp:118
std::size_t column_width_
The width of the left column (options/flags/subcommands)
Definition FormatterFwd.hpp:48
void right_column_width(std::size_t val)
Set the right column width (description of options/flags/subcommands)
Definition FormatterFwd.hpp:112
std::map< std::string, std::string > labels_
The required help printout labels (user changeable) Values are Needs, Excludes, etc.
Definition FormatterFwd.hpp:72
std::size_t footer_paragraph_width_
The width of the footer paragraph.
Definition FormatterFwd.hpp:60
CLI11_NODISCARD std::size_t get_column_width() const
Get the current left column width (options/flags/subcommands)
Definition FormatterFwd.hpp:141
CLI11_NODISCARD float get_long_option_alignment_ratio() const
Get the current alignment ratio for long options within the left column.
Definition FormatterFwd.hpp:154
bool enable_option_defaults_
options controlling formatting of options
Definition FormatterFwd.hpp:67
void enable_default_flag_values(bool value=true)
enable default flag values to be printed
Definition FormatterFwd.hpp:129
CLI11_NODISCARD bool is_option_defaults_enabled() const
Get the current status of whether option defaults are printed.
Definition FormatterFwd.hpp:163
CLI11_NODISCARD std::size_t get_footer_paragraph_width() const
Get the current footer paragraph width.
Definition FormatterFwd.hpp:150
void long_option_alignment_ratio(float ratio)
Definition FormatterFwd.hpp:106
CLI11_NODISCARD std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition FormatterFwd.hpp:135
CLI11_NODISCARD std::size_t get_description_paragraph_width() const
Get the current description paragraph width at the top of help.
Definition FormatterFwd.hpp:147
void enable_option_defaults(bool value=true)
enable option defaults to be printed
Definition FormatterFwd.hpp:125
void enable_description_formatting(bool value=true)
enable formatting for description paragraph
Definition FormatterFwd.hpp:120
void label(std::string key, std::string val)
Set the "REQUIRED" or other labels.
Definition FormatterFwd.hpp:99
bool enable_description_formatting_
options controlling formatting for footer and descriptions
Definition FormatterFwd.hpp:63
void enable_footer_formatting(bool value=true)
disable formatting for footer paragraph
Definition FormatterFwd.hpp:122
static std::string default_label(const std::string &key)
Default user-facing help labels used when no override is set.
Definition FormatterFwd.hpp:75
CLI11_NODISCARD bool is_default_flag_values_enabled() const
Get the current status of whether default flag values are printed.
Definition FormatterFwd.hpp:169
CLI11_NODISCARD bool is_description_paragraph_formatting_enabled() const
Get the current status of description paragraph formatting.
Definition FormatterFwd.hpp:157
CLI11_NODISCARD std::size_t get_right_column_width() const
Get the current right column width (description of options/flags/subcommands)
Definition FormatterFwd.hpp:144
virtual ~FormatterBase() noexcept
Adding a destructor in this form to work around bug in GCC 4.7.
Definition FormatterFwd.hpp:89
virtual std::string make_help(const App *, std::string, AppFormatMode) const =0
This is the key method that puts together help.
void column_width(std::size_t val)
Set the left column width (options/flags/subcommands)
Definition FormatterFwd.hpp:102
std::size_t right_column_width_
The width of the right column (description of options/flags/subcommands)
Definition FormatterFwd.hpp:54
void enable_option_type_names(bool value=true)
enable option type names to be printed
Definition FormatterFwd.hpp:127
float long_option_alignment_ratio_
The alignment ratio for long options within the left column.
Definition FormatterFwd.hpp:51
std::size_t description_paragraph_width_
The width of the description paragraph at the top of help.
Definition FormatterFwd.hpp:57
CLI11_NODISCARD bool is_footer_paragraph_formatting_enabled() const
Get the current status of whether footer paragraph formatting is enabled.
Definition FormatterFwd.hpp:160
void description_paragraph_width(std::size_t val)
Set the description paragraph width at the top of help.
Definition FormatterFwd.hpp:115
CLI11_NODISCARD bool is_option_type_names_enabled() const
Get the current status of whether option type names are printed.
Definition FormatterFwd.hpp:166
Definition FormatterFwd.hpp:196
std::string make_groups(const App *app, AppFormatMode mode) const
This prints out all the groups of options.
Definition Formatter_inl.hpp:46
virtual std::string make_usage(const App *app, std::string name) const
This displays the usage line.
Definition Formatter_inl.hpp:96
virtual std::string make_option_name(const Option *, bool) const
This is the name part of an option, Default: left column.
Definition Formatter_inl.hpp:368
virtual std::string make_expanded(const App *sub, AppFormatMode mode) const
This prints out a subcommand in help-all.
Definition Formatter_inl.hpp:240
std::string make_help(const App *app, std::string, AppFormatMode mode) const override
This puts everything together.
Definition Formatter_inl.hpp:154
virtual CLI11_NODISCARD std::string make_group(std::string group, bool is_positional, std::vector< const Option * > opts) const
Definition Formatter_inl.hpp:25
virtual std::string make_option(const Option *, bool) const
This prints out an option help line, either positional or optional form.
Definition Formatter_inl.hpp:277
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
Definition Formatter_inl.hpp:146
virtual std::string make_option_usage(const Option *opt) const
This is used to print the name on the USAGE line.
Definition Formatter_inl.hpp:423
virtual std::string make_subcommand(const App *sub) const
This prints out a subcommand.
Definition Formatter_inl.hpp:229
virtual std::string make_positionals(const App *app) const
This prints out just the positionals "group".
Definition Formatter_inl.hpp:36
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const
This prints out all the subcommands.
Definition Formatter_inl.hpp:187
virtual std::string make_description(const App *app) const
This displays the description line.
Definition Formatter_inl.hpp:71
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_inl.hpp:421
virtual std::string make_option_opts(const Option *) const
This is the options part of the name, Default: combined into left column.
Definition Formatter_inl.hpp:375
This is a specialty override for lambda functions.
Definition FormatterFwd.hpp:175
std::string make_help(const App *app, std::string name, AppFormatMode mode) const override
This will simply call the lambda function.
Definition FormatterFwd.hpp:189
~FormatterLambda() noexcept override
Adding a destructor (mostly to make GCC 4.7 happy)
Definition FormatterFwd.hpp:186
FormatterLambda(funct_t funct)
Create a FormatterLambda with a lambda function.
Definition FormatterFwd.hpp:183
Definition Option.hpp:259