Trade equipment common library. 1.0.0
GetOpt Class Reference

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
 

Detailed Description

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:

app -v --config=my.cnf -Wall input.dat

The typical usage has three stages:

  1. Construct a parser specifying what arguments to parse
  2. Set up the list of allowed and required options
  3. Run the parser

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:

int main(int argc, char **argv)
{
GetOpt opts(argc, argv);
bool verbose;
opts.addSwitch("verbose", &verbose);
if (!opts.parse())
return 1;
if (verbose)
cout << "VERBOSE mode on" << endl;
...
GetOpt()
Definition qgetopt.cpp:94

For a better understanding of the function names we'll better define some terms used in the API and its documentation:

  • Argument An argument is a plain text token like e.g. a file name one typically passes to an editor when invoking it.
  • Switch A switch is an on/off kind of argument without the need of additional information. Example: --debug.
  • Option An option is a normally optional argument with a key-value syntax like --output=out.txt or -I/usr/include.
  • Short Option A short option is a one letter option with a preceding dash. Like -v.
  • Long Option A long option has a more verbose, multi-letter name like --debug.
Author
froglogic GbR conta.nosp@m.ct@f.nosp@m.roglo.nosp@m.gic..nosp@m.com

Constructor & Destructor Documentation

◆ GetOpt() [1/3]

GetOpt::GetOpt ( )

Constructs a command line parser from the arguments stored in a previously created QApplication instance.

Example usage:

QApplication a(argc, argv);
GetOpt opt;

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() [2/3]

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:

int main(int argc, char **argv) {
GetOpt opt(argc, argv);
...
}

◆ GetOpt() [3/3]

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.

Member Function Documentation

◆ addArgument()

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

myeditor letter.txt

use a call like:

QString &file;
opt.addArgument("file", &file);

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.

◆ addOption()

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().

◆ addOptionalArgument()

void GetOpt::addOptionalArgument ( const QString & name,
QString * v )

Registers an optional command line argument name. For a more detailed description see the addArgument() documentation.

◆ addOptionalOption() [1/2]

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().

◆ addOptionalOption() [2/2]

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:

GetOpt opt;
QString file;
opt.addOptionalOption("dump", &file, "<stdout>");
void addOptionalOption(const QString &l, QString *v, const QString &def)
Definition qgetopt.cpp:538
See also
addOption

References addOptionalOption().

Referenced by addOptionalOption().

◆ addRepeatableOption() [1/2]

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

myapp -I/usr/include -I/usr/local/include

you can use code like this:

GetOpt opt;
QStringList includes;
opt.addRepeatableOption('I', &includes);
opt.parse();
void addRepeatableOption(char s, QStringList *v)
Definition qgetopt.cpp:502

References addOption().

◆ addRepeatableOption() [2/2]

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.

See also
addRepeatableOption( char, QStringList* )

References addOption().

◆ addSwitch()

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:

GetOpt opt;
bool verbose;
opt.addSwitch("verbose", &verbose);
void addSwitch(const QString &lname, bool *b)
Definition qgetopt.cpp:420

The boolean flag verbose will be set to true if --verbose has been specified in the command line; false otherwise.

References addOption().

◆ addVarLengthOption()

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:

QStringList args;
opt.addVarLengthOption("exec", &args);

Above code will lead to "-f" and "test.txt" being stored in args upon

myapp --exec otherapp -f test.txt

References addOption().

◆ isSet()

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.

◆ parse()

bool GetOpt::parse ( )
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().


The documentation for this class was generated from the following files: