forked from sailormoon/flags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags.h
292 lines (258 loc) · 9.41 KB
/
flags.h
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#ifndef FLAGS_H_
#define FLAGS_H_
#include <algorithm>
#include <array>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace flags {
namespace detail {
using argument_map =
std::unordered_map<std::string_view, std::vector<std::optional<std::string_view>>>;
// Non-destructively parses the argv tokens.
// * If the token begins with a -, it will be considered an option.
// * If the token does not begin with a -, it will be considered a value for the
// previous option. If there was no previous option, it will be considered a
// positional argument.
struct parser {
parser(const int argc, char** argv) {
for (int i = 1; i < argc; ++i) {
churn(argv[i]);
}
// If the last token was an option, it needs to be drained.
flush();
}
parser& operator=(const parser&) = delete;
const argument_map& options() const { return options_; }
const std::vector<std::string_view>& positional_arguments() const {
return positional_arguments_;
}
private:
// Advance the state machine for the current token.
void churn(const std::string_view& item) {
if(item.empty())
{
on_value(item);
return;
}
item.at(0) == '-' ? on_option(item) : on_value(item);
}
// Consumes the current option if there is one.
void flush() {
if (current_option_) on_value();
}
void on_option(const std::string_view& option) {
// Consume the current_option and reassign it to the new option while
// removing all leading dashes.
flush();
current_option_ = option;
current_option_->remove_prefix(current_option_->find_first_not_of('-'));
// Handle a packed argument (--arg_name=value).
if (const auto delimiter = current_option_->find_first_of('=');
delimiter != std::string_view::npos) {
auto value = *current_option_;
value.remove_prefix(delimiter + 1 /* skip '=' */);
current_option_->remove_suffix(current_option_->size() - delimiter);
on_value(value);
}
}
void on_value(const std::optional<std::string_view>& value = std::nullopt) {
// If there's not an option preceding the value, it's a positional argument.
if (!current_option_) {
if (value) positional_arguments_.emplace_back(*value);
return;
}
// Consume the preceding option and assign its value.
// operator[] will insert an empty vector if needed
options_[*current_option_].emplace_back(std::move(value));
current_option_.reset();
}
std::optional<std::string_view> current_option_;
argument_map options_;
std::vector<std::string_view> positional_arguments_;
};
// If a key exists, return an optional populated with its value.
inline std::optional<std::string_view> get_value(
const argument_map& options, const std::string_view& option) {
if (const auto it = options.find(option); it != options.end()) {
// If a key exists, there must be at least one value
return it->second[0];
}
return std::nullopt;
}
// If a key exists, return a vector with its values
inline std::vector<std::optional<std::string_view>> get_values(
const argument_map& options, const std::string_view& option) {
if (const auto it = options.find(option); it != options.end()) {
return it->second;
}
return {};
}
// Coerces the string value of the given option into <T>.
// If the value cannot be properly parsed or the key does not exist, returns
// nullopt.
template <class T>
std::optional<T> get(const argument_map& options,
const std::string_view& option) {
if (const auto view = get_value(options, option)) {
if (T value; std::istringstream(std::string(*view)) >> value) return value;
}
return std::nullopt;
}
// Since the values are already stored as strings, there's no need to use `>>`.
template <>
inline std::optional<std::string_view> get(const argument_map& options,
const std::string_view& option) {
return get_value(options, option);
}
template <>
inline std::optional<std::string> get(const argument_map& options,
const std::string_view& option) {
if (const auto view = get<std::string_view>(options, option)) {
return std::string(*view);
}
return std::nullopt;
}
// Special case for booleans: if the value is any of the below, the option will
// be considered falsy. Otherwise, it will be considered truthy just for being
// present.
constexpr std::array<const char*, 5> falsities{{"0", "n", "no", "f", "false"}};
template <>
inline std::optional<bool> get(const argument_map& options,
const std::string_view& option) {
if (const auto value = get_value(options, option)) {
return std::none_of(falsities.begin(), falsities.end(),
[&value](auto falsity) { return *value == falsity; });
}
if (options.find(option) != options.end()) return true;
return std::nullopt;
}
// Coerces the string values of the given option into std::vector<T>.
// If a value cannot be properly parsed it is not added. If there are
// no suitable values or the key does not exist, returns nullopt.
template <class T>
std::vector<std::optional<T>> get_multiple(const argument_map& options,
const std::string_view& option) {
std::vector<std::optional<T>> values;
const auto views = get_values(options, option);
for (const auto &view : views) {
if (!view) {
values.push_back(std::nullopt);
continue;
}
if (T value; std::istringstream(std::string(*view)) >> value) {
values.push_back(value);
} else {
values.push_back(std::nullopt);
}
}
return values;
}
// Since the values are already stored as strings, there's no need to use `>>`.
template <>
inline std::vector<std::optional<std::string_view>> get_multiple(
const argument_map& options, const std::string_view& option) {
return get_values(options, option);
}
template <>
inline std::vector<std::optional<std::string>> get_multiple(
const argument_map& options, const std::string_view& option) {
const auto views = get_values(options, option);
std::vector<std::optional<std::string>> values(views.begin(), views.end());
return values;
}
// Special case for booleans: if the value is in the falsities array (see get<bool>)
// the option will be considered falsy. Otherwise, it will be considered truthy just
// for being present.
template <>
inline std::vector<std::optional<bool>> get_multiple(
const argument_map& options, const std::string_view& option) {
const auto views = get_values(options, option);
std::vector<std::optional<bool>> values;
for (const auto view : views) {
if (!view) {
values.push_back(true);
continue;
}
values.push_back(std::none_of(falsities.begin(), falsities.end(),
[&view](auto falsity) { return view == falsity; }));
}
return values;
}
// Coerces the string value of the given positional index into <T>.
// If the value cannot be properly parsed or the key does not exist, returns
// nullopt.
template <class T>
std::optional<T> get(const std::vector<std::string_view>& positional_arguments,
size_t positional_index) {
if (positional_index < positional_arguments.size()) {
if (T value; std::istringstream(
std::string(positional_arguments[positional_index])) >>
value)
return value;
}
return std::nullopt;
}
// Since the values are already stored as strings, there's no need to use `>>`.
template <>
inline std::optional<std::string_view> get(
const std::vector<std::string_view>& positional_arguments,
size_t positional_index) {
if (positional_index < positional_arguments.size()) {
return positional_arguments[positional_index];
}
return std::nullopt;
}
template <>
inline std::optional<std::string> get(
const std::vector<std::string_view>& positional_arguments,
size_t positional_index) {
if (positional_index < positional_arguments.size()) {
return std::string(positional_arguments[positional_index]);
}
return std::nullopt;
}
} // namespace detail
struct args {
args(const int argc, char** argv) : parser_(argc, argv) {}
template <class T>
std::optional<T> get(const std::string_view& option) const {
return detail::get<T>(parser_.options(), option);
}
template <class T>
T get(const std::string_view& option, T&& default_value) const {
return get<T>(option).value_or(default_value);
}
template <class T>
std::vector<std::optional<T>> get_multiple(const std::string_view& option) const {
return detail::get_multiple<T>(parser_.options(), option);
}
template <class T>
std::vector<T> get_multiple(const std::string_view& option, T&& default_value) const {
const auto items = get_multiple<T>(option);
std::vector<T> values;
values.reserve(items.size());
for(const auto& item : items) {
values.push_back(item ? *item : default_value);
}
return values;
}
template <class T>
std::optional<T> get(size_t positional_index) const {
return detail::get<T>(parser_.positional_arguments(), positional_index);
}
template <class T>
T get(size_t positional_index, T&& default_value) const {
return get<T>(positional_index).value_or(default_value);
}
const std::vector<std::string_view>& positional() const {
return parser_.positional_arguments();
}
private:
const detail::parser parser_;
};
} // namespace flags
#endif // FLAGS_H_