-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptions.hpp
More file actions
415 lines (402 loc) · 15.4 KB
/
Options.hpp
File metadata and controls
415 lines (402 loc) · 15.4 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
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
#ifndef _OPTIONS_HPP
#define _OPTIONS_HPP
#include <iostream>
#include <map>
#include <list>
using namespace std;
class Option{
public:
Option (string name_, string description, unsigned type_) : name(name_), description(description){type =opt_type( type_);};
string name;
bool setByUser;
string description;
enum opt_type {str, num, boolean, numlist, numrange, generic} type;
virtual ~Option() {};
};
template <typename T>
class GenericOpt : public Option{
public:
GenericOpt(string name_, string description, T def_val): Option(name_,description, opt_type::generic){
val = def_val;
}
void setVal(T val_){
val = val_;
setByUser = true;
}
T val;
};
class BoolOpt : public Option{
public:
BoolOpt(string name_, string description, bool def_val): Option(name_,description, opt_type::boolean){
val = def_val;
}
void setVal(bool val_){
val = val_;
setByUser = true;
}
bool val;
};
class StringOpt : public Option{
public:
StringOpt(string name_, string description, string def_val): Option(name_,description,opt_type::str){
val = def_val;
}
void setVal(string val_){
val = val_;
setByUser = true;
}
string val;
};
class NumOpt : public Option{
public:
NumOpt(string name_, string description, int def_val): Option(name_, description,opt_type::num){
}
void setVal(int val_){
val = val_;
setByUser = true;
}
int val;
};
class NumListOpt : public Option{
public:
NumListOpt(string name_, string description, int def_val): Option(name_, description,opt_type::numlist){
}
void setVal(list<int> val_){
val = val_;
setByUser = true;
}
list<int> val;
};
class NumRangeOpt : public Option{
public:
NumRangeOpt(string name_, string description, int def_val): Option(name_, description,opt_type::numrange){
}
void setVal(list<int> val_){
val = val_;
setByUser = true;
}
list<int> val;
};
class Options{
public:
static NumOpt* AddIntOption(string name_, string description, int def_val){
NumOpt *opt = new NumOpt(name_, description, def_val);
opt_map.emplace(make_pair(name_,opt));
return opt;
}
static StringOpt* AddStringOption(string name_, string description, string def_val){
StringOpt *opt = new StringOpt(name_, description, def_val);
opt_map.emplace(make_pair(name_,opt));
return opt;
}
static BoolOpt* AddBoolOption(string name_, string description, bool def_val){
BoolOpt *opt = new BoolOpt(name_, description, def_val);
opt_map.emplace(make_pair(name_,opt));
return opt;
}
static NumListOpt* AddIntListOption(string name_, string description){
NumListOpt *opt = new NumListOpt(name_, description, {});
opt_map.emplace(make_pair(name_,opt));
return opt;
}
static NumRangeOpt* AddIntRangeOption(string name_, string description){
NumRangeOpt *opt = new NumRangeOpt(name_, description, {});
opt_map.emplace(make_pair(name_,opt));
return opt;
}
static bool wasSet(string name_){
return opt_map.find(name_)->second->setByUser;
}
static int isBoolean(string name_){
return opt_map.find(name_)->second->type == Option::opt_type::boolean;
}
static int isNum(string name_){
return opt_map.find(name_)->second->type == Option::opt_type::num;
}
static int isString(string name_){
return opt_map.find(name_)->second->type == Option::opt_type::str;
}
static int isNumList(string name_){
return opt_map.find(name_)->second->type == Option::opt_type::numlist;
}
static int isRange(string name_){
return opt_map.find(name_)->second->type == Option::opt_type::numrange;
}
static int isGeneric(string name_){
return opt_map.find(name_)->second->type == Option::opt_type::generic;
}
static bool has(string name_){
return opt_map.find(name_) != opt_map.end();
}
static void setVal(string name, bool val){
if(opt_map.find(name) != opt_map.end()){
if( auto opt_bool = dynamic_cast<BoolOpt*>((opt_map.find(name)->second)))
opt_bool->setVal(val);
}
}
static void setVal(string name, int val){
if(opt_map.find(name) != opt_map.end()){
if( auto opt_bool = dynamic_cast<NumOpt*>((opt_map.find(name)->second)))
opt_bool->setVal(val);
}
}
static void setVal(string name, string val){
if(opt_map.find(name) != opt_map.end()){
if( auto opt_bool = dynamic_cast<StringOpt*>((opt_map.find(name)->second)))
opt_bool->setVal(val);
}
}
static void setVal(string name, list<int> val){
if(opt_map.find(name) != opt_map.end()){
if( auto opt_bool = dynamic_cast<NumListOpt*>((opt_map.find(name)->second)))
opt_bool->setVal(val);
if( auto opt_bool = dynamic_cast<NumRangeOpt*>((opt_map.find(name)->second)))
opt_bool->setVal(val);
}
}
static int parseArgs(int argc, char *argv[]){
appName = argv[0];
for(int i = 1; i< argc; i++){
if(argv[i][0] != '-'){
cout<< "cmd opt need to start with '-'\n" <<endl;
printHelp();
return 1;
}
string opt_name = argv[i]+1;
if(has(opt_name)){
if(isBoolean(argv[i]+1)){
setVal(argv[i]+1,true);
if(BoolOpt* b_opt = dynamic_cast<BoolOpt*>((opt_map.find(argv[i]+1))->second)){
b_opt->setVal(true);
}
}
if(isNum(argv[i]+1)){
i++;
if(i >=argc){
cout <<"Option "<<opt_name<<" requires an argument"<<endl;
printHelp();
return 1;
}
if(argv[i][0] == '-'){
int j = 1;
bool negative_number = true;
for(int j=1;argv[i][j]!='\0';j++){
if(argv[i][j] < '0' || argv[i][j] > '9'){
negative_number = false;
break;
}
}
if(!negative_number){
cout <<"Option "<<opt_name<<" requires an argument"<<endl;
printHelp();
return 1;
}
}
setVal(opt_name, atoi(argv[i]));
continue;
}
if(isString(argv[i]+1)){
i++;
if(i >= argc || argv[i][0] == '-'){
cout <<"Option "<<opt_name<<" requires an argument"<<endl;
printHelp();
return 1;
}
string arg_string(argv[i]);
setVal(opt_name, arg_string);
continue;
}
if(isNumList(argv[i]+1)){
i++;
if(i >= argc){
cout <<"Option "<<opt_name<<" requires an argument"<<endl;
printHelp();
return 1;
}
if(argv[i][0] == '-'){
int j = 1;
bool negative_number = true;
for(int j=1;argv[i][j]!='\0' && argv[i][j]!=',';j++){
if(argv[i][j] < '0' || argv[i][j] > '9'){
negative_number = false;
break;
}
}
if(!negative_number){
cout <<"Option "<<opt_name<<" requires an argument"<<endl;
printHelp();
return 1;
}
}
int j=0;
string curr_number= "";
list<int> values;
while(argv[i][j]!='\0'){
if((argv[i][j] < '0' || argv[i][j] > '9')
&& (argv[i][j]!=',') && (argv[i][j]!='-')){
cout<<"Error, expecting a list of comma separated numbers"<<endl;
printHelp();
return 1;
}
if( argv[i][j] == ','){
if(!curr_number.empty()){
values.push_back(atoi(curr_number.c_str()));
curr_number="";
}
else{
cout<<"Error, expecting a list of comma separated numbers"<<endl;
printHelp();
return 1;
}
}else{
curr_number+=argv[i][j];
}
j++;
}
if(!curr_number.empty())
values.push_back(atoi(curr_number.c_str()));
setVal(opt_name, values);
continue;
}
if(isRange(argv[i]+1)){
i++;
if(i>=argc || argv[i][0] == '-'){
cout <<"Option "<<opt_name<<" requires an argument"<<endl;
printHelp();
return 1;
}
int j=0;
string curr_number = "";
list<int> values;
while(argv[i][j]!='\0'){
if((argv[i][j] < '0' || argv[i][j] > '9')
&& (argv[i][j]!=':')){
cout<<"Error, expecting a list of 3 ':' separated numbers"<<endl;
printHelp();
return 1;
}
if( argv[i][j] == ':'){
if(!curr_number.empty()){
values.push_back(atoi(curr_number.c_str()));
curr_number = "";
}
else{
cout<<"Error, expecting a list of 3 ':' separated numbers"<<endl;
printHelp();
return 1;
}
}else{
curr_number+=argv[i][j];
}
j++;
}
if(!curr_number.empty())
values.push_back(atoi(curr_number.c_str()));
if(values.size() != 3){
cout<<"Error, expecting a list of 3 ':' separated numbers"<<endl;
printHelp();
return 1;
}
setVal(opt_name, values);
continue;
}
}else{
cout <<"Unrecognized option: "<< argv[i]<<endl;
printHelp();
return 1;
}
}
return 0;
}
static void printHelp(){
cout<<"Usage: "<<appName<<" [Options]"<<endl;
for(auto opt_pair : opt_map){
cout<<"-"<<opt_pair.first;
switch(opt_pair.second->type){
case Option::num:
if(NumOpt* b_opt = dynamic_cast<NumOpt*>(opt_pair.second)){
cout<<" <int>\t "<<b_opt->description<<"\t [default: "<<b_opt->val<<"]"<<endl;
}
break;
case Option::boolean:
if(BoolOpt* b_opt = dynamic_cast<BoolOpt*>(opt_pair.second)){
cout<<" <boolean>\t "<<b_opt->description<<"\t[default: "<<b_opt->val<<"]"<<endl;
}
break;
case Option::numlist:
if(NumListOpt* b_opt = dynamic_cast<NumListOpt*>(opt_pair.second)){
cout<<" <num1,num2>\t "<<b_opt->description<<"\t[default: {}]"<<endl;
}
break;
case Option::str:
if(StringOpt* b_opt = dynamic_cast<StringOpt*>(opt_pair.second)){
cout<<" <string>\t "<<b_opt->description<<"\t[default: '']"<<endl;
}
break;
case Option::numrange:
if(NumRangeOpt* b_opt = dynamic_cast<NumRangeOpt*>(opt_pair.second)){
cout<<" <begin:end:stride>\t "<<b_opt->description<<"\t[default: 0:0:0]"<<endl;
}
break;
default:
cout<<"Unrecognized type"<<endl;
}
}
}
static void dumpOpts(){
for(auto opt_pair : opt_map){
cout<<opt_pair.first<< ": ";
switch(opt_pair.second->type){
case Option::num:
{
NumOpt* b_opt = dynamic_cast<NumOpt*>((opt_pair.second));
if(b_opt!= nullptr){
cout << b_opt->val;
}
break;
}
case Option::boolean:
if(BoolOpt* b_opt = dynamic_cast<BoolOpt*>(opt_pair.second)){
cout << b_opt->val;
}
break;
case Option::numlist:
if(NumListOpt* b_opt = dynamic_cast<NumListOpt*>(opt_pair.second)){
for(auto v1_it = b_opt->val.begin(); v1_it != b_opt->val.end(); v1_it ++){
if(next(v1_it)!= b_opt->val.end())
cout << *v1_it << ",";
else
cout << *v1_it;
}
}
break;
case Option::str:
if(StringOpt* b_opt = dynamic_cast<StringOpt*>(opt_pair.second)){
cout << b_opt->val;
}
break;
case Option::numrange:
if(NumRangeOpt* b_opt = dynamic_cast<NumRangeOpt*>(opt_pair.second)){
for(auto v1_it = b_opt->val.begin(); v1_it != b_opt->val.end(); v1_it ++){
if(next(v1_it)!= b_opt->val.end())
cout << *v1_it << ":";
else
cout << *v1_it;
}
}
break;
default:
cout<<"Unrecognized type"<<endl;
}
if(opt_pair.second->setByUser)
cout << " (set) "<<endl;
else
cout<<endl;
}
}
static map<string,Option*> opt_map;
static string appName;
};
map<string,Option*> Options::opt_map = {};
string Options::appName = "";
#endif