-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOptions.h
More file actions
302 lines (277 loc) · 9.16 KB
/
Options.h
File metadata and controls
302 lines (277 loc) · 9.16 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
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
/*
* Copyright (C) 2021 - 2022 absop (zlang0@163.com)
*/
#include <initializer_list>
#include <iostream>
#include <map>
#include <string>
#include <vector>
struct Option {
bool is_given;
unsigned int num_args;
#ifdef TEST_OPTION_PARSER
std::string pattern;
#endif
std::vector<std::string> values;
};
class Options {
public:
Options(const std::string &lines)
: Options(split(lines, "\n"))
{}
Options(const std::initializer_list<std::string> &lines)
: Options(std::vector<std::string>(lines))
{}
Options(const std::vector<std::string> &lines) {
std::vector<size_t> usage_line_indexes;
std::vector<std::string> option_explains;
std::string::size_type max_pattern_length = 0;
for (auto &line : lines) {
if (line.substr(0, 3) != " -") {
_usage_lines.push_back(line);
continue;
}
if (!(line.length() > 3)) {
continue;
}
size_t option_index = _options.size();
std::string option_pattern;
std::string::size_type i = 3;
for (auto & name : parse_names(line, i)) {
_option_index_by_names[name] = option_index;
if (!option_pattern.empty()) {
option_pattern.append(", ");
} else {
option_pattern.append(" ");
}
option_pattern.push_back('-');
option_pattern.append(name);
}
Option option = {
#ifdef TEST_OPTION_PARSER
.pattern = option_pattern.substr(2)
#endif
};
for (auto & ph : parse_placeholders(line, i)) {
option_pattern.push_back(' ');
option_pattern.append(ph);
option.num_args += 1;
}
if (option.num_args &&
option_pattern.length() > 3 &&
option_pattern.substr(option_pattern.length() - 3) == "...") {
option.num_args = -1;
}
usage_line_indexes.push_back(_usage_lines.size());
_usage_lines.push_back(option_pattern);
_options.push_back(option);
option_explains.push_back(line.substr(i));
if (option_pattern.length() > max_pattern_length) {
max_pattern_length = option_pattern.length();
}
}
max_pattern_length += 4;
for (size_t i = 0; i < usage_line_indexes.size(); ++i) {
std::string &line = _usage_lines[usage_line_indexes[i]];
line.append(max_pattern_length - line.length(), ' ');
line.append(option_explains[i]);
}
}
void parse(int argc, const char * const argv[]) {
std::string key, val;
std::string::size_type pos;
Option *option;
for (int i = 1; i < argc; ++i) {
val = argv[i];
if (val[0] == '-') {
parse_option:
if ((pos = val.find('=')) != std::string::npos) {
key = val.substr(1, pos - 1);
val = val.substr(pos + 1);
} else {
key = val.substr(1);
val.clear();
}
option = find_option_by_name(key);
if (option) {
option->is_given = true;
} else {
invalid_option(argv[0], key);
}
unsigned int n = 0;
if (!val.empty()) {
goto push_option_val;
}
for (; n < option->num_args && ++i < argc;) {
val = argv[i];
if (val[0] == '-') {
goto parse_option;
}
push_option_val:
option->values.push_back(val);
n += 1;
}
} else {
_args.push_back(val);
}
}
#ifdef TEST_OPTION_PARSER
std::cout << "Given options:" << std::endl;
for (auto &option : _options) {
if (option.is_given) {
std::cout << " " << option.pattern
<< ": num_args: " << option.num_args
<< ", values:";
for (auto &v : option.values) {
std::cout << " " << v;
}
std::cout << std::endl;
}
}
std::cout << "Other options:" << std::endl;
for (auto &option : _options) {
if (!option.is_given) {
std::cout << " " << option.pattern << std::endl;
}
}
std::cout << "args:";
for (auto &x : _args) {
std::cout << " " << x;
}
std::cout << std::endl << std::endl;
#endif
if (has("?")) {
show_usage();
exit(0);
}
}
void show_usage() {
for (auto &line : _usage_lines) {
std::cout << line << std::endl;
}
}
const std::vector<std::string> &args() const { return _args; }
const Option *operator[](const std::string &name) {
return find_option_by_name(name);
}
const std::string &get(const std::string &name,
const std::string &dval) {
return get(name, 0, dval);
}
int get(const std::string &name, int dval) {
return get(name, 0, dval);
}
const std::string &get(const std::string &name, size_t i,
const std::string &dval) {
const Option *opt = find_option_by_name(name);
if (opt && opt->is_given && opt->values.size() > i) {
return opt->values[i];
}
return dval;
}
int get(const std::string &name, size_t i, int dval) {
const Option *opt = find_option_by_name(name);
if (opt && opt->is_given && opt->values.size() > i) {
return std::stoi(opt->values[i]);
}
return dval;
}
bool has(const std::string &name) {
const Option *opt = find_option_by_name(name);
return opt && opt->is_given;
}
private:
std::map<std::string, size_t> _option_index_by_names;
std::vector<std::string> _args;
std::vector<std::string> _usage_lines;
std::vector<Option> _options;
Option *find_option_by_name(const std::string &name) {
auto iter = _option_index_by_names.find(name);
if (iter != _option_index_by_names.end()) {
size_t option_index = iter->second;
return &_options[option_index];
}
return nullptr;
}
std::vector<std::string> parse_names(const std::string &line,
std::string::size_type &i) {
std::vector<std::string> names;
std::string name;
char c;
if (line[i] == '(') {
for (++i; i < line.length(); ++i) {
c = line[i];
if (c == '|' || c == ')') {
if (!name.empty()) {
names.push_back(name);
name.clear();
}
if (c == ')') {
break;
}
} else if (c != ' ') {
name.push_back(c);
}
}
if (c != ')' || (++i < line.length() && line[i] != ' ')) {
invalid_option(nullptr, line);
}
} else {
for (; i < line.length(); ++i) {
c = line[i];
if (c == ' ') {
break;
}
name.push_back(c);
}
names.push_back(name);
}
return names;
}
std::vector<std::string> parse_placeholders(
const std::string &line, std::string::size_type &i) {
std::vector<std::string> phs;
std::string ph;
char c;
int nspace = 0;
for (; i < line.length(); ++i) {
c = line[i];
if (c == ' ') {
if (!ph.empty()) {
phs.push_back(ph);
ph.clear();
}
nspace += 1;
} else {
if (nspace > 1) {
break;
}
nspace = 0;
ph.push_back(c);
}
}
return phs;
}
std::string invalid_option(const char *prog, const std::string &name) {
if (prog) {
std::cout << prog << ": error: unknown command line option '"
<< name << "'\n";
} else {
std::cout << "invalid option entry '" << name << "'\n";
}
exit(1);
}
std::vector<std::string> split(const std::string& str,
const std::string& sep) {
std::vector<std::string> v;
std::string::size_type lensep = sep.length();
std::string::size_type start = 0;
std::string::size_type end = 0;
while ((end = str.find(sep, start)) != std::string::npos) {
v.push_back(str.substr(start, end - start));
start = end + lensep;
}
v.push_back(str.substr(start));
return v;
}
};