-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparser.cpp
479 lines (407 loc) · 13.6 KB
/
argparser.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "argparser.h"
argparser::argparser(int argc, char **argv)
{
this->argc = argc;
this->argv = argv;
this->current_group = "General Options.";
this->usage_info = "";
this->header = "Help menu.";
this->footer = "(c) 2020 ";
}
argparser::~argparser()
{
}
int argparser::check_duplicate(const char* a_name, const char* alt_name)
{
bool duplicate = false;
typedef std::map<std::string, std::vector<option_t> > opt_map;
for(opt_map::iterator sn = this->my_options.begin() ; sn != this->my_options.end() ; sn++)
{
for(std::vector<option_t>::iterator itr = sn->second.begin() ;
itr != sn->second.end() ; itr++)
{
if((itr->opt_alt_name == alt_name) || (itr->opt_name == a_name) )
{
duplicate = true;
break;
}
}
}
if(duplicate)
{
std::cerr << "[W] Duplicate option added." << std::endl;
return 1;
}
return 0;
}
void argparser::add_option(const char* a_name, const char* alt_name, bool *is_parsed,
const char* desc , bool is_required)
{
if(this->my_options.size() == 0) add_option_group();
if(check_duplicate(a_name, alt_name))
return;
//if h is used and option is not help
if(!strcmp(alt_name, "-h") && strcmp(a_name, "--help"))
{
std::cerr << "Short option -h is reserved for help." << std::endl;
return;
}
option_t opt;
opt.opt_name = a_name;
opt.opt_alt_name = alt_name;
opt.is_parsed = is_parsed;
opt.opt_desc = desc;
opt.opt_type = A_NONE;
opt.opt_ref = NULL;
opt.is_required = is_required;
this->my_options.at(this->current_group).push_back(opt);
}
void argparser::add_option(const char* a_name, const char* alt_name, bool *is_parsed,
arg_type type, void* ref, const char* desc , bool is_required)
{
if(this->my_options.size() == 0) add_option_group();
if(check_duplicate(a_name, alt_name))
return;
//if h is used and option is not help
if(!strcmp(alt_name, "-h") && strcmp(a_name, "--help"))
{
std::cerr << "Short option -h is reserved for help." << std::endl;
return;
}
option_t opt;
opt.opt_name = a_name;
opt.opt_alt_name = alt_name;
opt.is_parsed = is_parsed;
opt.opt_desc = desc;
opt.opt_type = type;
opt.opt_ref = ref;
opt.is_required = is_required;
this->my_options.at(this->current_group).push_back(opt);
}
int argparser::required_options()
{
int num = 0;
typedef std::map<std::string, std::vector<option_t> > opt_map;
for(opt_map::iterator sn = this->my_options.begin() ; sn != this->my_options.end() ; sn++)
{
for(std::vector<option_t>::iterator itr = sn->second.begin() ;
itr != sn->second.end() ; itr++)
{
if(itr->is_required)
{
num += 1;
int check = num;
std::string tmp = "-" + itr->opt_alt_name;
std::string tmp1 = "--" + itr->opt_name;
for(int n = 1; n < this->argc;n++)
{
if(!strcmp(tmp.c_str(), this->argv[n]) ||
!strcmp(tmp1.c_str(), this->argv[n]))
{
num -= 1;
break;
}
}
if(check == num)
std::cerr << "[E] Missing argument: --" << itr->opt_name << " or -" << itr->opt_alt_name
<< " .\n";
}
}
}
return num;
}
int argparser::parse()
{
add_help_option();
get_opt_len();
if(argc > 1)
{
for(int n = 1; n < argc; n++)
{
if(!strcmp(argv[n],help_opt[0].c_str()) || !strcmp(argv[n], help_opt[1].c_str()))
{
show_help();
}
}
for(int count = 1 ; count < argc ; count++)
{
if(argv[count][0] == '-')
{
opt_check ret = check_option(argv[count]);
if(ret.group != "")
{
if(index_accessed(ret.group.c_str()))
{
show_help("Already parsed option: " + std::string(argv[count]));
}
*this->my_options.at(ret.group).at(ret.index).is_parsed = true;
if(this->my_options.at(ret.group).at(ret.index).opt_type != A_NONE)
{
++count;
if(count != argc)
{
std::string possible_boolean[2][6] = {"Y","y", "1","true","True","TRUE",
"N","n","0","false","False","FALSE"};
int a;
double b;
float c;
std::string d;
bool e;
char f;
switch (this->my_options.at(ret.group).at(ret.index).opt_type)
{
case A_INT:
a = std::stoi(argv[count]);
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &a, sizeof(a));
break;
case A_FLOAT:
c = std::stof(argv[count]);
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &c, sizeof(c));;
break;
case A_DOUBLE:
b = std::stod(argv[count]);
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &b, sizeof(b));
break;
case A_CHAR:
f = argv[count][0];
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &f, sizeof(f));
break;
case A_STRING:
d = std::string(argv[count]);
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &d, sizeof(d));
break;
case A_BOOL:
if(possible_boolean[0]->find(argv[count]) != std::string::npos)
{
e = true;
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &e, sizeof(e));
}
else if(possible_boolean[1]->find(argv[count]) != std::string::npos)
{
e = false;
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &e, sizeof(e));
}
else
{
show_help("Unkwown boolean value: " + std::string(argv[count]));
}
break;
case A_NONE:
/* unlikely to reach here */
break;
default:
a = std::stoi(argv[count]);
memcpy(this->my_options.at(ret.group).at(ret.index).opt_ref , &a, sizeof(a));
break;
}
}
else
{
show_help("Missing argument for option: " + std::string(argv[count - 1]));
}
}
}
else
{
show_help("Unknown option: " + std::string(argv[count]));
}
}
else
{
show_help("Unknown option: " + std::string(argv[count]));
}
}
}
else if(required_options())
{
show_help("You must include the missing arguments.");
}
else
{
show_help();
}
return 0;
}
void argparser::show_help(std::string message)
{
std::string appname = this->argv[0];
std::string help_buff;
if(message.length() != 0)
{
help_buff = message + "\n\n";
}
if(usage_info.length() == 0)
{
help_buff.append("Usage: " + appname + " [options] ...\n\n");
}
else {
help_buff.append(usage_info + "\n\n");
}
if(this->header.length() != 0)
{
help_buff.append(this->header + "\n");
}
if(this->my_options.at(this->current_group).size() != 0)
{
typedef std::map<std::string, std::vector<option_t> > opt_map;
for(opt_map::iterator sn = this->my_options.begin() ; sn != this->my_options.end() ; sn++)
{
help_buff.append( "\n" + sn->first + " \n");
for(std::vector<option_t>::iterator itr = sn->second.begin() ;
itr != sn->second.end() ; itr++)
{
help_buff.append(f_output_line(itr->opt_name, itr->opt_alt_name, itr->opt_desc));
}
}
}
if(this->footer.length() != 0)
{
help_buff.append( "\n\n" + this->footer + "\n");
}
std::cout << help_buff << std::endl;
exit(1);
}
argparser::opt_check argparser::check_option(const char* opt)
{
opt_check temp = {
.group = "",
.index = 0
};
typedef std::map<std::string, std::vector<option_t> > opt_map;
for(opt_map::iterator sn = this->my_options.begin() ; sn != this->my_options.end() ; sn++)
{
temp.group = sn->first;
temp.index = 0;
for(std::vector<option_t>::iterator itr = sn->second.begin() ;
itr != sn->second.end() ; itr++)
{
if((("--" + itr->opt_name) == opt) || (("-" + itr->opt_alt_name) == opt))
{
return temp;
}
temp.index += 1;
}
}
temp.group = "";
temp.index = 0;
return temp;
}
int argparser::index_accessed(const char* a_name)
{
for(std::list<std::string>::iterator itr = accessed_index.begin(); itr != accessed_index.end(); itr++)
{
if(*itr == a_name)
{
return 1;
}
}
accessed_index.push_back(a_name);
return 0;
}
void argparser::get_opt_len()
{
typedef std::map<std::string, std::vector<option_t> > opt_map;
for(opt_map::iterator sn = this->my_options.begin() ; sn != this->my_options.end() ; sn++)
{
for(std::vector<option_t>::iterator itr = sn->second.begin() ;
itr != sn->second.end() ; itr++)
{
if(itr->opt_alt_name.length() > max_alt_opt_len)
{
max_alt_opt_len = (int)itr->opt_alt_name.length();
}
if(itr->opt_name.length() > max_alt_opt_len)
{
max_opt_len = (int)itr->opt_name.length();
}
}
}
opt_len = 6 + max_opt_len + 3 + max_alt_opt_len + 4;
}
std::string argparser::f_output_line(std::string n_opt, std::string a_opt, std::string data)
{
std::string tmp;
tmp = padding("",3);
tmp.append("-");
tmp.append(padding(a_opt, max_alt_opt_len - a_opt.length() + 3));
tmp.append("--");
tmp.append(padding(n_opt, (max_opt_len + 4) - n_opt.length()));
if((opt_len + data.length()) < LINE_LENGTH)
{
tmp.append(data);
tmp.append("\n");
}
else
{
size_t data_len = LINE_LENGTH - opt_len;
while(1)
{
if(data.length() > data_len)
{
for (size_t i = data_len; i > 0 ; i--)
{
if(data[i] == ' ')
{
tmp.append(data.substr(0, i));
data = data.substr(i,data.size());
tmp.append(padding("\n",opt_len));
break;
}
}
}
else
{
tmp.append(data);
break;
}
}
}
return tmp;
}
std::string argparser::padding(std::string data, int num)
{
std::string tmp = data;
for(int n = 0; n < num; n++ )
tmp.append(" ");
return tmp;
}
void argparser::add_help_option()
{
if(!check_duplicate("help","h")) // Help option not defined.
{
option_t opt;
opt.opt_alt_name = "h";
opt.opt_name = "help";
opt.opt_desc = "Print the help menu.";
opt.opt_type = A_NONE;
opt.is_required = false;
this->help_opt[0] = "-" + opt.opt_alt_name;
this->help_opt[1] = "--" + opt.opt_name;
this->my_options.begin()->second.push_back(opt);
}
}
void argparser::add_option_group(const char* opt_group_dec)
{
if(strlen(opt_group_dec) != 0)
this->current_group = opt_group_dec;
if(this->my_options.count(opt_group_dec) == 0)
{
std::pair<std::string, std::vector<option_t>> m_pair;
std::vector<option_t> n_options;
m_pair.first = opt_group_dec;
m_pair.second = n_options;
this->my_options.insert(m_pair);
}
}
void argparser::set_usage_info(std::string info)
{
this->usage_info = info;
}
void argparser::set_header_info(std::string info)
{
this->header = info;
}
void argparser::set_footer_info(std::string info)
{
this->footer = info;
}