CLI11 2.6.1
 
Loading...
Searching...
No Matches
FormatterFwd.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2025, 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 {
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
65
68 std::map<std::string, std::string> labels_{};
69
73
74 public:
75 FormatterBase() = default;
76 FormatterBase(const FormatterBase &) = default;
80
82 virtual ~FormatterBase() noexcept {} // NOLINT(modernize-use-equals-default)
83
85 virtual std::string make_help(const App *, std::string, AppFormatMode) const = 0;
86
90
92 void label(std::string key, std::string val) { labels_[key] = val; }
93
95 void column_width(std::size_t val) { column_width_ = val; }
96
100
102 void right_column_width(std::size_t val) { right_column_width_ = val; }
103
106
108 void footer_paragraph_width(std::size_t val) { footer_paragraph_width_ = val; }
112 void enable_footer_formatting(bool value = true) { enable_footer_formatting_ = value; }
116
118 CLI11_NODISCARD std::string get_label(std::string key) const {
119 if(labels_.find(key) == labels_.end())
120 return key;
121 return labels_.at(key);
122 }
123
125 CLI11_NODISCARD std::size_t get_column_width() const { return column_width_; }
126
129
132
135
138
141
143};
144
146class FormatterLambda final : public FormatterBase {
147 using funct_t = std::function<std::string(const App *, std::string, AppFormatMode)>;
148
150 funct_t lambda_;
151
152 public:
154 explicit FormatterLambda(funct_t funct) : lambda_(std::move(funct)) {}
155
157 ~FormatterLambda() noexcept override {} // NOLINT(modernize-use-equals-default)
158
160 std::string make_help(const App *app, std::string name, AppFormatMode mode) const override {
161 return lambda_(app, name, mode);
162 }
163};
164
167class Formatter : public FormatterBase {
168 public:
169 Formatter() = default;
170 Formatter(const Formatter &) = default;
171 Formatter(Formatter &&) = default;
172 Formatter &operator=(const Formatter &) = default;
174
177
180 CLI11_NODISCARD virtual std::string
181 make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
182
184 virtual std::string make_positionals(const App *app) const;
185
187 std::string make_groups(const App *app, AppFormatMode mode) const;
188
190 virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
191
193 virtual std::string make_subcommand(const App *sub) const;
194
196 virtual std::string make_expanded(const App *sub, AppFormatMode mode) const;
197
199 virtual std::string make_footer(const App *app) const;
200
202 virtual std::string make_description(const App *app) const;
203
205 virtual std::string make_usage(const App *app, std::string name) const;
206
208 std::string make_help(const App *app, std::string, AppFormatMode mode) const override;
209
213
215 virtual std::string make_option(const Option *, bool) const;
216
218 virtual std::string make_option_name(const Option *, bool) const;
219
221 virtual std::string make_option_opts(const Option *) const;
222
224 virtual std::string make_option_desc(const Option *) const;
225
227 virtual std::string make_option_usage(const Option *opt) const;
228
230};
231
232// [CLI11:formatter_fwd_hpp:end]
233} // namespace CLI
#define CLI11_NODISCARD
Definition Macros.hpp:58
Creates a command line program, with very few defaults.
Definition App.hpp:98
void footer_paragraph_width(std::size_t val)
Set the footer paragraph width.
Definition FormatterFwd.hpp:108
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:102
std::map< std::string, std::string > labels_
The required help printout labels (user changeable) Values are Needs, Excludes, etc.
Definition FormatterFwd.hpp:68
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:125
CLI11_NODISCARD std::size_t get_footer_paragraph_width() const
Get the current footer paragraph width.
Definition FormatterFwd.hpp:134
void long_option_alignment_ratio(float ratio)
Definition FormatterFwd.hpp:99
CLI11_NODISCARD std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition FormatterFwd.hpp:118
FormatterBase(FormatterBase &&)=default
CLI11_NODISCARD std::size_t get_description_paragraph_width() const
Get the current description paragraph width at the top of help.
Definition FormatterFwd.hpp:131
FormatterBase & operator=(FormatterBase &&)=default
void enable_description_formatting(bool value=true)
enable formatting for description paragraph
Definition FormatterFwd.hpp:110
void label(std::string key, std::string val)
Set the "REQUIRED" or other labels.
Definition FormatterFwd.hpp:92
FormatterBase & operator=(const FormatterBase &)=default
bool enable_description_formatting_
options controlling formatting for footer and descriptions
Definition FormatterFwd.hpp:63
FormatterBase()=default
void enable_footer_formatting(bool value=true)
disable formatting for footer paragraph
Definition FormatterFwd.hpp:112
bool enable_footer_formatting_
Definition FormatterFwd.hpp:64
CLI11_NODISCARD bool is_description_paragraph_formatting_enabled() const
Get the current status of description paragraph formatting.
Definition FormatterFwd.hpp:137
FormatterBase(const FormatterBase &)=default
CLI11_NODISCARD std::size_t get_right_column_width() const
Get the current right column width (description of options/flags/subcommands)
Definition FormatterFwd.hpp:128
virtual ~FormatterBase() noexcept
Adding a destructor in this form to work around bug in GCC 4.7.
Definition FormatterFwd.hpp:82
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:95
std::size_t right_column_width_
The width of the right column (description of options/flags/subcommands)
Definition FormatterFwd.hpp:54
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:140
void description_paragraph_width(std::size_t val)
Set the description paragraph width at the top of help.
Definition FormatterFwd.hpp:105
std::string make_help(const App *app, std::string name, AppFormatMode mode) const override
This will simply call the lambda function.
Definition FormatterFwd.hpp:160
~FormatterLambda() noexcept override
Adding a destructor (mostly to make GCC 4.7 happy)
Definition FormatterFwd.hpp:157
FormatterLambda(funct_t funct)
Create a FormatterLambda with a lambda function.
Definition FormatterFwd.hpp:154
virtual std::string make_option_desc(const Option *) const
This is the description. Default: Right column, on new line if left column too large.
Formatter(Formatter &&)=default
virtual std::string make_positionals(const App *app) const
This prints out just the positionals "group".
std::string make_help(const App *app, std::string, AppFormatMode mode) const override
This puts everything together.
virtual std::string make_option_opts(const Option *) const
This is the options part of the name, Default: combined into left column.
virtual std::string make_option_usage(const Option *opt) const
This is used to print the name on the USAGE line.
Formatter(const Formatter &)=default
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const
This prints out all the subcommands.
virtual std::string make_description(const App *app) const
This displays the description line.
virtual std::string make_subcommand(const App *sub) const
This prints out a subcommand.
virtual std::string make_expanded(const App *sub, AppFormatMode mode) const
This prints out a subcommand in help-all.
Formatter()=default
Formatter & operator=(Formatter &&)=default
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
Formatter & operator=(const Formatter &)=default
virtual std::string make_option(const Option *, bool) const
This prints out an option help line, either positional or optional form.
std::string make_groups(const App *app, AppFormatMode mode) const
This prints out all the groups of options.
virtual CLI11_NODISCARD std::string make_group(std::string group, bool is_positional, std::vector< const Option * > opts) const
virtual std::string make_usage(const App *app, std::string name) const
This displays the usage line.
virtual std::string make_option_name(const Option *, bool) const
This is the name part of an option, Default: left column.
Definition Option.hpp:259
Definition App.hpp:36
AppFormatMode
Definition FormatterFwd.hpp:32
@ Normal
The normal, detailed help.
Definition FormatterFwd.hpp:33
@ All
A fully expanded help.
Definition FormatterFwd.hpp:34
@ Sub
Used when printed as part of expanded subcommand.
Definition FormatterFwd.hpp:35