File indexing completed on 2025-12-16 10:11:57
0001
0002
0003
0004
0005
0006
0007 #pragma once
0008
0009
0010
0011
0012 #include <functional>
0013 #include <map>
0014 #include <string>
0015 #include <utility>
0016 #include <vector>
0017
0018
0019 #include "StringTools.hpp"
0020
0021 namespace CLI {
0022
0023
0024 class Option;
0025 class App;
0026
0027
0028
0029
0030
0031
0032 enum class AppFormatMode {
0033 Normal,
0034 All,
0035 Sub,
0036 };
0037
0038
0039
0040
0041
0042 class FormatterBase {
0043 protected:
0044
0045
0046
0047
0048 std::size_t column_width_{30};
0049
0050
0051 std::size_t right_column_width_{65};
0052
0053
0054 std::size_t description_paragraph_width_{80};
0055
0056
0057 std::size_t footer_paragraph_width_{80};
0058
0059
0060
0061 std::map<std::string, std::string> labels_{};
0062
0063
0064
0065
0066
0067 public:
0068 FormatterBase() = default;
0069 FormatterBase(const FormatterBase &) = default;
0070 FormatterBase(FormatterBase &&) = default;
0071 FormatterBase &operator=(const FormatterBase &) = default;
0072 FormatterBase &operator=(FormatterBase &&) = default;
0073
0074
0075 virtual ~FormatterBase() noexcept {}
0076
0077
0078 virtual std::string make_help(const App *, std::string, AppFormatMode) const = 0;
0079
0080
0081
0082
0083
0084
0085 void label(std::string key, std::string val) { labels_[key] = val; }
0086
0087
0088 void column_width(std::size_t val) { column_width_ = val; }
0089
0090
0091 void right_column_width(std::size_t val) { right_column_width_ = val; }
0092
0093
0094 void description_paragraph_width(std::size_t val) { description_paragraph_width_ = val; }
0095
0096
0097 void footer_paragraph_width(std::size_t val) { footer_paragraph_width_ = val; }
0098
0099
0100
0101
0102
0103
0104 CLI11_NODISCARD std::string get_label(std::string key) const {
0105 if(labels_.find(key) == labels_.end())
0106 return key;
0107 return labels_.at(key);
0108 }
0109
0110
0111 CLI11_NODISCARD std::size_t get_column_width() const { return column_width_; }
0112
0113
0114 CLI11_NODISCARD std::size_t get_right_column_width() const { return right_column_width_; }
0115
0116
0117 CLI11_NODISCARD std::size_t get_description_paragraph_width() const { return description_paragraph_width_; }
0118
0119
0120 CLI11_NODISCARD std::size_t get_footer_paragraph_width() const { return footer_paragraph_width_; }
0121
0122
0123 };
0124
0125
0126 class FormatterLambda final : public FormatterBase {
0127 using funct_t = std::function<std::string(const App *, std::string, AppFormatMode)>;
0128
0129
0130 funct_t lambda_;
0131
0132 public:
0133
0134 explicit FormatterLambda(funct_t funct) : lambda_(std::move(funct)) {}
0135
0136
0137 ~FormatterLambda() noexcept override {}
0138
0139
0140 std::string make_help(const App *app, std::string name, AppFormatMode mode) const override {
0141 return lambda_(app, name, mode);
0142 }
0143 };
0144
0145
0146
0147 class Formatter : public FormatterBase {
0148 public:
0149 Formatter() = default;
0150 Formatter(const Formatter &) = default;
0151 Formatter(Formatter &&) = default;
0152 Formatter &operator=(const Formatter &) = default;
0153 Formatter &operator=(Formatter &&) = default;
0154
0155
0156
0157
0158
0159
0160 CLI11_NODISCARD virtual std::string
0161 make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
0162
0163
0164 virtual std::string make_positionals(const App *app) const;
0165
0166
0167 std::string make_groups(const App *app, AppFormatMode mode) const;
0168
0169
0170 virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
0171
0172
0173 virtual std::string make_subcommand(const App *sub) const;
0174
0175
0176 virtual std::string make_expanded(const App *sub, AppFormatMode mode) const;
0177
0178
0179 virtual std::string make_footer(const App *app) const;
0180
0181
0182 virtual std::string make_description(const App *app) const;
0183
0184
0185 virtual std::string make_usage(const App *app, std::string name) const;
0186
0187
0188 std::string make_help(const App *app, std::string, AppFormatMode mode) const override;
0189
0190
0191
0192
0193
0194
0195 virtual std::string make_option(const Option *, bool) const;
0196
0197
0198 virtual std::string make_option_name(const Option *, bool) const;
0199
0200
0201 virtual std::string make_option_opts(const Option *) const;
0202
0203
0204 virtual std::string make_option_desc(const Option *) const;
0205
0206
0207 virtual std::string make_option_usage(const Option *opt) const;
0208
0209
0210 };
0211
0212
0213 }