MLIB
|
Command Line Parsing class. More...
Public Member Functions | |
OptParser () | |
Initialize parser. | |
OptParser (std::vector< const char * > &list) | |
Initializes parser and sets the list of valid options. | |
OptParser (std::initializer_list< const char * > list) | |
Initializes parser and sets the list of valid options. | |
OptParser (const char **list) | |
Initializes parser and sets the list of options descriptors. | |
void | set_options (std::vector< const char * > &list) |
Set list of valid options. | |
void | add_option (const char *descr) |
Add a new option descriptor to the list of options. | |
int | parse (int argc, const char *const *argv, int *stop=0) |
Parse a command line. | |
const std::string | synopsis () const |
Return a nicely formatted syntax string containing all the options. | |
const std::string | description (size_t indent_size=2) const |
Return options description. | |
const std::string & | appname () const |
Return program name. | |
bool | next (std::string &opt, std::string &optarg, char sep='|') |
bool | next (std::string &opt, std::vector< std::string > &optarg) |
Return next option in command line. | |
int | getopt (const std::string &option, std::string &optarg, char sep='|') const |
int | getopt (const std::string &option, std::vector< std::string > &optarg) const |
Return a specific option from the command line. | |
int | getopt (char option, std::string &optarg, char sep='|') const |
Return a specific option from the command line. | |
int | getopt (char option, std::vector< std::string > &optarg) const |
Return a specific option from the command line. | |
bool | hasopt (const std::string &option) const |
bool | hasopt (char option) const |
Check if command line has an option. | |
Command Line Parsing class.
The parser matches approximately the POSIX specifications from: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html and GNU Standard for Command line Interface at http://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html
It accepts either short options composed of a dash (-
) followed by a letter, or long options composed of --
followed by the option name. An option can have arguments that are either required or optional. If an option can have multiple arguments, all program arguments following the option are considered option arguments until the next option argument (argument preceded by -
or --
) or until the last argument. The first --
argument stops options' processing. All remaining arguments are considered non-option arguments (operands).
It is possible to specify several short options after one -
, as long as all (except possibly the last) do not have required or optional arguments.
An option can be repeated. In this case, if the option has arguments, they are concatenated. For instance, the following two command lines are equivalent:
and
See OptParser::add_option() function for the syntax of the option descriptor.
The following example shows how to use an OptParser object and the different formats available for options.
mlib::OptParser::OptParser | ( | std::vector< const char * > & | list | ) |
Initializes parser and sets the list of valid options.
list | array of option descriptor strings |
mlib::OptParser::OptParser | ( | std::initializer_list< const char * > | list | ) |
Initializes parser and sets the list of valid options.
list | array of option descriptor strings |
mlib::OptParser::OptParser | ( | const char ** | list | ) |
Initializes parser and sets the list of options descriptors.
list | array of pointers to option descriptor strings |
void mlib::OptParser::add_option | ( | const char * | descr | ) |
Add a new option descriptor to the list of options.
descr | option descriptor string |
The descriptor string is has the following syntax:
<short_form>
is a letter giving the short form of the option.
flag
is one of:
?
option has one optional parameter:
option requires one argument+
option has one or more arguments*
option has 0 or more arguments|
option doesn't have any arguments<long_form>
is the long form of the option.
The <arg_description>
is the argument description and is used to generate the synopsis string. The <opt_description>
is the option description and is used to generate the description string. The <opt_description>
part is preceded by a TAB (\t
) character
Example:
"l?list list_arg \t list something"
The option syntax part is l?list
, the argument description is list_arg
and the option description is list something
. The option can appear on the command line as:
or
or, simply:
because the argument is optional.
The synopsis string will include the fragment:
and the description string will include the line:
|
inline |
Return program name.
This is the content of argv[0]
without any path or extension.
const std::string mlib::OptParser::description | ( | size_t | indent_size = 2 | ) | const |
Return options description.
indent_size | number of spaces to indent each option description line |
Each option and its synopsis is shown on a separate line indented by indent_size
spaces, followed by the option description (if any).
|
inline |
Return a specific option from the command line.
option | the requested option |
optarg | option argument(s) |
int mlib::OptParser::getopt | ( | const std::string & | option, |
std::string & | optarg, | ||
char | sep = '|' |
||
) | const |
Return a specific option from the command line
option | the requested option |
optarg | option argument(s) |
sep | separator character to insert between arguments if option has multiple arguments |
If the option has multiple arguments, optarg
contains the arguments separated by separator character.
|
inline |
Return a specific option from the command line.
option | the requested option |
optarg | option argument(s) |
|
inline |
Check if command line has an option.
option | short form of the option |
|
inline |
Check if command line has an option
option | long or short form of the option |
bool mlib::OptParser::next | ( | std::string & | opt, |
std::string & | optarg, | ||
char | sep = '|' |
||
) |
Return next option in command line
opt | option |
optarg | option argument(s) |
sep | separator character to insert between arguments if option has multiple arguments |
true
if successful, false
if there are no more optionsThe internal position counter is initialized to the first option when command line is parsed and is incremented after each call to this function. Note that the internal counter is not thread-safe. Calls to next()
function from any thread increment the same counter.
If the next option has both a long form and a short form, the function returns the long form.
If the option has multiple arguments, optarg
contains the arguments separated by separator character.
bool mlib::OptParser::next | ( | std::string & | opt, |
std::vector< std::string > & | optarg | ||
) |
Return next option in command line.
opt | option |
optarg | option argument(s) |
true
if successful, false
if there are no more optionsThe internal position counter is initialized to the first option when command line is parsed and is incremented after each call to next() function. Note that the internal counter is not thread-safe. Calls to next()
function from any thread increment the same counter.
If the next option has both a long form and a short form, the function returns the long form.
int mlib::OptParser::parse | ( | int | argc, |
const char *const * | argv, | ||
int * | stop = 0 |
||
) |
Parse a command line.
argc | number of arguments |
argv | array of arguments |
stop | pointer to an integer that receives the index of the first non-option argument if function is successful, or the index of the invalid option in case of failure |
void mlib::OptParser::set_options | ( | std::vector< const char * > & | list | ) |
Set list of valid options.
list | - array of option descriptor strings |
Each valid option is described by a string in the list array.