CLI11 2.3.0
Loading...
Searching...
No Matches
FormatterFwd.hpp
Go to the documentation of this file.
1// Copyright (c) 2017-2022, 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;
60
62 virtual ~FormatterBase() noexcept {} // NOLINT(modernize-use-equals-default)
63
65 virtual std::string make_help(const App *, std::string, AppFormatMode) const = 0;
66
70
72 void label(std::string key, std::string val) { labels_[key] = val; }
73
75 void column_width(std::size_t val) { column_width_ = val; }
76
80
82 CLI11_NODISCARD std::string get_label(std::string key) const {
83 if(labels_.find(key) == labels_.end())
84 return key;
85 return labels_.at(key);
86 }
87
89 CLI11_NODISCARD std::size_t get_column_width() const { return column_width_; }
90
92};
93
95class FormatterLambda final : public FormatterBase {
96 using funct_t = std::function<std::string(const App *, std::string, AppFormatMode)>;
97
99 funct_t lambda_;
100
101 public:
103 explicit FormatterLambda(funct_t funct) : lambda_(std::move(funct)) {}
104
106 ~FormatterLambda() noexcept override {} // NOLINT(modernize-use-equals-default)
107
109 std::string make_help(const App *app, std::string name, AppFormatMode mode) const override {
110 return lambda_(app, name, mode);
111 }
112};
113
116class Formatter : public FormatterBase {
117 public:
118 Formatter() = default;
119 Formatter(const Formatter &) = default;
120 Formatter(Formatter &&) = default;
121
124
127 CLI11_NODISCARD virtual std::string
128 make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
129
131 virtual std::string make_positionals(const App *app) const;
132
134 std::string make_groups(const App *app, AppFormatMode mode) const;
135
137 virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
138
140 virtual std::string make_subcommand(const App *sub) const;
141
143 virtual std::string make_expanded(const App *sub) const;
144
146 virtual std::string make_footer(const App *app) const;
147
149 virtual std::string make_description(const App *app) const;
150
152 virtual std::string make_usage(const App *app, std::string name) const;
153
155 std::string make_help(const App * /*app*/, std::string, AppFormatMode) const override;
156
160
162 virtual std::string make_option(const Option *opt, bool is_positional) const {
163 std::stringstream out;
165 out, make_option_name(opt, is_positional) + make_option_opts(opt), make_option_desc(opt), column_width_);
166 return out.str();
167 }
168
170 virtual std::string make_option_name(const Option *, bool) const;
171
173 virtual std::string make_option_opts(const Option *) const;
174
176 virtual std::string make_option_desc(const Option *) const;
177
179 virtual std::string make_option_usage(const Option *opt) const;
180
182};
183
184// [CLI11:formatter_fwd_hpp:end]
185} // namespace CLI
#define CLI11_NODISCARD
Definition: Macros.hpp:47
Creates a command line program, with very few defaults.
Definition: App.hpp:85
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:89
CLI11_NODISCARD std::string get_label(std::string key) const
Get the current value of a name (REQUIRED, etc.)
Definition: FormatterFwd.hpp:82
FormatterBase(FormatterBase &&)=default
void label(std::string key, std::string val)
Set the "REQUIRED" label.
Definition: FormatterFwd.hpp:72
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:62
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:75
This is a specialty override for lambda functions.
Definition: FormatterFwd.hpp:95
std::string make_help(const App *app, std::string name, AppFormatMode mode) const override
This will simply call the lambda function.
Definition: FormatterFwd.hpp:109
~FormatterLambda() noexcept override
Adding a destructor (mostly to make GCC 4.7 happy)
Definition: FormatterFwd.hpp:106
FormatterLambda(funct_t funct)
Create a FormatterLambda with a lambda function.
Definition: FormatterFwd.hpp:103
Definition: FormatterFwd.hpp:116
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:162
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
virtual std::string make_footer(const App *app) const
This prints out all the groups of options.
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:228
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.