A parser for command line arguments. More...
Public Member Functions | |
void | addHelpSwitch (String aKey, String aName, String aInfo) |
Adds a help-type switch with either a single character key and / or a key name. More... | |
void | addMandatory (String aName) |
Add a mandatory parameter. More... | |
void | addOption (String aKey, String aName, String aParam, String aInfo) |
Adds a option with either a single character key and / or a key name. More... | |
void | addOptional (String aName) |
Add a single optional parameter. More... | |
void | addOptionals (String aName) |
Add a multiple optional parameter. More... | |
void | addSwitch (String aKey, String aName, String aInfo) |
Adds a switch with either a single character key and / or a key name. More... | |
CmdlineParser () | |
The constructor. | |
QString | help (Number aWidth) |
Formats a help message for the command line parser. More... | |
String | mandatory (String aParam) |
Query a mandatory parameter after parsing the arguments. More... | |
String | option (String aKey, String aName) |
Query an option value after parsing the arguments. More... | |
String | optional (String aParam) |
Query an optional single parameter after parsing the arguments. More... | |
Array | optionals () |
Query the multiple optional parameter after parsing the arguments. More... | |
void | parse () |
Parses the command line. More... | |
String | program () |
Query the invoked program after parsing the arguments. More... | |
void | setDescription (String aDescription) |
Set the description what the program is supposed to do. More... | |
void | setProgram (String aName) |
Set the program name used by the help() funcion. More... | |
Boolean | switchState (String aKey, String aName) |
Query a switch after parsing the arguments. More... | |
A parser for command line arguments.
Specify the command line syntax with a minimum of statements in a readable way, check it against the actual command line arguments and find the retrieved values.
A command line that a user might have entered is:
The typical usage has three stages:
For the first step setting up the accepted syntax is done by a set of functions like setDescription() and addSwitch(). The final step of running the parser is simply done by calling parse().
A short example implementing a parser for the sample above:
For a better understanding of the function names we'll better define some terms used in the API and its documentation:
–debug
. –output=out.txt
or -I/usr/include
. Adds a help-type switch with either a single character key and / or a key name.
When this switch is found while parsing the arguments, the parsing will stop no check for missing mandatory parameters will be done.
aKey | The single character key. Enter 0 for none. |
aName | The name key. Enter an empty string for none. |
aInfo | A descriptive text for the switch to be displayed by the help() function. Paragraphs can be separated by a newline character. Running text in paragraphs is word-wrapped to the available line length, except for paragraphs starting with a blank which are output as-is for preformated text. |
void bps::CmdlineParser::addMandatory | ( | String | aName | ) |
Add a mandatory parameter.
An arbitrary number of mandatory parameters may be added. When parsing the arguments, the mandatory parameters will be served before any optional parameters, and in the order they were added.
aName | Name of the parameter. By convention this should be all uppercase. The name will be used again when querying the parse results later with mandatory(). |
Adds a option with either a single character key and / or a key name.
aKey | The single character key. Enter 0 for none. |
aName | The name key. Enter an empty string for none. |
aParam | A parameter name. |
aInfo | A descriptive text for the option to be displayed by the help() function. Paragraphs can be separated by a newline character. Running text in paragraphs is word-wrapped to the available line length, except for paragraphs starting with a blank which are output as-is for preformated text. |
void bps::CmdlineParser::addOptional | ( | String | aName | ) |
Add a single optional parameter.
An arbitrary number of single optional parameters may be added. When parsing the arguments, the single optional parameters will be served after the mandatory parameters, but before the multiple optional parameter, and in the order they were added.
aName | Name of the parameter. By convention this should be all uppercase. The name will be used again when querying the parse results later with optional(). |
void bps::CmdlineParser::addOptionals | ( | String | aName | ) |
Add a multiple optional parameter.
Only one multiple optional parameter may be added. When parsing the arguments, the multiple optional parameter will be served after the mandatory and after the single optional parameters.
aName | Name of the parameter. By convention this should be all uppercase. Enter an empty string if you want an invisible multi-optional parameter to silently eat up all supernumerary tokens. |
Adds a switch with either a single character key and / or a key name.
aKey | The single character key. Enter 0 for none. |
aName | The name key. Enter an empty string for none. |
aInfo | A descriptive text for the switch to be displayed by the help() function. Paragraphs can be separated by a newline character. Running text in paragraphs is word-wrapped to the available line length, except for paragraphs starting with a blank which are output as-is for preformated text. |
QString bps::CmdlineParser::help | ( | Number | aWidth | ) |
Formats a help message for the command line parser.
The message format is similar to the man pages on unix based operating systems:
aWidth | The maximum width of the message. Default is 79. |
Query a mandatory parameter after parsing the arguments.
aParam | The name of the parameter, exactly as defined with addMandatory(). |
Query an option value after parsing the arguments.
aKey | The single character key, exactly as defined with addSwitch(). |
aName | The name key, exactly as defined with addSwitch(). |
Query an optional single parameter after parsing the arguments.
aParam | The name of the parameter, exactly as defined with addOptional(). |
Array bps::CmdlineParser::optionals | ( | ) |
Query the multiple optional parameter after parsing the arguments.
void bps::CmdlineParser::parse | ( | ) |
Parses the command line.
In case of errors, a exception will be thrown. After parsing, the results can be collected by the functions program(), mandatory(), optional(), optionals(), switchState() and option().
String bps::CmdlineParser::program | ( | ) |
Query the invoked program after parsing the arguments.
void bps::CmdlineParser::setDescription | ( | String | aDescription | ) |
Set the description what the program is supposed to do.
You should also explain the parameters added by addMandatory(), addOptional() and addOptionals() in this description. By convention the names of those parameters are written in all uppercase.
Paragraphs can be separated by a newline character. Running text in paragraphs is word-wrapped to the available line length, except for paragraphs starting with a blank which are output as-is to enable preformating.
aDescription | The description to set. |
void bps::CmdlineParser::setProgram | ( | String | aName | ) |
Set the program name used by the help() funcion.
aName | The program name to set. |
Query a switch after parsing the arguments.
aKey | The single character key, exactly as defined with addSwitch(). |
aName | The name key, exactly as defined with addSwitch(). |