#include <utilmm/configfile/commandline.hh>
Public Member Functions | |
command_line (const char *options[]) | |
command_line (const std::list< std::string > &description) | |
~command_line () | |
void | parse (int argc, char *const argv[], config_set &config) |
std::list< std::string > | remaining () const |
void | setBanner (std::string const &banner) |
void | usage (std::ostream &out) const |
The command_line class allows you to parse user-provided command line options and fill a Config object with them.
For each command line option, you must provide a long option (--option_text), may provide a short one (-option_character), and each option may have one (optional) argument. Eventually, you can give a help string (not used yet).
During the parsing, a key/value pair is added to a Config object for each option encountered. The value of the entry is either the option's argument or a boolean value of true if no option is specified.
The full syntax is
[!*][config_key]:long_name[,short_name][=value_type[,default]|?value_type,default][:help]
where value_type is one of: int, bool, string
When an option is found, an entry is added to a Config object with the config_key
key. The value associated is:
true
default
. default
is required in case of optional arguments
If default
is given, the option is set to default
if it is not found
If the option has a mandatory argument, add =value_type
after the option names. If it is optional, use the ?value_type syntax. The '
int'
and 'bool'
value types are checked by the command_line object and an error is generated if the user-provided value dos not match.
When the same option is provided more than once on the command line, the normal behaviour is to use the value of the latest. However, you can also get all the values by adding *
at the front of the description line. In that case, the config value will a list of values in the config object
For instance, an option like the -I
option of gcc
will be described using
*:include,I=string|include path
. The result of gcc -I /a/path -I=/another/path
can then be retrieved with list<string> includes = config.get< list<string> >('include');
If the ! flag is set, the option is required.
The classical --help option will be given using :help|display this help and exit
The -r
and --recursive
options of grep are described using :recursive,r|equivalent to --directories=recurse
The -m
and --max-count
options of grep are described using :max-count,m=int|stop after NUM matches
utilmm::command_line::command_line | ( | const char * | options[] | ) |
Builds an object with a null-terminated string list
options | the option list, null-terminated |
utilmm::command_line::command_line | ( | const std::list< std::string > & | description | ) |
utilmm::command_line::~command_line | ( | ) |
void utilmm::command_line::parse | ( | int | argc, | |
char *const | argv[], | |||
config_set & | config | |||
) |
Parses the command line option
argc | the argument count | |
argv | the argument value | |
config | the Config object the option values will be written to |
commandline_error |
std::list<std::string> utilmm::command_line::remaining | ( | ) | const |
Remaining command line options After all options are matched, and if no error has occured, this function will return all non-option arguments (input files for instance)
void utilmm::command_line::setBanner | ( | std::string const & | banner | ) |
Sets the first line to appear in usage()
void utilmm::command_line::usage | ( | std::ostream & | out | ) | const |
Outputs a help message to out