2e73e7bc
David Mayerich
basic changes for...
|
1
2
|
#ifndef STIM_ARGUMENTS
#define STIM_ARGUMENTS
|
7a2d0012
David Mayerich
mirst1d updates
|
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <iterator>
#include <algorithm>
#ifdef _WIN32
#include <Windows.h>
#endif
|
2e73e7bc
David Mayerich
basic changes for...
|
16
|
namespace stim{
|
7a2d0012
David Mayerich
mirst1d updates
|
17
|
|
81e0d221
David Mayerich
separated executa...
|
18
|
class cmd_option
|
7a2d0012
David Mayerich
mirst1d updates
|
19
20
21
22
23
24
25
26
27
28
29
|
{
private:
bool ansi;
//argument name
std::string name;
//description of the argument
std::vector<std::string> desc;
//argument values
|
3b012a80
David Mayerich
added code for co...
|
30
31
32
33
34
35
|
std::vector<std::string> vals;
//range or example
std::string range;
//flag is true when the argument is user-specified
|
7a2d0012
David Mayerich
mirst1d updates
|
36
37
38
39
|
bool flag;
void parse_val(const std::string &s){
|
3b012a80
David Mayerich
added code for co...
|
40
41
|
vals.clear();
|
7a2d0012
David Mayerich
mirst1d updates
|
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, ' ')) {
vals.push_back(item);
}
}
void parse_desc(const std::string &s){
desc.clear();
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, '\n')) {
desc.push_back(item);
}
}
|
3b012a80
David Mayerich
added code for co...
|
60
61
|
public:
void set_ansi(bool b){ ansi = b; }
|
81e0d221
David Mayerich
separated executa...
|
62
63
|
//create an option with a given name, description, and default value
cmd_option(std::string _name, std::string _desc, std::string _default = "", std::string _range = "")
|
7a2d0012
David Mayerich
mirst1d updates
|
64
65
66
|
{
name = _name;
parse_desc(_desc);
|
3b012a80
David Mayerich
added code for co...
|
67
68
69
70
71
72
73
74
75
|
parse_val(_default);
//if a default value is provided, set the flag
if(_default != "")
flag = true;
else flag = false;
range = _range;
|
7a2d0012
David Mayerich
mirst1d updates
|
76
|
|
3b012a80
David Mayerich
added code for co...
|
77
78
79
80
81
82
83
|
}
int nargs()
{
return vals.size();
}
|
81e0d221
David Mayerich
separated executa...
|
84
|
//return the value of a text option
|
2e73e7bc
David Mayerich
basic changes for...
|
85
|
std::string as_string(unsigned int n = 0)
|
3b012a80
David Mayerich
added code for co...
|
86
87
88
|
{
if(!flag)
{
|
81e0d221
David Mayerich
separated executa...
|
89
|
std::cout<<"ERROR - Option requested without being set: "<<name<<std::endl;
|
3b012a80
David Mayerich
added code for co...
|
90
91
92
93
94
95
96
97
98
|
exit(1);
}
if(vals.size() > n)
return vals[n];
else return "";
}
|
81e0d221
David Mayerich
separated executa...
|
99
|
//return the value of a floating point option
|
2e73e7bc
David Mayerich
basic changes for...
|
100
|
float as_float(unsigned int n = 0)
|
3b012a80
David Mayerich
added code for co...
|
101
102
103
|
{
if(!flag)
{
|
81e0d221
David Mayerich
separated executa...
|
104
|
std::cout<<"ERROR - option requested without being set: "<<name<<std::endl;
|
3b012a80
David Mayerich
added code for co...
|
105
106
107
108
109
110
|
exit(1);
}
if(vals.size() > n)
{
float r;
|
2e73e7bc
David Mayerich
basic changes for...
|
111
|
if ( ! (std::istringstream(vals[n]) >> r) ) r = 0;
|
3b012a80
David Mayerich
added code for co...
|
112
113
114
115
116
117
|
return r;
}
else return 0;
}
|
81e0d221
David Mayerich
separated executa...
|
118
|
//return the value of an integer option
|
2e73e7bc
David Mayerich
basic changes for...
|
119
|
int as_int(unsigned int n = 0)
|
3b012a80
David Mayerich
added code for co...
|
120
121
122
|
{
if(!flag)
{
|
81e0d221
David Mayerich
separated executa...
|
123
|
std::cout<<"ERROR - option requested without being set: "<<name<<std::endl;
|
3b012a80
David Mayerich
added code for co...
|
124
125
126
127
128
129
|
exit(1);
}
if(vals.size() > n)
{
int r;
|
2e73e7bc
David Mayerich
basic changes for...
|
130
|
if ( ! (std::istringstream(vals[n]) >> r) ) r = 0;
|
3b012a80
David Mayerich
added code for co...
|
131
132
133
134
135
136
137
138
139
140
|
return r;
}
else return 0;
}
//get the width of the left column
int col_width()
{
int n = 3;
|
81e0d221
David Mayerich
separated executa...
|
141
|
//add the length of the option name
|
3b012a80
David Mayerich
added code for co...
|
142
143
144
145
146
147
148
149
|
n += name.size();
//if there are any default parameters
if(vals.size() > 0)
{
//padding (parenthesis, =, etc.)
n += 6;
|
81e0d221
David Mayerich
separated executa...
|
150
|
//for each default option value
|
2e73e7bc
David Mayerich
basic changes for...
|
151
|
for(unsigned int v=0; v<vals.size(); v++)
|
3b012a80
David Mayerich
added code for co...
|
152
153
154
155
156
157
158
|
n += vals[v].size() + 1;
}
//add a buffer of 4 characters
n += 4;
return n;
|
7a2d0012
David Mayerich
mirst1d updates
|
159
160
161
162
163
164
|
}
//string output
std::string toStr(int width = 0)
{
|
3b012a80
David Mayerich
added code for co...
|
165
166
167
168
|
std::stringstream ss;
int color_size = 0;
|
7a2d0012
David Mayerich
mirst1d updates
|
169
170
171
172
|
//create the left column
std::string left_part = std::string(" --") + name;
if(vals.size() != 0)
|
3b012a80
David Mayerich
added code for co...
|
173
174
175
|
{
if(ansi)
left_part += "\033[1;32m";
|
7a2d0012
David Mayerich
mirst1d updates
|
176
|
left_part += " ( = ";
|
2e73e7bc
David Mayerich
basic changes for...
|
177
|
for(unsigned int v=0; v<vals.size(); v++)
|
7a2d0012
David Mayerich
mirst1d updates
|
178
|
left_part += vals[v] + std::string(" ");
|
3b012a80
David Mayerich
added code for co...
|
179
180
181
182
183
184
185
|
left_part += ")";
if(ansi)
left_part += "\033[0m";
if(ansi)
color_size = 11;
}
else
|
7a2d0012
David Mayerich
mirst1d updates
|
186
187
188
|
color_size = 0;
//if no width is passed, put 4 spaces between left and right columns
|
3b012a80
David Mayerich
added code for co...
|
189
|
if(width == 0) width = col_width();
|
7a2d0012
David Mayerich
mirst1d updates
|
190
191
192
193
|
ss<<std::left<<std::setw(width + color_size)<<left_part;
//output right column
|
2e73e7bc
David Mayerich
basic changes for...
|
194
|
for(unsigned int d=0; d<desc.size(); d++)
|
7a2d0012
David Mayerich
mirst1d updates
|
195
196
197
|
{
if(d == 0)
ss<<desc[0];
|
3b012a80
David Mayerich
added code for co...
|
198
|
else
|
0ef519a4
David Mayerich
optimized materia...
|
199
|
ss<<std::endl<<std::setfill(' ')<<std::setw(width)<<" "<<desc[d];
|
7a2d0012
David Mayerich
mirst1d updates
|
200
|
|
3b012a80
David Mayerich
added code for co...
|
201
202
203
204
|
}
//output the range in the right column
if(range != "" && ansi)
|
0ef519a4
David Mayerich
optimized materia...
|
205
|
ss<<std::endl<<std::setfill(' ')<<std::setw(width)<<" "<<" "<<std::string("\033[1;36m") + range + "\033[0m";
|
7a2d0012
David Mayerich
mirst1d updates
|
206
|
else if(range != "")
|
0ef519a4
David Mayerich
optimized materia...
|
207
|
ss<<std::endl<<std::setfill(' ')<<std::setw(width)<<" "<<" "<<range;
|
7a2d0012
David Mayerich
mirst1d updates
|
208
209
|
return ss.str();
|
3b012a80
David Mayerich
added code for co...
|
210
211
|
}
|
81e0d221
David Mayerich
separated executa...
|
212
|
//compare the name of the option to a string
|
3b012a80
David Mayerich
added code for co...
|
213
214
215
216
217
|
bool operator==(std::string rhs)
{
return (name == rhs);
}
|
81e0d221
David Mayerich
separated executa...
|
218
|
//set the option to a given value
|
3b012a80
David Mayerich
added code for co...
|
219
220
221
222
223
224
225
226
227
228
229
|
void set(std::string _value)
{
parse_val(_value);
//set the flag
flag = true;
}
bool is_set()
{
return flag;
|
7a2d0012
David Mayerich
mirst1d updates
|
230
231
|
}
|
3b012a80
David Mayerich
added code for co...
|
232
233
234
235
236
237
238
239
|
};
struct argsection
{
std::string name;
unsigned int index;
};
|
025d4bf3
David Mayerich
added documentati...
|
240
241
|
/**The arglist class implements command line arguments.
Example:
|
025d4bf3
David Mayerich
added documentati...
|
242
|
|
9c97e126
David Mayerich
added an axis-ali...
|
243
|
1) Create an arglist instance:
|
025d4bf3
David Mayerich
added documentati...
|
244
|
|
9c97e126
David Mayerich
added an axis-ali...
|
245
|
stim::arglist args;
|
025d4bf3
David Mayerich
added documentati...
|
246
|
|
9c97e126
David Mayerich
added an axis-ali...
|
247
|
2) Add arguments:
|
025d4bf3
David Mayerich
added documentati...
|
248
249
250
251
252
|
args.add("help", "prints this help");
args.add("foo", "foo takes a single integer value", "", "[intval]");
args.add("bar", "bar takes two floating point values", "", "[value1], [value2]");
|
9c97e126
David Mayerich
added an axis-ali...
|
253
|
3) Parse the command line:
|
27b826a8
David Mayerich
added a spherical...
|
254
255
256
|
args.parse(argc, argv);
|
9c97e126
David Mayerich
added an axis-ali...
|
257
|
4) You generally want to immediately test for help and output available arguments:
|
025d4bf3
David Mayerich
added documentati...
|
258
259
260
261
|
if(args["help"].is_set())
std::cout<<args.str();
|
025d4bf3
David Mayerich
added documentati...
|
262
|
|
9c97e126
David Mayerich
added an axis-ali...
|
263
264
|
5) Retrieve values:
|
025d4bf3
David Mayerich
added documentati...
|
265
266
267
268
269
270
271
272
273
274
275
276
277
|
int foo;
float bar1, bar2;
if(args["foo"])
foo = args["foo"].as_int();
if(args["bar"]){
bar1 = args["bar"].as_float(0);
bar2 = args["bar"].as_float(1);
}
**/
|
3b012a80
David Mayerich
added code for co...
|
278
279
280
281
282
|
class arglist
{
private:
bool ansi;
|
81e0d221
David Mayerich
separated executa...
|
283
284
285
|
//vector of options
std::vector<cmd_option> opts;
std::vector<std::string> args;
|
3b012a80
David Mayerich
added code for co...
|
286
|
|
81e0d221
David Mayerich
separated executa...
|
287
|
//column width of the longest option
|
3b012a80
David Mayerich
added code for co...
|
288
289
290
291
292
293
294
|
int col_width;
//list of sections
std::vector<argsection> sections;
public:
|
0ef519a4
David Mayerich
optimized materia...
|
295
296
|
arglist(){
col_width = 0;
|
9c97e126
David Mayerich
added an axis-ali...
|
297
298
299
300
301
302
303
304
305
|
ansi = true;
//set ansi to false by default if this is a Windows system
// (console doesn't support ANSI colors)
#ifdef _WIN32
ansi=false;
#endif
|
0ef519a4
David Mayerich
optimized materia...
|
306
|
}
|
3b012a80
David Mayerich
added code for co...
|
307
|
|
025d4bf3
David Mayerich
added documentati...
|
308
309
310
|
///Sets ANSI support (default is on), which allows colored values in the help output.
/// @param b is a boolean value specifying ANSI support (true is on, false is off)
|
3b012a80
David Mayerich
added code for co...
|
311
312
313
|
void set_ansi(bool b)
{
ansi = b;
|
81e0d221
David Mayerich
separated executa...
|
314
315
|
for(unsigned int i=0; i<opts.size(); i++)
opts[i].set_ansi(ansi);
|
3b012a80
David Mayerich
added code for co...
|
316
317
|
}
|
025d4bf3
David Mayerich
added documentati...
|
318
319
320
321
322
323
|
///Add a supported command line argument.
/// @param _name is the name of the command line argument (supplied as --name)
/// @param _desc is a text description of the argument
/// @param _default is the default value of the argument
/// @param _range is the supported range of values, list of values, etc. It will be displayed to the user.
|
3b012a80
David Mayerich
added code for co...
|
324
325
|
void add(std::string _name, std::string _desc, std::string _default = "", std::string _range = "")
{
|
81e0d221
David Mayerich
separated executa...
|
326
327
328
|
cmd_option opt(_name, _desc, _default, _range);
opt.set_ansi(ansi);
opts.push_back(opt);
|
3b012a80
David Mayerich
added code for co...
|
329
|
|
81e0d221
David Mayerich
separated executa...
|
330
|
col_width = std::max<int>(col_width, opt.col_width());
|
3b012a80
David Mayerich
added code for co...
|
331
332
|
}
|
025d4bf3
David Mayerich
added documentati...
|
333
334
335
|
///Specifies a section name that can be used to organize parameters in the output.
/// @param _name is the name of the section, which will be displayed to the user
|
3b012a80
David Mayerich
added code for co...
|
336
337
338
339
|
void section(std::string _name)
{
argsection s;
s.name = _name;
|
81e0d221
David Mayerich
separated executa...
|
340
|
s.index = opts.size();
|
3b012a80
David Mayerich
added code for co...
|
341
342
343
|
sections.push_back(s);
}
|
025d4bf3
David Mayerich
added documentati...
|
344
|
///Outputs supported arguments. If ANSI support is available, they will be color-coded. Generally this is called in response to "--help"
|
7a2d0012
David Mayerich
mirst1d updates
|
345
|
std::string str()
|
3b012a80
David Mayerich
added code for co...
|
346
347
348
349
350
351
352
353
|
{
std::stringstream ss;
int si = -1;
if(sections.size() > 0)
si = 0;
|
81e0d221
David Mayerich
separated executa...
|
354
355
|
//for each option
for(unsigned int a=0; a<opts.size(); a++)
|
3b012a80
David Mayerich
added code for co...
|
356
357
358
359
|
{
if(si != -1 && a == sections[si].index)
{
if(ansi)
|
eb5dfb2b
David Mayerich
fixed linux compa...
|
360
|
ss<<std::endl<<std::left<<std::setw(col_width)<<std::string("\033[1;31m") + sections[si].name<<"\033[0m"<<std::endl;
|
3b012a80
David Mayerich
added code for co...
|
361
|
else
|
eb5dfb2b
David Mayerich
fixed linux compa...
|
362
|
ss<<std::endl<<std::left<<std::setw(col_width)<<sections[si].name<<std::endl;
|
3b012a80
David Mayerich
added code for co...
|
363
|
si++;
|
2e73e7bc
David Mayerich
basic changes for...
|
364
|
if(si == (int)sections.size()) si = -1;
|
3b012a80
David Mayerich
added code for co...
|
365
366
|
}
|
81e0d221
David Mayerich
separated executa...
|
367
|
ss<<opts[a].toStr(col_width)<<std::endl;
|
3b012a80
David Mayerich
added code for co...
|
368
369
370
371
372
|
}
return ss.str();
}
|
025d4bf3
David Mayerich
added documentati...
|
373
374
375
|
///Retrieves the index for a supported argument, given that argument's name.
/// @param _name is the name of the requested argument
|
3b012a80
David Mayerich
added code for co...
|
376
377
|
int index(std::string _name)
{
|
81e0d221
David Mayerich
separated executa...
|
378
|
unsigned int i = find(opts.begin(), opts.end(), _name) - opts.begin();
|
3b012a80
David Mayerich
added code for co...
|
379
|
|
81e0d221
David Mayerich
separated executa...
|
380
|
if(i >= opts.size())
|
cac62fd3
David Mayerich
modified to work ...
|
381
|
return -1;
|
3b012a80
David Mayerich
added code for co...
|
382
|
|
cac62fd3
David Mayerich
modified to work ...
|
383
|
return (int)i;
|
3b012a80
David Mayerich
added code for co...
|
384
385
|
}
|
025d4bf3
David Mayerich
added documentati...
|
386
387
388
389
|
///Sets an argument to a specified value
/// @param _name is the name of the argument to be set
/// @param _value is the value that it is given
|
3b012a80
David Mayerich
added code for co...
|
390
391
392
393
394
395
|
void set(std::string _name, std::string _value)
{
int i = index(_name);
if(i != -1)
{
|
81e0d221
David Mayerich
separated executa...
|
396
|
opts[i].set(_value);
|
3b012a80
David Mayerich
added code for co...
|
397
|
//adjust the column width if necessary
|
81e0d221
David Mayerich
separated executa...
|
398
|
col_width = (std::max)(col_width, opts[i].col_width());
|
3b012a80
David Mayerich
added code for co...
|
399
400
|
}
else
|
81e0d221
David Mayerich
separated executa...
|
401
|
std::cout<<"ERROR - option not recognized: "<<_name<<std::endl;
|
3b012a80
David Mayerich
added code for co...
|
402
403
|
}
|
025d4bf3
David Mayerich
added documentati...
|
404
405
406
407
|
///Parses the command line
/// @param argc is the number of command line arguments (provided by the OS)
/// @param argv[] is the list of command line arguments (provided by the OS)
|
3b012a80
David Mayerich
added code for co...
|
408
409
|
void parse(int argc, char* argv[])
{
|
81e0d221
David Mayerich
separated executa...
|
410
|
//if the number of options is 1, we're done
|
3b012a80
David Mayerich
added code for co...
|
411
412
413
414
415
|
if(argc <= 1) return;
std::string name;
std::string params;
|
81e0d221
David Mayerich
separated executa...
|
416
417
|
bool args_done = false; //create a flag that turns true when the first option is encountered
|
3b012a80
David Mayerich
added code for co...
|
418
419
|
for(int i=1; i<argc; i++)
{
|
81e0d221
David Mayerich
separated executa...
|
420
|
//if the argument is an option
|
3b012a80
David Mayerich
added code for co...
|
421
422
|
if(argv[i][0] == '-' && argv[i][1] == '-')
{
|
81e0d221
David Mayerich
separated executa...
|
423
424
|
args_done = true; //arguments for the executable are done, all options now
//add any previous options
|
3b012a80
David Mayerich
added code for co...
|
425
426
|
if(name != "")
set(name, params);
|
81e0d221
David Mayerich
separated executa...
|
427
|
//set the current option to this name
|
3b012a80
David Mayerich
added code for co...
|
428
429
430
431
|
name = argv[i]+2;
//clear the parameters list
params = "";
}
|
81e0d221
David Mayerich
separated executa...
|
432
433
434
435
|
else if(!args_done){
args.push_back(argv[i]);
}
else{ //everything else is an arg for the most recent option
|
3b012a80
David Mayerich
added code for co...
|
436
437
438
439
440
441
|
if(params != "")
params += " ";
params += argv[i];
}
}
|
81e0d221
David Mayerich
separated executa...
|
442
|
//set the last option
|
27b826a8
David Mayerich
added a spherical...
|
443
444
|
if(name != "")
set(name, params);
|
3b012a80
David Mayerich
added code for co...
|
445
446
|
}
|
025d4bf3
David Mayerich
added documentati...
|
447
448
449
|
///Determines of a parameter has been set and returns true if it has
/// @param _name is the name of the argument
|
3b012a80
David Mayerich
added code for co...
|
450
451
|
bool operator()(std::string _name)
{
|
81e0d221
David Mayerich
separated executa...
|
452
|
int i = find(opts.begin(), opts.end(), _name) - opts.begin();
|
3b012a80
David Mayerich
added code for co...
|
453
454
455
456
457
458
459
|
if(i < 0)
{
std::cout<<"ERROR - Unspecified parameter name: "<<_name<<std::endl;
exit(1);
}
|
81e0d221
David Mayerich
separated executa...
|
460
|
return opts[i].is_set();
|
3b012a80
David Mayerich
added code for co...
|
461
462
|
}
|
025d4bf3
David Mayerich
added documentati...
|
463
464
465
|
///Returns the number of parameters for a specified argument
/// @param _name is the name of the argument whose parameter number will be returned
|
81e0d221
David Mayerich
separated executa...
|
466
|
unsigned int nargs(std::string _name)
|
3b012a80
David Mayerich
added code for co...
|
467
|
{
|
81e0d221
David Mayerich
separated executa...
|
468
|
int i = find(opts.begin(), opts.end(), _name) - opts.begin();
|
3b012a80
David Mayerich
added code for co...
|
469
470
471
472
473
474
475
|
if(i < 0)
{
std::cout<<"ERROR - Unspecified parameter name: "<<_name<<std::endl;
exit(1);
}
|
81e0d221
David Mayerich
separated executa...
|
476
477
478
|
return opts[i].nargs();
}
|
025d4bf3
David Mayerich
added documentati...
|
479
|
///Returns the number of arguments that have been set
|
81e0d221
David Mayerich
separated executa...
|
480
481
482
483
|
unsigned int nargs(){
return args.size();
}
|
e9bddc57
David Mayerich
added band croppi...
|
484
485
486
487
488
489
490
491
|
/// Returns the number of options that are set
unsigned int nopts(){
unsigned int n = 0; //initialize the counter for the number of options
for(unsigned int i = 0; i < opts.size(); i++) //go through each option
if(opts[i].is_set()) n++; //if a value is specified, increment the counter
return n;
}
|
025d4bf3
David Mayerich
added documentati...
|
492
493
494
|
///Returns the name of an argument, given its index
/// @param a is the index of the requested argument
|
81e0d221
David Mayerich
separated executa...
|
495
496
|
std::string arg(unsigned int a){
return args[a];
|
3b012a80
David Mayerich
added code for co...
|
497
498
|
}
|
025d4bf3
David Mayerich
added documentati...
|
499
500
501
|
///Returns an object describing the argument
/// @param _name is the name of the requested argument
|
81e0d221
David Mayerich
separated executa...
|
502
|
cmd_option operator[](std::string _name)
|
3b012a80
David Mayerich
added code for co...
|
503
|
{
|
81e0d221
David Mayerich
separated executa...
|
504
|
int i = find(opts.begin(), opts.end(), _name) - opts.begin();
|
3b012a80
David Mayerich
added code for co...
|
505
|
|
695d32fa
unknown
David's changes t...
|
506
|
if(i < 0 || i >= opts.size())
|
3b012a80
David Mayerich
added code for co...
|
507
508
509
510
511
|
{
std::cout<<"ERROR - Unspecified parameter name: "<<_name<<std::endl;
exit(1);
}
|
81e0d221
David Mayerich
separated executa...
|
512
|
return opts[i];
|
3b012a80
David Mayerich
added code for co...
|
513
514
515
|
}
|
7a2d0012
David Mayerich
mirst1d updates
|
516
517
518
519
|
};
|
2e73e7bc
David Mayerich
basic changes for...
|
520
|
} //end namespace stim
|
3b012a80
David Mayerich
added code for co...
|
521
522
|
#endif
|