Commit 025d4bf37b32e2295556e4498db82af4ff69f715
1 parent
1cb3eb53
added documentation to arguments.h
Showing
3 changed files
with
81 additions
and
11 deletions
Show diff stats
.gitignore
.gitmodules deleted
stim/parser/arguments.h
@@ -237,6 +237,48 @@ namespace stim{ | @@ -237,6 +237,48 @@ namespace stim{ | ||
237 | unsigned int index; | 237 | unsigned int index; |
238 | }; | 238 | }; |
239 | 239 | ||
240 | + /**The arglist class implements command line arguments. | ||
241 | + Example: | ||
242 | + | ||
243 | + 1) Create an arglist instance: | ||
244 | + | ||
245 | + stim::arglist args; | ||
246 | + | ||
247 | + 2) Test to see if ANSI output is supported (arguments will use color if it is). Generally, Windows consoles don't support ANSI. | ||
248 | + | ||
249 | + #ifdef _WIN32 | ||
250 | + args.set_ansi(false); | ||
251 | + #endif | ||
252 | + | ||
253 | + 3) Add arguments: | ||
254 | + | ||
255 | + args.add("help", "prints this help"); | ||
256 | + args.add("foo", "foo takes a single integer value", "", "[intval]"); | ||
257 | + args.add("bar", "bar takes two floating point values", "", "[value1], [value2]"); | ||
258 | + | ||
259 | + 4) You generally want to immediately test for help and output available arguments: | ||
260 | + | ||
261 | + if(args["help"].is_set()) | ||
262 | + std::cout<<args.str(); | ||
263 | + | ||
264 | + 5) Parse the command line: | ||
265 | + | ||
266 | + args.parse(argc, argv); | ||
267 | + | ||
268 | + 6) Retrieve values: | ||
269 | + | ||
270 | + int foo; | ||
271 | + float bar1, bar2; | ||
272 | + if(args["foo"]) | ||
273 | + foo = args["foo"].as_int(); | ||
274 | + if(args["bar"]){ | ||
275 | + bar1 = args["bar"].as_float(0); | ||
276 | + bar2 = args["bar"].as_float(1); | ||
277 | + } | ||
278 | + | ||
279 | + | ||
280 | + **/ | ||
281 | + | ||
240 | class arglist | 282 | class arglist |
241 | { | 283 | { |
242 | private: | 284 | private: |
@@ -259,6 +301,9 @@ namespace stim{ | @@ -259,6 +301,9 @@ namespace stim{ | ||
259 | ansi = true; | 301 | ansi = true; |
260 | } | 302 | } |
261 | 303 | ||
304 | + ///Sets ANSI support (default is on), which allows colored values in the help output. | ||
305 | + | ||
306 | + /// @param b is a boolean value specifying ANSI support (true is on, false is off) | ||
262 | void set_ansi(bool b) | 307 | void set_ansi(bool b) |
263 | { | 308 | { |
264 | ansi = b; | 309 | ansi = b; |
@@ -266,6 +311,12 @@ namespace stim{ | @@ -266,6 +311,12 @@ namespace stim{ | ||
266 | opts[i].set_ansi(ansi); | 311 | opts[i].set_ansi(ansi); |
267 | } | 312 | } |
268 | 313 | ||
314 | + ///Add a supported command line argument. | ||
315 | + | ||
316 | + /// @param _name is the name of the command line argument (supplied as --name) | ||
317 | + /// @param _desc is a text description of the argument | ||
318 | + /// @param _default is the default value of the argument | ||
319 | + /// @param _range is the supported range of values, list of values, etc. It will be displayed to the user. | ||
269 | void add(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") | 320 | void add(std::string _name, std::string _desc, std::string _default = "", std::string _range = "") |
270 | { | 321 | { |
271 | cmd_option opt(_name, _desc, _default, _range); | 322 | cmd_option opt(_name, _desc, _default, _range); |
@@ -275,6 +326,9 @@ namespace stim{ | @@ -275,6 +326,9 @@ namespace stim{ | ||
275 | col_width = std::max<int>(col_width, opt.col_width()); | 326 | col_width = std::max<int>(col_width, opt.col_width()); |
276 | } | 327 | } |
277 | 328 | ||
329 | + ///Specifies a section name that can be used to organize parameters in the output. | ||
330 | + | ||
331 | + /// @param _name is the name of the section, which will be displayed to the user | ||
278 | void section(std::string _name) | 332 | void section(std::string _name) |
279 | { | 333 | { |
280 | argsection s; | 334 | argsection s; |
@@ -283,7 +337,7 @@ namespace stim{ | @@ -283,7 +337,7 @@ namespace stim{ | ||
283 | sections.push_back(s); | 337 | sections.push_back(s); |
284 | } | 338 | } |
285 | 339 | ||
286 | - //output the options (generally in response to --help) | 340 | + ///Outputs supported arguments. If ANSI support is available, they will be color-coded. Generally this is called in response to "--help" |
287 | std::string str() | 341 | std::string str() |
288 | { | 342 | { |
289 | std::stringstream ss; | 343 | std::stringstream ss; |
@@ -312,6 +366,9 @@ namespace stim{ | @@ -312,6 +366,9 @@ namespace stim{ | ||
312 | return ss.str(); | 366 | return ss.str(); |
313 | } | 367 | } |
314 | 368 | ||
369 | + ///Retrieves the index for a supported argument, given that argument's name. | ||
370 | + | ||
371 | + /// @param _name is the name of the requested argument | ||
315 | int index(std::string _name) | 372 | int index(std::string _name) |
316 | { | 373 | { |
317 | unsigned int i = find(opts.begin(), opts.end(), _name) - opts.begin(); | 374 | unsigned int i = find(opts.begin(), opts.end(), _name) - opts.begin(); |
@@ -322,6 +379,10 @@ namespace stim{ | @@ -322,6 +379,10 @@ namespace stim{ | ||
322 | return (int)i; | 379 | return (int)i; |
323 | } | 380 | } |
324 | 381 | ||
382 | + ///Sets an argument to a specified value | ||
383 | + | ||
384 | + /// @param _name is the name of the argument to be set | ||
385 | + /// @param _value is the value that it is given | ||
325 | void set(std::string _name, std::string _value) | 386 | void set(std::string _name, std::string _value) |
326 | { | 387 | { |
327 | int i = index(_name); | 388 | int i = index(_name); |
@@ -336,7 +397,10 @@ namespace stim{ | @@ -336,7 +397,10 @@ namespace stim{ | ||
336 | std::cout<<"ERROR - option not recognized: "<<_name<<std::endl; | 397 | std::cout<<"ERROR - option not recognized: "<<_name<<std::endl; |
337 | } | 398 | } |
338 | 399 | ||
339 | - //parse a parameter string | 400 | + ///Parses the command line |
401 | + | ||
402 | + /// @param argc is the number of command line arguments (provided by the OS) | ||
403 | + /// @param argv[] is the list of command line arguments (provided by the OS) | ||
340 | void parse(int argc, char* argv[]) | 404 | void parse(int argc, char* argv[]) |
341 | { | 405 | { |
342 | //if the number of options is 1, we're done | 406 | //if the number of options is 1, we're done |
@@ -375,7 +439,9 @@ namespace stim{ | @@ -375,7 +439,9 @@ namespace stim{ | ||
375 | set(name, params); | 439 | set(name, params); |
376 | } | 440 | } |
377 | 441 | ||
378 | - //determine if a parameter has been set (either specified by the user or with a default value) | 442 | + ///Determines of a parameter has been set and returns true if it has |
443 | + | ||
444 | + /// @param _name is the name of the argument | ||
379 | bool operator()(std::string _name) | 445 | bool operator()(std::string _name) |
380 | { | 446 | { |
381 | int i = find(opts.begin(), opts.end(), _name) - opts.begin(); | 447 | int i = find(opts.begin(), opts.end(), _name) - opts.begin(); |
@@ -389,7 +455,9 @@ namespace stim{ | @@ -389,7 +455,9 @@ namespace stim{ | ||
389 | return opts[i].is_set(); | 455 | return opts[i].is_set(); |
390 | } | 456 | } |
391 | 457 | ||
392 | - //number of arguments in a specified option | 458 | + ///Returns the number of parameters for a specified argument |
459 | + | ||
460 | + /// @param _name is the name of the argument whose parameter number will be returned | ||
393 | unsigned int nargs(std::string _name) | 461 | unsigned int nargs(std::string _name) |
394 | { | 462 | { |
395 | int i = find(opts.begin(), opts.end(), _name) - opts.begin(); | 463 | int i = find(opts.begin(), opts.end(), _name) - opts.begin(); |
@@ -403,16 +471,21 @@ namespace stim{ | @@ -403,16 +471,21 @@ namespace stim{ | ||
403 | return opts[i].nargs(); | 471 | return opts[i].nargs(); |
404 | } | 472 | } |
405 | 473 | ||
406 | - //number of arguments for the executable | 474 | + ///Returns the number of arguments that have been set |
407 | unsigned int nargs(){ | 475 | unsigned int nargs(){ |
408 | return args.size(); | 476 | return args.size(); |
409 | } | 477 | } |
410 | 478 | ||
411 | - //return the a'th executable argument | 479 | + ///Returns the name of an argument, given its index |
480 | + | ||
481 | + /// @param a is the index of the requested argument | ||
412 | std::string arg(unsigned int a){ | 482 | std::string arg(unsigned int a){ |
413 | return args[a]; | 483 | return args[a]; |
414 | } | 484 | } |
415 | 485 | ||
486 | + ///Returns an object describing the argument | ||
487 | + | ||
488 | + /// @param _name is the name of the requested argument | ||
416 | cmd_option operator[](std::string _name) | 489 | cmd_option operator[](std::string _name) |
417 | { | 490 | { |
418 | int i = find(opts.begin(), opts.end(), _name) - opts.begin(); | 491 | int i = find(opts.begin(), opts.end(), _name) - opts.begin(); |