Public Member Functions | |
GetOpt () | |
GetOpt (int argc, char *argv[]) | |
GetOpt (const QStringList &a) | |
void | addSwitch (const QString &lname, bool *b) |
void | addOption (char s, const QString &l, QString *v) |
void | addVarLengthOption (const QString &l, QStringList *v) |
void | addRepeatableOption (char s, QStringList *v) |
void | addRepeatableOption (const QString &l, QStringList *v) |
void | addOptionalOption (const QString &l, QString *v, const QString &def) |
void | addOptionalOption (char s, const QString &l, QString *v, const QString &def) |
void | addArgument (const QString &name, QString *v) |
void | addOptionalArgument (const QString &name, QString *v) |
bool | parse () |
bool | isSet (const QString &name) const |
This class helps to overcome the repetitive, tedious and error-prone task of parsing the command line options passed to your application by the user. Specify the acceptable syntax with a minimum of statements in a readable way, check it against the actual arguments passed and find the retrieved values in variables of your program. The name GetOpt is based on similar utilities build into the Unix shell and other languages.
A command line that a user might have entered is:
app -v --config=my.cnf -Wall input.dat
The typical usage has three stages:
For the first step there are three different constructors that either take arguments directly from main()
, QApplication
or a user specified list. Setting up the accepted syntax is done by a set of add
functions like addSwitch(). The final step of running the parser is simply done by calling parse().
A short example implementing a --verbose
switch:
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
.-v
.--debug
.
|
Constructs a command line parser from the arguments stored in a previously created QApplication instance. Example usage:
This constructor is probably the most convenient one to use in a regular Qt application. Note that QApplication may already have removed Qt (or X11) specific arguments. Also see QApplication::argv() and QApplication::argc(). |
|
Construct a command line parser from the array argv of string pointers with the size argc. Those parameters have the form typically found in the Example usage:
|
|
Construct a command line parser from the arguments specified in the list of arguments a. This constructor is convenient in those cases where you want to parse a command line assembled on-the-fly instead of relying on the |
|
Registers a required command line argument name. If the argument is missing parse() will return false to indicate an error and *v will remain with its default QString::null value. Otherwise *v will be set to the value of the argument. Example: To accept simple arguments like
use a call like:
Note: the name parameter has a rather descriptive meaning for now. It might be used for generating a usage or error message in the future. Right now, the only current use is in relation with the isSet() function. |
|
Registers an option with the short name s and long name l to the parser. If this option is found during parsing the value will be stored in the string pointed to by v. By default *v will be initialized to |
|
Registers an optional command line argument name. For a more detailed description see the addArgument() documentation. |
|
Adds a short option s that has an optional value parameter. If the value is not specified by the user it will be set to def. |
|
Adds a long option l that has an optional value parameter. If the value is not specified by the user it will be set to def. Example:
|
|
Registers an option with the long name l that can be specified repeatedly in the command line.
|
|
Registers an option with the short name s that can be specified repeatedly in the command line. The option values will be stored in the list pointed to by v. If no s option is found *v will remain at its default value of an empty QStringList instance. Example:
To parse the
you can use code like this:
|
|
Adds a switch with the long name lname. If the switch is found during parsing the bool *b will bet set to true. Otherwise the bool will be initialized to false. Example:
The boolean flag |
|
Registers a long option l that can have a variable number of corresponding value parameters. As there currently is no way to tell the end of the value list the only sensible use of this option is at the end of the command line. Example:
Above code will lead to "-f" and "test.txt" being stored in args upon
|
|
Returns true if the (long) option or switch name has been found in the command line; returns false otherwise. Leading hyphens are not part of the name.
As the set/not set decision can also be made depending on the value of the variable reference used in the respective |
|
Parse the command line arguments specified in the constructor under the conditions set by the various
In the future there'll be a way to retrieve an error message. In the current version the message will be printed to |