Option.hpp
7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#pragma once
// Distributed under the LGPL version 3.0 license. See accompanying
// file LICENSE or https://github.com/henryiii/CLI11 for details.
#include <string>
#include <functional>
#include <vector>
#include <tuple>
#include <algorithm>
#include "CLI/Error.hpp"
#include "CLI/StringTools.hpp"
#include "CLI/Split.hpp"
namespace CLI {
typedef std::vector<std::vector<std::string>> results_t;
typedef std::function<bool(results_t)> callback_t;
class App;
class Option {
friend App;
protected:
// Config
std::vector<std::string> snames;
std::vector<std::string> lnames;
std::string pname;
std::string description;
callback_t callback;
// These are for help strings
std::string defaultval;
std::string typeval;
std::string _group {"Options"};
bool _default {false};
bool _required {false};
int _expected {1};
bool allow_vector {false};
std::vector<std::function<bool(std::string)>> _validators;
// Results
results_t results;
public:
Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool _default=true) :
description(description), callback(callback), _default(_default) {
std::tie(snames, lnames, pname) = detail::get_names(detail::split_names(name));
}
// This class is "true" if optio passed.
operator bool() const {
return results.size() > 0;
}
/// Clear the parsed results (mostly for testing)
void clear() {
results.clear();
}
/// Set the option as required
Option* required(bool value = true) {
_required = value;
return this;
}
bool get_required() const {
return _required;
}
/// Set the number of expected arguments (Flags bypass this)
Option* expected(int value) {
if(value == 0)
throw IncorrectConstruction("Cannot set 0 expected, use a flag instead");
if(!allow_vector && value != 1)
throw IncorrectConstruction("You can only change the Expected arguments for vectors");
_expected = value;
return this;
}
/// The number of arguments the option expects
int get_expected() const {
return _expected;
}
/// True if this has a default value
int get_default() const {
return _default;
}
/// True if the argument can be given directly
bool get_positional() const {
return pname.length() > 0;
}
/// True if option has at least one non-positional name
bool nonpositional() const {
return (snames.size() + lnames.size()) > 0;
}
/// True if option has description
bool has_description() const {
return description.length() > 0;
}
/// Adds a validator
Option* check(std::function<bool(std::string)> validator) {
_validators.push_back(validator);
return this;
}
Option* group(std::string name) {
_group = name;
return this;
}
const std::string& get_group() const {
return _group;
}
/// Get the description
const std::string& get_description() const {
return description;
}
/// The name and any extras needed for positionals
std::string help_positional() const {
std::string out = pname;
if(get_expected()<1)
out = out + "x" + std::to_string(get_expected());
else if(get_expected()==-1)
out = out + "...";
out = get_required() ? out : "["+out+"]";
return out;
}
// Just the pname
std::string get_pname() const {
return pname;
}
/// Process the callback
bool run_callback() const {
if(_validators.size()>0) {
for(const std::string & result : flatten_results())
for(const std::function<bool(std::string)> &vali : _validators)
if(!vali(result))
return false;
}
return callback(results);
}
/// If options share any of the same names, they are equal (not counting positional)
bool operator== (const Option& other) const {
for(const std::string &sname : snames)
for(const std::string &othersname : other.snames)
if(sname == othersname)
return true;
for(const std::string &lname : lnames)
for(const std::string &otherlname : other.lnames)
if(lname == otherlname)
return true;
return false;
}
/// Gets a , sep list of names. Does not include the positional name if opt_only=true.
std::string get_name(bool opt_only=false) const {
std::vector<std::string> name_list;
if(!opt_only && pname.length() > 0)
name_list.push_back(pname);
for(const std::string& sname : snames)
name_list.push_back("-"+sname);
for(const std::string& lname : lnames)
name_list.push_back("--"+lname);
return detail::join(name_list);
}
/// Check a name. Requires "-" or "--" for short / long, supports positional name
bool check_name(std::string name) const {
if(name.length()>2 && name.substr(0,2) == "--")
return check_lname(name.substr(2));
else if (name.length()>1 && name.substr(0,1) == "-")
return check_sname(name.substr(1));
else
return name == pname;
}
/// Requires "-" to be removed from string
bool check_sname(const std::string& name) const {
return std::find(std::begin(snames), std::end(snames), name) != std::end(snames);
}
/// Requires "--" to be removed from string
bool check_lname(const std::string& name) const {
return std::find(std::begin(lnames), std::end(lnames), name) != std::end(lnames);
}
/// Puts a result at position r
void add_result(int r, std::string s) {
results.at(r).push_back(s);
}
/// Starts a new results vector (used for r in add_result)
int get_new() {
results.emplace_back();
return results.size() - 1;
}
/// Count the total number of times an option was passed
int count() const {
int out = 0;
for(const std::vector<std::string>& vec : results)
out += vec.size();
return out;
}
/// The first half of the help print, name plus default, etc
std::string help_name() const {
std::stringstream out;
out << get_name(true);
if(get_expected() != 0) {
if(typeval != "")
out << " " << typeval;
if(defaultval != "")
out << "=" << defaultval;
if(get_expected() > 1)
out << " x " << get_expected();
if(get_expected() == -1)
out << " ...";
}
return out.str();
}
/// pname with type info
std::string help_pname() const {
std::stringstream out;
out << get_pname();
if(typeval != "")
out << " " << typeval;
if(defaultval != "")
out << "=" << defaultval;
if(get_expected() > 1)
out << " x " << get_expected();
if(get_expected() == -1)
out << " ...";
return out.str();
}
/// Produce a flattened vector of results, vs. a vector of vectors.
std::vector<std::string> flatten_results() const {
std::vector<std::string> output;
for(const std::vector<std::string> result : results)
output.insert(std::end(output), std::begin(result), std::end(result));
return output;
}
};
}