-
Notifications
You must be signed in to change notification settings - Fork 1
/
argparse.cpp
executable file
·423 lines (377 loc) · 9.97 KB
/
argparse.cpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/***
argparse.cpp
Command line argument parsing and verification for libcodehappy.
The argument parser can save options/flags and their values, as well as any command
line arguments that are not known flags. (Flags may be indicated by a prefix of "-",
"--", or "/" identically.) ArgParse comes with a built-in help flag (named "help" or "?"),
which displays all of the known options and their descriptions. Requirements may
be added to arguments to ensure that values are in the correct range.
Copyright (c) 2022 C. M. Street.
***/
#include <libcodehappy.h>
const u32 MAX_CMD_LINE_ARGS = 256;
ArgParse::ArgParse() {
}
ArgParse::~ArgParse() {
for (auto& e : args) {
auto& cmd = e.second;
if (cmd.ty == type_string && cmd.present) {
delete cmd.val.v_str;
cmd.present = false;
}
}
}
void ArgParse::add_argument(const std::string& argname, ArgType typ, const std::string& help_string, void* data_ptr) {
ArgumentCmd cmd;
cmd.name = argname;
cmd.ty = typ;
cmd.present = false;
cmd.helpstr = help_string;
#ifdef GENERIC_REQUIREMENTS
cmd.reqfail_str = "";
#endif
cmd.ptr = data_ptr;
args[argname] = cmd;
}
#ifdef GENERIC_REQUIREMENTS
void ArgParse::enforce_requirement(const std::string& argname, const Requirement& rqt, const std::string& reqfail_str) {
if (args.find(argname) == args.end()) {
codehappy_cerr << "Unknown flag '" << argname << "' when setting requirement\n";
return;
}
args[argname].req = rqt;
args[argname].reqfail_str = reqfail_str;
}
#endif
void ArgParse::add_help_flags() {
args["help"].name = "help";
args["help"].helpstr = "Show this message";
args["help"].ty = type_none;
args["help"].present = false;
args["help"].ptr = nullptr;
args["?"] = args["help"];
}
void ArgParse::ensure_args(const char* full_argstr) {
std::vector<char*> args;
char* cpy;
char* w, * we;
cpy = new char [strlen(full_argstr) + 1];
strcpy(cpy, full_argstr);
w = cpy;
we = w + strlen(w);
while (w < we) {
if (isspace(*w)) {
++w;
continue;
}
// An argument enclosed in double quotes.
if (*w == '\"') {
++w;
args.push_back(w);
while (w < we && *w != '\"') {
++w;
}
*w = '\000';
if (w < we)
++w;
continue;
}
// A regular argument.
args.push_back(w);
while (w < we && !isspace(*w))
++w;
*w = '\000';
++w;
}
int argc = args.size();
char* argv[MAX_CMD_LINE_ARGS];
for (int e = 0; e < argc && e < 256; ++e) {
argv[e] = args[e];
}
ensure_args(argc, (const char**)argv);
delete cpy;
}
void ArgParse::ensure_args(int argc, const char* argv[]) {
int e;
/* Flags "help" and "?" are always defined. */
add_help_flags();
for (e = 1; e < argc; ++e) {
if (argv[e][0] == '-' || argv[e][0] == '/') {
/* A flag/option. */
const char* w = argv[e] + 1;
while (*w == '-')
++w;
if (flag_known(w)) {
e = ensure_arg_val(argc, argv, e, args[w]);
} else if (argv[e][0] != '/') {
codehappy_cerr << "Unknown flag '" << w << "'\n";
show_help();
} else {
unenum_args.push_back(argv[e]);
}
} else {
/* An unenumerated/unknown option. Store it. */
unenum_args.push_back(argv[e]);
}
}
}
int ArgParse::ensure_arg_val(int argc, const char* argv[], int idx, ArgumentCmd& a) {
if (a.ty == type_none) {
a.present = true;
if (a.name == "help" || a.name == "?")
show_help();
if (a.ptr != nullptr)
*((bool *)(a.ptr)) = true;
return idx;
}
if (idx + 1 >= argc) {
codehappy_cerr << "Argument '" << argv[idx] << "' requires a value\n";
a.present = false;
return idx;
}
++idx;
switch (a.ty) {
case type_int:
a.val.v_i = atoi(argv[idx]);
a.present = true;
if (a.ptr != nullptr)
*((int *)(a.ptr)) = a.val.v_i;
break;
case type_uint:
a.val.v_u = strtoul(argv[idx], nullptr, 10);
a.present = true;
if (a.ptr != nullptr)
*((uint *)(a.ptr)) = a.val.v_u;
break;
case type_int64:
a.val.v_i64 = strtoll(argv[idx], nullptr, 10);
a.present = true;
if (a.ptr != nullptr)
*((i64 *)(a.ptr)) = a.val.v_i64;
break;
case type_uint64:
a.val.v_u64 = strtoull(argv[idx], nullptr, 10);
a.present = true;
if (a.ptr != nullptr)
*((u64 *)(a.ptr)) = a.val.v_u64;
break;
case type_double:
a.val.v_g = atof(argv[idx]);
a.present = true;
if (a.ptr != nullptr)
*((double *)(a.ptr)) = a.val.v_g;
break;
case type_string:
a.val.v_str = new char [strlen(argv[idx]) + 1];
strcpy(a.val.v_str, argv[idx]);
a.present = true;
// TODO: allow specifying the length of the passed buffer?
if (a.ptr != nullptr)
strcpy((char*)a.ptr, a.val.v_str);
break;
case type_bool:
/* Bools can be represented the following ways:
(Numeric) 0 = false, anything else = true
t/f/true/false Capitalization ignored.
y/n/yes/no Capitalization ignored. */
a.present = true;
if (isdigit(argv[idx][0])) {
int v = atoi(argv[idx]);
a.val.v_b = (v != 0);
} else if (strlen(argv[idx]) == 1) {
switch(tolower(argv[idx][0])) {
case 't':
case 'y':
a.val.v_b = true;
break;
case 'n':
case 'f':
a.val.v_b = false;
break;
default:
codehappy_cerr << "unknown boolean " << argv[idx] << " -- does this mean 'true' or 'false'?\n";
a.present = false;
break;
}
} else {
if (!__strnicmp(argv[idx], "true", 4)) {
a.val.v_b = true;
} else if (!__strnicmp(argv[idx], "false", 5)) {
a.val.v_b = false;
} else if (!__strnicmp(argv[idx], "no", 2)) {
a.val.v_b = false;
} else if (!__strnicmp(argv[idx], "yes", 3)) {
a.val.v_b = true;
} else {
codehappy_cerr << "unknown boolean option " << argv[idx] << "\n";
a.present = false;
}
}
if (a.ptr != nullptr)
*((bool *)(a.ptr)) = a.val.v_b;
break;
}
return idx;
}
bool ArgParse::flag_present(const std::string& name) const {
const auto it = args.find(name);
if (it == args.end())
return false;
return it->second.present;
}
bool ArgParse::flag_known(const std::string& name) const {
const auto it = args.find(name);
return it != args.end();
}
void ArgParse::nonflag_arg(u32 idx, std::string& str_out) const {
if (idx >= unenum_args.size()) {
str_out.clear();
return;
}
str_out = unenum_args[idx];
}
void ArgParse::all_nonflag_args(std::string& str_out) const {
if (0 == nonflag_args()) {
str_out.clear();
return;
}
nonflag_arg(0, str_out);
// if the text is separated by spaces w/o quotes, it's present as multiple command line arguments; let's assemble the complete text.
for (int e = 1; e < nonflag_args(); ++e) {
std::string text_cont;
str_out += " ";
nonflag_arg(e, text_cont);
str_out += text_cont;
}
}
void ArgParse::say_bad_type(const std::string& option_name) const {
codehappy_cerr << "Incompatible type for option '" << option_name << "'\n";
}
int ArgParse::value_int(const std::string& option_name) const {
const auto it = args.find(option_name);
if (it == args.end())
return 0;
switch (it->second.ty) {
case type_double:
case type_string:
case type_none:
say_bad_type(option_name);
break;
}
return it->second.val.v_i;
}
uint ArgParse::value_uint(const std::string& option_name) const {
const auto it = args.find(option_name);
if (it == args.end())
return 0;
switch (it->second.ty) {
case type_double:
case type_string:
case type_none:
say_bad_type(option_name);
break;
}
return it->second.val.v_u;
}
i64 ArgParse::value_i64(const std::string& option_name) const {
const auto it = args.find(option_name);
if (it == args.end())
return 0;
switch (it->second.ty) {
case type_double:
case type_string:
case type_none:
say_bad_type(option_name);
break;
}
return it->second.val.v_i64;
}
u64 ArgParse::value_u64(const std::string& option_name) const {
const auto it = args.find(option_name);
if (it == args.end())
return 0;
switch (it->second.ty) {
case type_double:
case type_string:
case type_none:
say_bad_type(option_name);
break;
}
return it->second.val.v_u64;
}
double ArgParse::value_double(const std::string& option_name) const {
const auto it = args.find(option_name);
if (it == args.end())
return 0;
switch (it->second.ty) {
case type_int:
case type_uint:
case type_int64:
case type_uint64:
case type_string:
case type_none:
say_bad_type(option_name);
break;
}
return it->second.val.v_g;
}
std::string ArgParse::value_str(const std::string& option_name) const {
std::string ret;
const auto it = args.find(option_name);
if (it == args.end())
return "";
if (it->second.ty != type_string) {
say_bad_type(option_name);
ret = "";
} else {
ret = it->second.val.v_str;
}
return ret;
}
void ArgParse::value_str(const std::string& option_name, std::string& str_out) const {
if (flag_present(option_name))
str_out = value_str(option_name);
}
bool ArgParse::value_bool(const std::string& option_name) const {
const auto it = args.find(option_name);
if (it == args.end())
return false;
switch (it->second.ty) {
case type_double:
case type_string:
case type_none:
say_bad_type(option_name);
break;
}
return it->second.val.v_b;
}
struct HelpStr {
std::string name;
std::string helpstr;
};
static bool helpstr_sort_predicate(const HelpStr& hs1, const HelpStr& hs2) {
return hs1.name < hs2.name;
}
void ArgParse::show_help() const {
std::vector<HelpStr> helpstrs;
int maxlen = 0;
for (const auto& e : args) {
const auto& ac = e.second;
HelpStr hs;
if (e.first == "?")
continue;
hs.name = ac.name;
hs.helpstr = ac.helpstr;
helpstrs.push_back(hs);
maxlen = std::max(maxlen, (int) hs.name.length());
}
std::sort(helpstrs.begin(), helpstrs.end(), helpstr_sort_predicate);
for (const auto& e : helpstrs) {
std::cout << e.name;
for (int f = e.name.length(); f <= maxlen; ++f)
std::cout << " ";
std::cout << e.helpstr << "\n";
}
exit(1);
}
/* end argparse.cpp */