This is a class to aid handling of command line arguments in a C++ program. It follows (and enforces) the unsual conventions.
New arguments are added by calling functions of the form of new_[optional_/named_]type.
Unnamed arguments are expected to appear in order of addition on the command line. Named arguments can be passed in any order (and mixed with the unnamed arguments).
The special argument "--" means that all remaining arguments are treated as unnamed (so you can pass file names that begin with -).
When calling a new_foo function to create an argument the following can/must be passed in
When the program is called if "--help" is passed as an argument the useage information is printed and the program exits.
There is always an implicit "-v" flag for verbose which sets the dsr::verbose variable and a "-V" which sets the VERBOSE variable.
Any extra arguments or arguments with unexpected types are treated as errors and cause the program to abort. Extra arguments can be allowed for by adding a std::vector<std::string> to store them using the "set_string_vector" function. All extra (unnamed) arguments are placed there.
This software is not subject to copyright protection and is in the public domain. Neither Stanford nor the author assume any responsibility whatsoever for its use by other parties, and makes no guarantees, expressed or implied, about its quality, reliability, or any other characteristic.
An example using the class is:
#ifdef HAVE_CONFIG_H #include <config.h> #endif #include "Argument_helper.h" #include <iostream> std::string input_filename, output_filename; int iv, oiv, niv; int main(int argc, const char* argv[]){ dsr::Argument_helper ah; ah.new_string("input_filename.type", "The name of the input file", input_filename); ah.new_string("output_filename.type", "The name of the output file", output_filename); ah.new_int("count", "Some integer", iv); ah.new_optional_int("opt_count", "Some optional integer", oiv); ah.new_named_int('i', "integer", "named_int", "Some named integer", niv); ARGUMENT_HELPER_BASICS(ah); ah.set_description("A program"); ah.set_author("Daniel Russel, drussel@graphics.stanford.edu"); ah.process(argc, argv); //test(argv); ah.write_values(std::cout); return 0; }