cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
click.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <stdexcept>
6#include <string>
7
8#include <cpp-toolbox/cpp-toolbox_export.hpp>
9
11{
12
13class ini_config_t;
14
15class CPP_TOOLBOX_EXPORT parameter_t
16{
17protected:
18 std::string name_;
19 std::string description_;
21 bool is_set_;
22
23public:
24 parameter_t(const std::string& name,
25 const std::string& description,
26 bool required = false);
27 virtual ~parameter_t() = default;
28
29 [[nodiscard]] std::string get_name() const;
30 [[nodiscard]] std::string get_description() const;
31 [[nodiscard]] bool is_required() const;
32 [[nodiscard]] bool is_set() const;
33
34 virtual bool parse(const std::string& value) = 0;
35 virtual bool accepts_missing_value() const;
36 virtual bool is_option() const { return false; }
37 virtual bool is_argument() const { return false; }
38 virtual bool is_flag() const { return false; }
39 virtual std::string get_short_name() const;
40};
41
42template<typename T>
43class CPP_TOOLBOX_EXPORT option_t : public parameter_t
44{
45private:
46 T value_;
47 T default_value_;
48 std::string short_name_;
49 std::function<bool(const std::string&, T&)> parser_;
50
51public:
52 option_t(const std::string& name,
53 const std::string& short_name,
54 const std::string& description,
55 bool required = false);
56
57 option_t& set_default(const T& default_value);
58 option_t& set_parser(std::function<bool(const std::string&, T&)> parser);
59 [[nodiscard]] std::string get_short_name() const override;
60 T get() const;
61 bool parse(const std::string& value) override;
62 bool accepts_missing_value() const override;
63 bool is_option() const override;
64
65private:
66 void set_default_parser(); // Type-specific default parsers
67};
68
69class CPP_TOOLBOX_EXPORT flag_t : public option_t<bool>
70{
71public:
72 flag_t(const std::string& name,
73 const std::string& short_name,
74 const std::string& description);
75
76 bool parse(const std::string& value) override;
77 bool is_option() const override { return true; }
78 bool is_flag() const override { return true; }
79 std::string get_short_name() const override;
80 bool get() const;
81};
82
83template<typename T>
84class CPP_TOOLBOX_EXPORT argument_t : public parameter_t
85{
86private:
87 T value_;
88 T default_value_;
89 std::function<bool(const std::string&, T&)> parser_;
90
91public:
92 argument_t(const std::string& name,
93 const std::string& description,
94 bool required = true);
95
96 argument_t& set_default(const T& default_value);
97 T get() const;
98 bool parse(const std::string& value) override;
99 bool accepts_missing_value() const override;
100 bool is_argument() const override;
101 std::string get_short_name() const override;
102
103private:
104 void set_default_parser();
105};
106
107class CPP_TOOLBOX_EXPORT command_t
108{
109private:
110 std::string name_;
111 std::string description_;
112 std::vector<std::unique_ptr<parameter_t>> parameters_;
113 std::vector<std::unique_ptr<command_t>> subcommands_;
114 std::function<int()> callback_;
115 bool help_flag_added_ = false;
116 bool help_requested_ = false;
117
118public:
119 command_t(const std::string& name, const std::string& description);
120
121 // 显式删除拷贝操作,允许移动操作
122 command_t(const command_t&) = delete;
123 command_t& operator=(const command_t&) = delete;
124 command_t(command_t&&) = default;
126
127 std::string get_name() const;
128 std::string get_description() const;
129
130 template<typename T>
131 option_t<T>& add_option(const std::string& name,
132 const std::string& short_name,
133 const std::string& description,
134 bool required = false);
135
136 flag_t& add_flag(const std::string& name,
137 const std::string& short_name,
138 const std::string& description);
139
140 template<typename T>
141 argument_t<T>& add_argument(const std::string& name,
142 const std::string& description,
143 bool required = true);
144
145 command_t& add_command(const std::string& name,
146 const std::string& description);
147 command_t& set_callback(std::function<int()> callback);
148
149 int parse_and_execute(const std::vector<std::string>& args);
150 std::string format_help() const;
151
152 void apply_ini_config(const class ini_config_t& config,
153 const std::string& section = "");
154 void apply_ini_file(const std::string& file_path,
155 const std::string& section = "");
156
157private:
158 void add_help();
159 [[nodiscard]] bool handle_help_flag() const;
160 [[nodiscard]] std::string format_command_list() const;
161};
162
163class CPP_TOOLBOX_EXPORT CommandLineApp : public command_t
164{
165public:
166 CommandLineApp(const std::string& name, const std::string& description);
167
168 int run(int argc, char** argv);
169
170private:
171 void handle_exceptions(const std::exception& e) const;
172};
173
174class CPP_TOOLBOX_EXPORT ClickException : public std::runtime_error
175{
176public:
177 ClickException(const std::string& message);
178 virtual void print() const;
179};
180
181class CPP_TOOLBOX_EXPORT ParameterError : public ClickException
182{
183public:
184 ParameterError(const std::string& message);
185};
186
187class CPP_TOOLBOX_EXPORT UsageError : public ClickException
188{
189public:
190 UsageError(const std::string& message);
191};
192
193} // namespace toolbox::utils
194
Definition click.hpp:175
ClickException(const std::string &message)
virtual void print() const
Definition click.hpp:164
CommandLineApp(const std::string &name, const std::string &description)
int run(int argc, char **argv)
Definition click.hpp:182
ParameterError(const std::string &message)
Definition click.hpp:188
UsageError(const std::string &message)
Definition click.hpp:85
Definition click.hpp:108
command_t(command_t &&)=default
std::string get_name() const
command_t(const std::string &name, const std::string &description)
command_t & set_callback(std::function< int()> callback)
command_t & operator=(command_t &&)=default
std::string format_help() const
flag_t & add_flag(const std::string &name, const std::string &short_name, const std::string &description)
command_t & operator=(const command_t &)=delete
command_t & add_command(const std::string &name, const std::string &description)
std::string get_description() const
command_t(const command_t &)=delete
int parse_and_execute(const std::vector< std::string > &args)
Definition click.hpp:70
std::string get_short_name() const override
bool is_option() const override
Definition click.hpp:77
bool parse(const std::string &value) override
bool is_flag() const override
Definition click.hpp:78
flag_t(const std::string &name, const std::string &short_name, const std::string &description)
Definition ini_config.hpp:11
Definition click.hpp:44
Definition click.hpp:16
virtual std::string get_short_name() const
parameter_t(const std::string &name, const std::string &description, bool required=false)
std::string name_
Definition click.hpp:18
virtual bool is_argument() const
Definition click.hpp:37
virtual ~parameter_t()=default
std::string description_
Definition click.hpp:19
virtual bool parse(const std::string &value)=0
virtual bool is_option() const
Definition click.hpp:36
virtual bool is_flag() const
Definition click.hpp:38
bool is_set_
Definition click.hpp:21
virtual bool accepts_missing_value() const
std::string get_name() const
std::string get_description() const
bool required_
Definition click.hpp:20
< 用于 std::out_of_range/For std::out_of_range
Definition click.hpp:11