-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptparser.cpp
444 lines (393 loc) · 10.2 KB
/
optparser.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
// Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
// the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
// reserved. See file COPYRIGHT for details.
//
// This file is part of the MFEM library. For more information and source code
// availability see http://mfem.org.
//
// MFEM is free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License (as published by the Free
// Software Foundation) version 2.1 dated February 1999.
#include "optparser.hpp"
#include "../linalg/vector.hpp"
#include <cctype>
namespace mfem
{
using namespace std;
int isValidAsInt(char * s)
{
if ( s == NULL || *s == '\0' )
{
return 0; //Empty string
}
if ( *s == '+' || *s == '-' )
{
++s;
}
if ( *s == '\0')
{
return 0; //sign character only
}
while (*s)
{
if ( !isdigit(*s) )
{
return 0;
}
++s;
}
return 1;
}
int isValidAsDouble(char * s)
{
//A valid floating point number for atof using the "C" locale is formed by
// - an optional sign character (+ or -),
// - followed by a sequence of digits, optionally containing a decimal-point
// character (.),
// - optionally followed by an exponent part (an e or E character followed by
// an optional sign and a sequence of digits).
if ( s == NULL || *s == '\0' )
{
return 0; //Empty string
}
if ( *s == '+' || *s == '-' )
{
++s;
}
if ( *s == '\0')
{
return 0; //sign character only
}
while (*s)
{
if (!isdigit(*s))
{
break;
}
++s;
}
if (*s == '\0')
{
return 1; //s = "123"
}
if (*s == '.')
{
++s;
while (*s)
{
if (!isdigit(*s))
{
break;
}
++s;
}
if (*s == '\0')
{
return 1; //this is a fixed point double s = "123." or "123.45"
}
}
if (*s == 'e' || *s == 'E')
{
++s;
return isValidAsInt(s);
}
else
{
return 0; //we have encounter a wrong character
}
}
void parseArray(char * str, Array<int> & var)
{
var.SetSize(0);
std::stringstream input(str);
int val;
while ( input >> val)
{
var.Append(val);
}
}
void parseVector(char * str, Vector & var)
{
int nentries = 0;
double val;
{
std::stringstream input(str);
while ( input >> val)
{
++nentries;
}
}
var.SetSize(nentries);
{
nentries = 0;
std::stringstream input(str);
while ( input >> val)
{
var(nentries++) = val;
}
}
}
void OptionsParser::Parse()
{
option_check.SetSize(options.Size());
option_check = 0;
for (int i = 1; i < argc; )
{
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
{
// print help message
error_type = 1;
return;
}
for (int j = 0; true; j++)
{
if (j >= options.Size())
{
// unrecognized option
error_type = 2;
error_idx = i;
return;
}
if (strcmp(argv[i], options[j].short_name) == 0 ||
strcmp(argv[i], options[j].long_name) == 0)
{
OptionType type = options[j].type;
if ( option_check[j] )
{
error_type = 4;
error_idx = j;
return;
}
option_check[j] = 1;
i++;
if (type != ENABLE && type != DISABLE && i >= argc)
{
// missing argument
error_type = 3;
error_idx = j;
return;
}
int isValid = 1;
switch (options[j].type)
{
case INT:
isValid = isValidAsInt(argv[i]);
*(int *)(options[j].var_ptr) = atoi(argv[i++]);
break;
case DOUBLE:
isValid = isValidAsDouble(argv[i]);
*(double *)(options[j].var_ptr) = atof(argv[i++]);
break;
case STRING:
*(const char **)(options[j].var_ptr) = argv[i++];
break;
case ENABLE:
*(bool *)(options[j].var_ptr) = true;
option_check[j+1] = 1; // Do not allow the DISABLE Option
break;
case DISABLE:
*(bool *)(options[j].var_ptr) = false;
option_check[j-1] = 1; // Do not allow the ENABLE Option
break;
case ARRAY:
parseArray(argv[i++], *(Array<int>*)(options[j].var_ptr) );
break;
case VECTOR:
parseVector(argv[i++], *(Vector*)(options[j].var_ptr) );
break;
}
if (!isValid)
{
error_type = 5;
error_idx = i;
return;
}
break;
}
}
}
// check for missing required options
for (int i = 0; i < options.Size(); i++)
if (options[i].required &&
(option_check[i] == 0 ||
(options[i].type == ENABLE && option_check[++i] == 0)))
{
error_type = 6; // required option missing
error_idx = i; // for a boolean option i is the index of DISABLE
return;
}
error_type = 0;
}
void OptionsParser::WriteValue(const Option &opt, std::ostream &out)
{
switch (opt.type)
{
case INT:
out << *(int *)(opt.var_ptr);
break;
case DOUBLE:
out << *(double *)(opt.var_ptr);
break;
case STRING:
out << *(const char **)(opt.var_ptr);
break;
case ARRAY:
{
Array<int> &list = *(Array<int>*)(opt.var_ptr);
out << '\'';
if (list.Size() > 0)
{
out << list[0];
}
for (int i = 1; i < list.Size(); i++)
{
out << ' ' << list[i];
}
out << '\'';
break;
}
case VECTOR:
{
Vector &list = *(Vector*)(opt.var_ptr);
out << '\'';
if (list.Size() > 0)
{
out << list(0);
}
for (int i = 1; i < list.Size(); i++)
{
out << ' ' << list(i);
}
out << '\'';
break;
}
default: // provide a default to suppress warning
break;
}
}
void OptionsParser::PrintOptions(ostream &out) const
{
static const char *indent = " ";
out << "Options used:\n";
for (int j = 0; j < options.Size(); j++)
{
OptionType type = options[j].type;
out << indent;
if (type == ENABLE)
{
if (*(bool *)(options[j].var_ptr) == true)
{
out << options[j].long_name;
}
else
{
out << options[j+1].long_name;
}
j++;
}
else
{
out << options[j].long_name << " ";
WriteValue(options[j], out);
}
out << '\n';
}
}
void OptionsParser::PrintError(ostream &out) const
{
static const char *line_sep = "";
out << line_sep;
switch (error_type)
{
case 2:
out << "Unrecognized option: " << argv[error_idx] << '\n' << line_sep;
break;
case 3:
out << "Missing argument for the last option: " << argv[argc-1] << '\n'
<< line_sep;
break;
case 4:
if (options[error_idx].type == ENABLE )
out << "Option " << options[error_idx].long_name << " or "
<< options[error_idx + 1].long_name
<< " provided multiple times\n" << line_sep;
else if (options[error_idx].type == DISABLE)
out << "Option " << options[error_idx - 1].long_name << " or "
<< options[error_idx].long_name
<< " provided multiple times\n" << line_sep;
else
out << "Option " << options[error_idx].long_name
<< " provided multiple times\n" << line_sep;
break;
case 5:
out << "Wrong option format: " << argv[error_idx - 1] << " "
<< argv[error_idx] << '\n' << line_sep;
break;
case 6:
out << "Missing required option: " << options[error_idx].long_name
<< '\n' << line_sep;
break;
}
out << endl;
}
void OptionsParser::PrintHelp(ostream &out) const
{
static const char *indent = " ";
static const char *seprtr = ", ";
static const char *descr_sep = "\n\t";
static const char *line_sep = "";
static const char *types[] = { " <int>", " <double>", " <string>", "", "",
" '<int>...'", " '<double>...'"
};
out << indent << "-h" << seprtr << "--help" << descr_sep
<< "Print this help message and exit.\n" << line_sep;
for (int j = 0; j < options.Size(); j++)
{
OptionType type = options[j].type;
out << indent << options[j].short_name << types[type]
<< seprtr << options[j].long_name << types[type]
<< seprtr;
if (options[j].required)
{
out << "(required)";
}
else
{
if (type == ENABLE)
{
j++;
out << options[j].short_name << types[type] << seprtr
<< options[j].long_name << types[type] << seprtr
<< "current option: ";
if (*(bool *)(options[j].var_ptr) == true)
{
out << options[j-1].long_name;
}
else
{
out << options[j].long_name;
}
}
else
{
out << "current value: ";
WriteValue(options[j], out);
}
}
out << descr_sep;
if (options[j].description)
{
out << options[j].description << '\n';
}
out << line_sep;
}
}
void OptionsParser::PrintUsage(ostream &out) const
{
static const char *line_sep = "";
PrintError(out);
out << "Usage: " << argv[0] << " [options] ...\n" << line_sep
<< "Options:\n" << line_sep;
PrintHelp(out);
}
}