CLI11 2.4.0
Loading...
Searching...
No Matches
FormatterFwd.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2024, 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
19namespace CLI {
20// [CLI11:formatter_fwd_hpp:verbatim]
21
22class Option;
23class App;
24
29
30enum 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;
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
97class 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
118class Formatter : public FormatterBase {
119 public:
120 Formatter() = default;
121 Formatter(const Formatter &) = default;
122 Formatter(Formatter &&) = default;
123 Formatter &operator=(const 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
#define CLI11_NODISCARD
Definition Macros.hpp:47
Creates a command line program, with very few defaults.
Definition App.hpp:88
Definition FormatterFwd.hpp:40
std::size_t column_width_
The width of the first column.
Definition FormatterFwd.hpp:46
std::map< std::string, std::string > labels_
The required help printout labels (user changeable) Values are Needs, Excludes, etc.
Definition FormatterFwd.hpp:50
CLI11_NODISCARD std::size_t get_column_width() const
Get the current column width.
Definition FormatterFwd.hpp:91
CLI11_NODISCARD std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition FormatterFwd.hpp:84
FormatterBase(FormatterBase &&)=default
FormatterBase & operator=(FormatterBase &&)=default
void label(std::string key, std::string val)
Set the "REQUIRED" label.
Definition FormatterFwd.hpp:74
FormatterBase & operator=(const FormatterBase &)=default
FormatterBase()=default
FormatterBase(const FormatterBase &)=default
virtual ~FormatterBase() noexcept
Adding a destructor in this form to work around bug in GCC 4.7.
Definition FormatterFwd.hpp:64
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 column width.
Definition FormatterFwd.hpp:77
This is a specialty override for lambda functions.
Definition FormatterFwd.hpp:97
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
~FormatterLambda() noexcept override
Adding a destructor (mostly to make GCC 4.7 happy)
Definition FormatterFwd.hpp:108
FormatterLambda(funct_t funct)
Create a FormatterLambda with a lambda function.
Definition FormatterFwd.hpp:105
Definition FormatterFwd.hpp:118
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_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 std::string make_positionals(const App *app) const
This prints out just the positionals "group".
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.
virtual std::string make_expanded(const App *sub) const
This prints out a subcommand in help-all.
Formatter(const Formatter &)=default
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const
This prints out all the subcommands.
std::string make_help(const App *, std::string, AppFormatMode) const override
This puts everything together.
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.
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
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:229
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.
Definition App.hpp:34
AppFormatMode
Definition FormatterFwd.hpp:30
@ Normal
The normal, detailed help.
@ All
A fully expanded help.
@ Sub
Used when printed as part of expanded subcommand.