Trade equipment common library. 1.0.0
|
A command line option parser. More...
Public Member Functions | |
GetOpt () | |
GetOpt (int offset) | |
GetOpt (int argc, char *argv[]) | |
GetOpt (const QStringList &a) | |
QString | appName () const |
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 untilFirstSwitchOnly) |
bool | parse () |
bool | isSet (const QString &name) const |
int | currentArgument () const |
Friends | |
struct | Option |
A command line option parser.
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:
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
.GetOpt::GetOpt | ( | ) |
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().
GetOpt::GetOpt | ( | int | argc, |
char * | argv[] ) |
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 main()
function. That means that you can simply pass on the arguments specified by the user of your application.
Example usage:
GetOpt::GetOpt | ( | const QStringList & | a | ) |
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 argc
and arg
parameters passed to the main()
function.
void GetOpt::addArgument | ( | const QString & | name, |
QString * | v ) |
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.
void GetOpt::addOption | ( | char | s, |
const QString & | l, | ||
QString * | v ) |
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 QString::null
.
References addOption().
Referenced by addOption(), addOptionalOption(), addRepeatableOption(), addRepeatableOption(), addSwitch(), and addVarLengthOption().
void GetOpt::addOptionalArgument | ( | const QString & | name, |
QString * | v ) |
Registers an optional command line argument name. For a more detailed description see the addArgument() documentation.
void GetOpt::addOptionalOption | ( | char | s, |
const QString & | l, | ||
QString * | v, | ||
const QString & | def ) |
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.
References addOption().
void GetOpt::addOptionalOption | ( | const QString & | l, |
QString * | v, | ||
const QString & | 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:
References addOptionalOption().
Referenced by addOptionalOption().
void GetOpt::addRepeatableOption | ( | char | s, |
QStringList * | v ) |
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 -I
options in a command line like
you can use code like this:
References addOption().
void GetOpt::addRepeatableOption | ( | const QString & | l, |
QStringList * | v ) |
Registers an option with the long name l that can be specified repeatedly in the command line.
References addOption().
void GetOpt::addSwitch | ( | const QString & | lname, |
bool * | b ) |
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 verbose
will be set to true if --verbose
has been specified in the command line; false otherwise.
References addOption().
void GetOpt::addVarLengthOption | ( | const QString & | l, |
QStringList * | v ) |
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
References addOption().
bool GetOpt::isSet | ( | const QString & | name | ) | const |
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 add*
() call there's generally little use for this function.
|
inline |
Parse the command line arguments specified in the constructor under the conditions set by the various add*
() functions. On success, the given variable reference will be initialized with their respective values and true will be returned. Returns false otherwise.
In the future there'll be a way to retrieve an error message. In the current version the message will be printed to stderr
.
References parse().
Referenced by parse().