-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgsParser.hpp
304 lines (298 loc) · 9.38 KB
/
ArgsParser.hpp
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
293
294
295
296
297
298
299
300
301
302
303
304
#pragma once
#include <cassert>
#include <cstring>
#include <span>
#include <string_view>
#include <utility>
#if defined(__EXCEPTIONS) || \
defined(_MSC_VER) && !defined(ARGS_PARSER_NO_EXCEPTIONS)
#include <stdexcept>
#define ARGS_PARSER_THROW(...) throw __VA_ARGS__;
#else
#include <cstdlib>
#define ARGS_PARSER_THROW(...) std::abort();
#endif
namespace ArgsParser {
namespace details {
inline void check_keys(std::span<std::string_view> keys) {
for (auto key : keys)
assert(key.starts_with('-') && "key is not starts with '-'.");
}
inline void check_arg(std::span<std::string_view> keys, const char *arg) {
for (auto key : keys)
if (strlen(arg) >= key.size() && key == std::string_view{arg, key.size()})
return;
assert(false && "arg mismatch.");
}
} // namespace details
struct OptionRange {
int begin = -1, end = -1;
operator bool() const { return begin < end; }
};
struct FlagOption {
std::span<std::string_view> keys;
bool val;
OptionRange range;
int parse(int argc, char **argv, int index) {
details::check_arg(keys, argv[index]);
val = true;
range = {index, index + 1};
return index + 1;
}
bool is(int argc, char **argv, int index) const {
for (auto key : keys)
if (argv[index] == key)
return true;
return false;
}
FlagOption(std::span<std::string_view> keys, bool def = false)
: keys(keys), val(def) {
details::check_keys(keys);
}
};
struct SimpleOption {
std::span<std::string_view> keys;
std::string_view val;
OptionRange range;
int parse(int argc, char **argv, int index) {
details::check_arg(keys, argv[index]);
for (auto key : keys) {
if (argv[index] == key) {
if (index + 1 < argc)
val = argv[index + 1];
else
ARGS_PARSER_THROW(std::length_error("value is not set."))
range = {index, index + 2};
return index + 2;
} else if (strlen(argv[index]) > key.size() &&
std::string_view{argv[index], key.size()} == key &&
argv[index][key.size()] == '=') {
val = argv[index];
if (auto pos = val.find('='); pos + 1 < val.size()) {
val = val.substr(pos + 1);
range = {index, index + 1};
return index + 1;
} else {
ARGS_PARSER_THROW(std::length_error("value is not set."))
}
}
}
assert(false && "key is not found.");
std::unreachable();
}
bool is(int argc, char **argv, int index) const {
for (auto key : keys)
if (argv[index] == key ||
strlen(argv[index]) >= key.size() &&
std::string_view{argv[index], key.size()} == key &&
argv[index][key.size()] == '=') {
return true;
}
return false;
}
SimpleOption(std::span<std::string_view> keys, std::string_view def = "")
: keys(keys), val(def) {
details::check_keys(keys);
}
};
struct ComplexOption {
std::span<std::string_view> keys;
std::span<FlagOption *> flags;
std::span<SimpleOption *> simples;
std::span<ComplexOption *> complexs;
OptionRange range;
int parse(int argc, char **argv, int index) {
details::check_arg(keys, argv[index]);
range.begin = index;
index++;
int current_flag_index = 0;
int current_simple_index = 0;
int current_complex_index = 0;
while (index < argc) {
bool continue_flag = false;
for (auto i = current_complex_index; i < complexs.size(); i++) {
if (complexs[i]->is(argc, argv, index)) {
index = complexs[i]->parse(argc, argv, index);
if (current_complex_index != i)
std::swap(complexs[current_complex_index], complexs[i]);
current_complex_index++;
continue_flag = true;
break;
}
}
if (continue_flag)
continue;
for (auto i = current_simple_index; i < simples.size(); i++) {
if (simples[i]->is(argc, argv, index)) {
index = simples[i]->parse(argc, argv, index);
if (current_simple_index != i)
std::swap(simples[current_simple_index], simples[i]);
current_simple_index++;
continue_flag = true;
break;
}
}
if (continue_flag)
continue;
for (auto i = current_flag_index; i < flags.size(); i++) {
if (flags[i]->is(argc, argv, index)) {
index = flags[i]->parse(argc, argv, index);
if (current_flag_index != i)
std::swap(flags[current_flag_index], flags[i]);
current_flag_index++;
continue_flag = true;
break;
}
}
if (continue_flag)
continue;
break;
}
range.end = index;
return index;
}
bool is(int argc, char **argv, int index) const {
for (auto key : keys)
if (argv[index] == key)
return true;
return false;
}
ComplexOption(std::span<std::string_view> keys,
std::span<FlagOption *> flags = {},
std::span<SimpleOption *> simples = {},
std::span<ComplexOption *> complexs = {})
: keys(keys), flags(flags), simples(simples), complexs(complexs) {
details::check_keys(keys);
}
};
struct UnmatchedOption {
std::string_view val;
OptionRange range;
int parse(int argc, char **argv, int index) {
val = argv[index];
range = {index, index + 1};
return index + 1;
}
bool is(int argc, char **argv, int index) const { return true; }
};
struct Parser {
std::span<FlagOption *> flags;
std::span<SimpleOption *> simples;
std::span<ComplexOption *> complexs;
int unmatched_option_count = -1;
int parse(int argc, char **argv) {
int index = 1;
int current_flag_index = 0;
int current_simple_index = 0;
int current_complex_index = 0;
int unmatched_option_count = 1;
while (index < argc) {
bool continue_flag = false;
for (auto i = current_complex_index; i < complexs.size(); i++) {
if (complexs[i]->is(argc, argv, index)) {
index = complexs[i]->parse(argc, argv, index);
if (current_complex_index != i)
std::swap(complexs[current_complex_index], complexs[i]);
current_complex_index++;
continue_flag = true;
break;
}
}
if (continue_flag)
continue;
for (auto i = current_simple_index; i < simples.size(); i++) {
if (simples[i]->is(argc, argv, index)) {
index = simples[i]->parse(argc, argv, index);
if (current_simple_index != i)
std::swap(simples[current_simple_index], simples[i]);
current_simple_index++;
continue_flag = true;
break;
}
}
if (continue_flag)
continue;
for (auto i = current_flag_index; i < flags.size(); i++) {
if (flags[i]->is(argc, argv, index)) {
index = flags[i]->parse(argc, argv, index);
if (current_flag_index != i)
std::swap(flags[current_flag_index], flags[i]);
current_flag_index++;
continue_flag = true;
break;
}
}
if (continue_flag)
continue;
unmatched_option_count++;
index++;
}
this->unmatched_option_count = unmatched_option_count;
return unmatched_option_count;
}
struct UnmatchedContext {
int current_flag_index = 0;
int current_simple_index = 0;
int current_complex_index = 0;
};
bool is_unmatched(int argc, char **argv, int index,
UnmatchedContext &context) {
auto &[flag, simple, complex] = context;
while (flag < flags.size() && flags[flag]->range.end <= index) {
flag++;
}
while (simple < simples.size() && simples[simple]->range.end <= index) {
simple++;
}
while (complex < complexs.size() && complexs[complex]->range.end <= index) {
complex++;
}
if (flag < flags.size() && flags[flag]->range.begin <= index &&
index < flags[flag]->range.end)
return false;
if (simple < simples.size() && simples[simple]->range.begin <= index &&
index < simples[simple]->range.end)
return false;
if (complex < complexs.size() && complexs[complex]->range.begin <= index &&
index < complexs[complex]->range.end)
return false;
return true;
}
void get_all_unmatched(int argc, char **argv,
std::span<UnmatchedOption *> unmatched_options) {
if (unmatched_option_count == -1)
parse(argc, argv);
if (unmatched_options.size() < unmatched_option_count)
ARGS_PARSER_THROW(
std::length_error("unmatched_options's length is too short."));
int index = 0;
int no_key_index = 0;
UnmatchedContext context;
while (index < argc) {
if (is_unmatched(argc, argv, index, context)) {
index = unmatched_options[no_key_index++]->parse(argc, argv, index);
} else
index++;
}
}
void enum_unmatched(int argc, char **argv, auto callable) {
UnmatchedContext context;
int index = 0;
while (index < argc) {
if (is_unmatched(argc, argv, index, context)) {
UnmatchedOption o;
o.parse(argc, argv, index);
if constexpr (requires(decltype(callable) c, UnmatchedOption *o) {
bool{c(o)};
}) {
if (!callable(static_cast<const UnmatchedOption *>(&o)))
break;
} else {
callable(static_cast<const UnmatchedOption *>(&o));
}
}
index++;
}
}
};
} // namespace ArgsParser