#include <ProgOptions.hpp>
Public Member Functions | |
ProgOptions (const std::string &helptext="", const std::string &briefdesc="") | |
unimplemented flag for required arguments that may be given multiple times More... | |
~ProgOptions () | |
void | setVersion (const std::string &version_string, bool addFlag=true) |
template<typename T > | |
void | addOpt (const std::string &namestring, const std::string &helpstring, T *value, int flags=0) |
template<typename T > | |
void | addOpt (const std::string &namestring, const std::string &helpstring, int flags=0) |
void | addOptionHelpHeading (const std::string &) |
template<typename T > | |
void | addRequiredArg (const std::string &helpname, const std::string &helpstring, T *value=NULL, int flags=0) |
template<typename T > | |
void | addOptionalArgs (unsigned max_count, const std::string &helpname, const std::string &helpstring, int flags=0) |
void | printHelp (std::ostream &str=std::cout) |
void | printUsage (std::ostream &str=std::cout) |
void | printVersion (std::ostream &str=std::cout) |
void | parseCommandLine (int argc, char *argv[]) |
template<typename T > | |
bool | getOpt (const std::string &namestring, T *value) |
template<typename T > | |
void | getOptAllArgs (const std::string &namestring, std::vector< T > &values) |
int | numOptSet (const std::string &namestring) |
template<typename T > | |
T | getReqArg (const std::string &namestring) |
template<typename T > | |
void | getArgs (const std::string &namestring, std::vector< T > &values) |
void | error (const std::string &message) |
void | write_man_page (std::ostream &to_this_stream) |
Static Public Attributes | |
static const int | help_flag = 1 << 0 |
Set for a flag that, when detected, prints help text and halts program. Constructor creates such a flag by default, so the user shouldn't need to use this directly. More... | |
static const int | add_cancel_opt = 1 << 1 |
Flag indicating that an option should be given a "cancel" flag. This creates, for option –foo, an additional option –no-foo that clears all previously read instances of the foo option. More... | |
static const int | store_false = 1 << 2 |
When applied to a flag argument (one with template type void), indicate that the value 'false' should be stored into the pointer that was given at option creation time. This overrides the default behavior, which is to store the value 'true'. More... | |
static const int | int_flag = 1 << 3 |
Specify a numerical flag where any positive integer is an acceptable value. E.g. –dimension=3 is equivalent to -3. Only values in the range [0,9] are accepted and the flag type must be integer. More... | |
static const int | rank_subst = 1 << 4 |
static const int | version_flag = 1 << 5 |
Set for a flag that, when detected, will call printVersion() and halt the program. More... | |
Protected Types | |
typedef std::pair< ProgOpt *, std::string > | help_line |
Protected Member Functions | |
std::string | get_option_usage_prefix (const ProgOpt &option) |
void | get_namestrings (const std::string &input, std::string *l, std::string *s) |
ProgOpt * | lookup (const std::map< std::string, ProgOpt * > &, const std::string &) |
ProgOpt * | lookup_option (const std::string &) |
bool | evaluate (const ProgOpt &opt, void *target, const std::string &option, unsigned *arg_idx=NULL) |
bool | process_option (ProgOpt *opt, std::string arg, const char *value=0) |
Protected Attributes | |
std::map< std::string, ProgOpt * > | long_names |
std::map< std::string, ProgOpt * > | short_names |
std::map< std::string, ProgOpt * > | required_args |
std::vector< help_line > | option_help_strings |
std::vector< help_line > | arg_help_strings |
std::vector< std::string > | main_help |
std::string | brief_help |
bool | expect_optional_args |
unsigned | optional_args_position |
unsigned | max_optional_args |
std::string | progname |
std::string | progversion |
std::string | number_option_name |
A simple command-line option parser and help utility
Utility class to specify a program's command-line options arguments, produce a help message explaining how they work, and parse user's command line input (producing useful errors messages if any problems arise). Loosely (okay, very loosely) inspired by boost program_options.
Options are specified by a comma-separated namestring. An option named "foo,f" can be specified three ways on the command line: "-f val", "--foo val", or "--foo=val". The types of options and arguments are specified by function templates. Valid template values for positional argument and options are int, double, and std::string. void may also be used in options, and it indicates a command line option that does not take an argument.
Example usage: ProgOptions po( "Example usage of ProgOptions" ); po.addOpt<void>( "verbose,v", "Turn on verbose messages" ); po.addOpt<std::string> ("foo", "Specify the foo string" ); int x = 0; po.addOpt<int>( ",x", "Specify the x number", &x ); // x will be automatically set when options parsed po.parseCommandLine( argc, argv ); bool verbose = po.numOptSet("verbose") > 0; std::string foo; if( !po.getOpt( "foo", &foo ) ) foo = "default"; ...
See the file dagmc_preproc.cpp in the dagmc directory for a real-world example.
Definition at line 35 of file ProgOptions.hpp.
|
protected |
Definition at line 262 of file ProgOptions.hpp.
ProgOptions::ProgOptions | ( | const std::string & | helptext = "" , |
const std::string & | briefdesc = "" |
||
) |
unimplemented flag for required arguments that may be given multiple times
helptext | A brief summary of the program's function, to be printed when the help flag is detected |
Definition at line 94 of file ProgOptions.cpp.
References brief_help, help_flag, and main_help.
ProgOptions::~ProgOptions | ( | ) |
Definition at line 102 of file ProgOptions.cpp.
References arg_help_strings, and option_help_strings.
|
inline |
Specify a new command-line option
This funtion is identical to the 4-arg version, but omits the value parameter, which is assumed to be NULL
Definition at line 122 of file ProgOptions.hpp.
void ProgOptions::addOpt | ( | const std::string & | namestring, |
const std::string & | helpstring, | ||
T * | value, | ||
int | flags = 0 |
||
) |
Specify a new command-line option
Instruct the parser to accept a new command-line argument, as well as specifying how the argument should be handled. The template parameter indicates the type of command-line option being specified: acceptable types are void (indicating a flag without an argument), int, double, and std::string.
namestring | The command-line options name(s). Format is longname,shortname. If the comma is omitted, or appears only at the end, this option will have no shortname; if the comma is the first letter of the namestring, the option has no longname. |
helpstring | The help information displayed for the option when the program is invoked with –help |
value | A pointer to memory in which to store the parsed value for this option. If NULL, then the value of the option must be queried using the getOpt function. If the template parameter is void and value is non-NULL, treat value as a bool* and store 'true' into it when the flag is encountered. (See also store_false, above) |
flags | Option behavior flags, which should come from static vars in the ProgOptions class |
Definition at line 141 of file ProgOptions.cpp.
References add_cancel_opt, ProgOpt::cancel_opt, error(), FLAG, get_namestrings(), help(), INT, int_flag, long_names, number_option_name, option_help_strings, short_names, ProgOpt::storage, and store_false.
Referenced by main(), RuntimeContext::ParseCLOptions(), and ToolContext::ParseCLOptions().
void ProgOptions::addOptionalArgs | ( | unsigned | max_count, |
const std::string & | helpname, | ||
const std::string & | helpstring, | ||
int | flags = 0 |
||
) |
Add optional positional arguments
Specify location in ordered argument list at which optional arguments may occur. Optional arguments are allowed at only one location it argument list (this function may not be called more than once.). The template parameter may be int, double, or std::string (but not void)
count | The maximum number of optional arguments. Specify zero for unlimited. |
helpname | The name to give the argument in the help text |
helpstring | The help text for the arguments |
Definition at line 192 of file ProgOptions.cpp.
References arg_help_strings, expect_optional_args, max_optional_args, optional_args_position, and required_args.
void ProgOptions::addOptionHelpHeading | ( | const std::string & | s | ) |
Add a new line of help text to the option help printout
Add a line of text to the option-related help. Called between calls to addOpt(), this function can be used to divide the option list into groups of related options to make the help text more convenient.
Definition at line 216 of file ProgOptions.cpp.
References option_help_strings.
Referenced by main().
void ProgOptions::addRequiredArg | ( | const std::string & | helpname, |
const std::string & | helpstring, | ||
T * | value = NULL , |
||
int | flags = 0 |
||
) |
Add required positional argument
Add a new required positional argument. The order in which arguments are specified is the order in which they will be expected on the command line. The template parameter may be int, double, or std::string (but not void)
helpname | The name to give the argument in the help text |
helpstring | The help text for the argument |
value | Pointer to where parsed value from command line should be stored. If NULL, the value must be queried using getReqArg() |
Definition at line 179 of file ProgOptions.cpp.
References arg_help_strings, help(), required_args, and ProgOpt::storage.
Referenced by main().
void ProgOptions::error | ( | const std::string & | message | ) |
Prints an error message to std::cerr, along with a brief usage message, then halts the program. Used throughout ProgramOptions implementation. Users may call this directly if they detect an incorrect usage of program options that the ProgramOptions wasn't able to detect itself.
message | The error message to print before program halt. |
Definition at line 396 of file ProgOptions.cpp.
References printUsage().
Referenced by addOpt(), evaluate(), getArgs(), getOpt(), getOptAllArgs(), getReqArg(), lookup_option(), main(), numOptSet(), parseCommandLine(), and process_option().
|
protected |
Check the input to a given option for correctness, converting it to its expected type (e.g. int) and storing the result to target, if target is non-NULL.
option | Used only in error messages to state which option could not be successfully converted |
arg_idx | If non-NULL, evaluate the (*arg_idx)'th item in opt's args list |
Definition at line 501 of file ProgOptions.cpp.
References ProgOpt::args, do_rank_subst(), error(), FLAG, ProgOpt::flags, INT, INT_VECT, parse_int_list(), rank_subst, REAL, STRING, and ProgOpt::type.
Referenced by getArgs(), getOpt(), getOptAllArgs(), getReqArg(), parseCommandLine(), and process_option().
|
protected |
Definition at line 118 of file ProgOptions.cpp.
Referenced by addOpt(), lookup_option(), and numOptSet().
|
protected |
Definition at line 309 of file ProgOptions.cpp.
References ProgOpt::flags, ProgOpt::get_argstring(), int_flag, ProgOpt::longname, and ProgOpt::shortname.
Referenced by printHelp().
void ProgOptions::getArgs | ( | const std::string & | namestring, |
std::vector< T > & | values | ||
) |
Append the values of any required or optional arguments
namestring | The helpname that was given to addRequiredArg or addOptionalArgs. |
Definition at line 655 of file ProgOptions.cpp.
References ProgOpt::args, error(), evaluate(), lookup(), required_args, and ProgOpt::type.
bool ProgOptions::getOpt | ( | const std::string & | namestring, |
T * | value | ||
) |
Get the value of the named option.
namestring | The name string given when the option was created. This need not be idential to the created name; only the longname, or the shortname (with comma prefix), will also work. |
value | Pointer to location to store option argument, if any is found |
Definition at line 571 of file ProgOptions.cpp.
References ProgOpt::args, error(), evaluate(), lookup_option(), and ProgOpt::type.
Referenced by main().
void ProgOptions::getOptAllArgs | ( | const std::string & | namestring, |
std::vector< T > & | values | ||
) |
Get a list of values for the named option– one value for each time it was given on the command line.
This function cannot be called with void as the template parameter; compilers will reject vector<void> as a type. This means it cannot be called for flag-type options. To count the number of times a given flag was specified, use numOptSet()
namestring | See similar argument to getOpt() |
values | Reference to list to store values into. Will have as many entries as there were instances of this option on the command line |
Definition at line 592 of file ProgOptions.cpp.
References ProgOpt::args, error(), evaluate(), INT, INT_VECT, lookup_option(), and ProgOpt::type.
Referenced by main(), and ToolContext::ParseCLOptions().
T ProgOptions::getReqArg | ( | const std::string & | namestring | ) |
Retrieve the value of a required command-line argument by name
namestring | The helpname that was given to addRequiredArg when the desired argument was created |
Definition at line 637 of file ProgOptions.cpp.
References error(), evaluate(), lookup(), and required_args.
Referenced by main().
|
protected |
Definition at line 368 of file ProgOptions.cpp.
References long_names, number_option_name, and short_names.
Referenced by getArgs(), getReqArg(), lookup_option(), numOptSet(), and parseCommandLine().
|
protected |
Definition at line 380 of file ProgOptions.cpp.
References error(), get_namestrings(), long_names, lookup(), and short_names.
Referenced by getOpt(), and getOptAllArgs().
int ProgOptions::numOptSet | ( | const std::string & | namestring | ) |
namestring | See similar argument to getOpt() |
Definition at line 620 of file ProgOptions.cpp.
References ProgOpt::args, error(), get_namestrings(), long_names, lookup(), and short_names.
Referenced by main(), and ToolContext::ParseCLOptions().
void ProgOptions::parseCommandLine | ( | int | argc, |
char * | argv[] | ||
) |
Parse command-line inputs as given to main()
Definition at line 737 of file ProgOptions.cpp.
References arg_help_strings, ProgOpt::args, error(), evaluate(), expect_optional_args, FLAG, ProgOpt::flags, int_flag, long_names, lookup(), max_optional_args, optional_args_position, process_option(), progname, required_args, short_names, ProgOpt::storage, and ProgOpt::type.
Referenced by main(), RuntimeContext::ParseCLOptions(), and ToolContext::ParseCLOptions().
void ProgOptions::printHelp | ( | std::ostream & | str = std::cout | ) |
Print the full help to the given stream
Definition at line 226 of file ProgOptions.cpp.
References arg_help_strings, brief_help, get_option_usage_prefix(), ProgOpt::longname, main_help, option_help_strings, and printUsage().
Referenced by main(), and process_option().
void ProgOptions::printUsage | ( | std::ostream & | str = std::cout | ) |
Print only the usage message to the given stream
Definition at line 347 of file ProgOptions.cpp.
References arg_help_strings, expect_optional_args, moab::GeomUtil::first(), max_optional_args, optional_args_position, and progname.
Referenced by error(), and printHelp().
void ProgOptions::printVersion | ( | std::ostream & | str = std::cout | ) |
Print the version string to the given stream
Definition at line 221 of file ProgOptions.cpp.
References progversion.
Referenced by process_option().
|
protected |
Definition at line 683 of file ProgOptions.cpp.
References ProgOpt::args, ProgOpt::cancel_opt, error(), evaluate(), FLAG, ProgOpt::flags, help_flag, printHelp(), printVersion(), ProgOpt::storage, store_false, ProgOpt::type, version_flag, and write_man_page().
Referenced by parseCommandLine().
void ProgOptions::setVersion | ( | const std::string & | version_string, |
bool | addFlag = true |
||
) |
Specify the program version
Set the program version to a given string. This will be printed when printVersion() is called.
version_string | The version string |
addflag | If true, a default '–version' option will be added. If false, the version will be set, but no option will be added to the parser. |
Definition at line 131 of file ProgOptions.cpp.
References progversion, and version_flag.
void ProgOptions::write_man_page | ( | std::ostream & | to_this_stream | ) |
Write help data formatted for use as a unix man page.
Definition at line 876 of file ProgOptions.cpp.
References arg_help_strings, brief_help, expect_optional_args, FLAG, int_flag, main_help, max_optional_args, option_help_strings, optional_args_position, and progname.
Referenced by process_option().
|
static |
Flag indicating that an option should be given a "cancel" flag. This creates, for option –foo, an additional option –no-foo that clears all previously read instances of the foo option.
Definition at line 51 of file ProgOptions.hpp.
|
protected |
Definition at line 264 of file ProgOptions.hpp.
Referenced by addOptionalArgs(), addRequiredArg(), parseCommandLine(), printHelp(), printUsage(), write_man_page(), and ~ProgOptions().
|
protected |
Definition at line 266 of file ProgOptions.hpp.
Referenced by printHelp(), ProgOptions(), and write_man_page().
|
protected |
Definition at line 268 of file ProgOptions.hpp.
Referenced by addOptionalArgs(), parseCommandLine(), printUsage(), and write_man_page().
|
static |
Set for a flag that, when detected, prints help text and halts program. Constructor creates such a flag by default, so the user shouldn't need to use this directly.
Flags for addOpt and addRequiredArg functions; may be combined with bitwise arithmetic (though not all combinations make sense!)
Definition at line 46 of file ProgOptions.hpp.
Referenced by process_option(), and ProgOptions().
|
static |
Specify a numerical flag where any positive integer is an acceptable value. E.g. –dimension=3 is equivalent to -3. Only values in the range [0,9] are accepted and the flag type must be integer.
Definition at line 61 of file ProgOptions.hpp.
Referenced by addOpt(), get_option_usage_prefix(), main(), parseCommandLine(), and write_man_page().
|
protected |
Definition at line 258 of file ProgOptions.hpp.
Referenced by addOpt(), lookup(), lookup_option(), numOptSet(), and parseCommandLine().
|
protected |
Definition at line 265 of file ProgOptions.hpp.
Referenced by printHelp(), ProgOptions(), and write_man_page().
|
protected |
Definition at line 269 of file ProgOptions.hpp.
Referenced by addOptionalArgs(), parseCommandLine(), printUsage(), and write_man_page().
|
protected |
Definition at line 276 of file ProgOptions.hpp.
|
protected |
Definition at line 263 of file ProgOptions.hpp.
Referenced by addOpt(), addOptionHelpHeading(), printHelp(), write_man_page(), and ~ProgOptions().
|
protected |
Definition at line 269 of file ProgOptions.hpp.
Referenced by addOptionalArgs(), parseCommandLine(), printUsage(), and write_man_page().
|
protected |
Definition at line 271 of file ProgOptions.hpp.
Referenced by parseCommandLine(), printUsage(), and write_man_page().
|
protected |
Definition at line 272 of file ProgOptions.hpp.
Referenced by printVersion(), and setVersion().
|
static |
Substitue any occurance of the '' symbol in a string with the the MPI rank of this process in MPI_COMM_WORLD. This option has no effect if not compiled with MPI. This flag has no effect for non-string options.
Definition at line 68 of file ProgOptions.hpp.
Referenced by evaluate().
|
protected |
Definition at line 260 of file ProgOptions.hpp.
Referenced by addOptionalArgs(), addRequiredArg(), getArgs(), getReqArg(), and parseCommandLine().
|
protected |
Definition at line 259 of file ProgOptions.hpp.
Referenced by addOpt(), lookup(), lookup_option(), numOptSet(), and parseCommandLine().
|
static |
When applied to a flag argument (one with template type void), indicate that the value 'false' should be stored into the pointer that was given at option creation time. This overrides the default behavior, which is to store the value 'true'.
Definition at line 56 of file ProgOptions.hpp.
Referenced by addOpt(), and process_option().
|
static |
Set for a flag that, when detected, will call printVersion() and halt the program.
Definition at line 71 of file ProgOptions.hpp.
Referenced by process_option(), and setVersion().