Program Options LITE base class. The Polite class provides a wrapper over boost::program_options to make life a bit easier. Functionality includes parsing the default –help and –version options, and access to positional parameters. Apps using this are supposed to subclass Polite and add their own extra options.
More...
#include "polite.hh"
|
| Polite (const std::string &descr="Options") |
|
template<typename Opttype > |
void | add_option (const std::string &name, Opttype *optvarp, const Opttype &defval, const std::string &descr, const char nm= '\0') |
|
void | add_bool_switch (const std::string &name, bool *varp, const std::string &descr, const char nm= '\0') |
|
template<typename Opttype > |
void | add_mandatory_option (const std::string &name, const std::string &descr, const char nm= '\0') |
|
bool | perform_parsing (int argc, char *argv[]) |
|
virtual bool | check_variables ()=0 |
|
bool | option_seen (const std::string &optnm) const |
|
template<typename Opttype > |
Opttype | fetch_value (const std::string &optnm) const |
|
void | add_error (const std::string &msg) |
|
void | error_reset () |
| Reset the error status to "no error".
|
|
virtual std::ostream & | version_info (std::ostream &out) const |
|
Program Options LITE base class. The Polite class provides a wrapper over boost::program_options to make life a bit easier. Functionality includes parsing the default –help and –version options, and access to positional parameters. Apps using this are supposed to subclass Polite and add their own extra options.
multovl::Polite::Polite |
( |
const std::string & |
descr = "Options" | ) |
|
|
explicitprotected |
Inits with a single option "--help" or "-h" and a single positional option. For simplicity's sake, the positionals are std::strings only, the name is fixed to "posopts", and they can be anywhere after the "real" options.
- Parameters
-
descr | the overall description of the visible options |
void multovl::Polite::add_bool_switch |
( |
const std::string & |
name, |
|
|
bool * |
varp, |
|
|
const std::string & |
descr, |
|
|
const char |
nm = '\0' |
|
) |
| |
|
protected |
Add a non-mandatory Boolean switch option.
- Parameters
-
name | the 'long' option name like "--opt" |
varp | pointer to the data member storing the option value |
descr | the description of the option |
nm | the 'short' option name like "-o", can be omitted |
void multovl::Polite::add_error |
( |
const std::string & |
msg | ) |
|
|
protected |
Adds an error message, sets error status. This method may be called more than once during parsing, the error messages will be "remembered".
- Parameters
-
msg | a descriptive error message. |
template<typename Opttype >
void multovl::Polite::add_mandatory_option |
( |
const std::string & |
name, |
|
|
const std::string & |
descr, |
|
|
const char |
nm = '\0' |
|
) |
| |
|
inlineprotected |
Add a mandatory option. Derived classes should have members for each option variable, and then invoke this method in their constructors to set them up.
- Parameters
-
name | the 'long' option name like "--opt" |
descr | the description of the option |
nm | the 'short' option name like "-o", can be omitted |
template<typename Opttype >
void multovl::Polite::add_option |
( |
const std::string & |
name, |
|
|
Opttype * |
optvarp, |
|
|
const Opttype & |
defval, |
|
|
const std::string & |
descr, |
|
|
const char |
nm = '\0' |
|
) |
| |
|
inlineprotected |
Add a non-mandatory option. Derived classes should have members for each option variable, and then invoke this method in their constructors to set them up.
- Parameters
-
name | the 'long' option name like "--opt" |
optvarp | pointer to the data member storing the option value |
defval | default value for the option |
descr | the description of the option |
nm | the 'short' option name like "-o", can be omitted |
virtual bool multovl::Polite::check_variables |
( |
| ) |
|
|
protectedpure virtual |
Derived classes must override this method so that first an appropriate base class version is invoked to process the base class' commandline variables and then additional processing must be done on the derived class' variables if /true/ was returned, like this:-
bool DerivedOpts::check_variables()
{
bool ok = BaseOpts::check_variables(); // don't invoke Polite::check_variables() !
if (ok) {
// e.g. check some variables here
if (option_seen("opt") > 0)
{
int opt = fetch_value<int>("opt");
if (opt < 1) opt = 1;
// ...
ok = true;
}
else
{
add_error("opt must be present");
ok = false;
}
}
return ok;
}
Implemented in multovl::prob::ProbOpts, multovl::ClassicOpts, multovl::MultovlOptbase, and multovl::prob::ParProbOpts.
std::string multovl::Polite::error_messages |
( |
| ) |
const |
|
inline |
Error messages
- Returns
- all error messages as a multiline std::string each line contains one message added with the (protected) add_error(msg) method.
bool multovl::Polite::error_status |
( |
| ) |
const |
|
inline |
Error status
- Returns
- /true/ if at least one error has been seen during parsing.
template<typename Opttype >
Opttype multovl::Polite::fetch_value |
( |
const std::string & |
optnm | ) |
const |
|
inlineprotected |
Fetches the value of an option.
- Parameters
-
optnm | the name of the option. |
- Returns
- the value of the option.
bool multovl::Polite::help_needed |
( |
| ) |
const |
|
inline |
Indicates whether the help should be printed.
- Returns
- /true/ if the "--help" or "-h" option has seen, or if error(s) occurred.
bool multovl::Polite::option_seen |
( |
const std::string & |
optnm | ) |
const |
|
inlineprotected |
Checks whether an option has been seen.
- Parameters
-
optnm | the name of the option. |
- Returns
- true if the option has been seen on the command line.
bool multovl::Polite::parse_check |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Parses the command line and checks the variables. the number of commandline arguments as provided by main() the commandline arguments in a C std::string array as provided by main()
- Returns
- /true/ on success, /false/ on error. Upon success, you may access the variables' values.
bool multovl::Polite::perform_parsing |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
|
protected |
Actually parses the command line. NOTE: You may invoke this method only once, because otherwise strange things happen (seem to be a "feature" of boost::program_options).
- Parameters
-
argc | the length of the argument array, must be >=1 |
argv | the arguments, argv[0] is the program name |
- Returns
- /true/ on success, /false/ on error, including repeated parsing.
std::vector<std::string> multovl::Polite::pos_opts |
( |
| ) |
const |
Returns the positional options as a std::string vector.
- Returns
- a std::string vector or an empty vector if there were no arguments.
virtual std::ostream& multovl::Polite::print_help |
( |
std::ostream & |
out | ) |
const |
|
virtual |
virtual std::ostream& multovl::Polite::print_version |
( |
std::ostream & |
out | ) |
const |
|
inlinevirtual |
Prints the version information
- Parameters
-
out | this is the stream the version text is printed to. |
- Returns
- /out/ is returned here
Reimplemented in multovl::MultovlOptbase.
void multovl::Polite::process_commandline |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Convenience method to process all command line arguments at once. This method invokes parse_check(argc, argv) and then does the following:- If version information was requested, invokes print_version() and then invokes exit(EXIT_SUCCESS). If help was requested, invokes print_help(std::cout) and then exit(EXIT_SUCCESS). If parsing fails, then the error messages are printed to std::cerr prefixed with "ERROR: ", then print_help(std::cerr) is invoked, then exit(EXIT_FAILURE) is invoked. If parsing was successful, then the method returns normally and the program can continue. the number of commandline arguments as provided by main() the commandline arguments in a C std::string array as provided by main()
virtual std::ostream& multovl::Polite::version_info |
( |
std::ostream & |
out | ) |
const |
|
protectedvirtual |
This method prints version information relevant to the current class. It is invoked by the print_version() method by default. Do not invoke the base class version(s) here, just specify what is relevant to your current class, and override print_version() so that the base class infos are combined correctly (important when doing diamond-style inheritance).
- Parameters
-
out | the stream the version information is printed to |
- Returns
- out
Reimplemented in multovl::MultovlOptbase.
bool multovl::Polite::version_needed |
( |
| ) |
const |
|
inline |
Indicates whether version information should be printed.
- Returns
- /true/ if the "--version" option has seen.
The documentation for this class was generated from the following file: