. 1.0.0
Открытые члены | Друзья | Полный список членов класса
Класс GetOpt

A command line option parser. Подробнее...

Открытые члены

 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
 

Друзья

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:

app -v --config=my.cnf -Wall input.dat
Definition conv.h:259

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;
...
A command line option parser.
Definition qgetopt.h:45

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

Автор
froglogic GbR conta.nosp@m.ct@f.nosp@m.roglo.nosp@m.gic..nosp@m.com

Конструктор(ы)

◆ GetOpt() [1/3]

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

Методы

◆ 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

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.

Перекрестные ссылки addOption().

Используется в addOption(), addOptionalOption(), addRepeatableOption(), addRepeatableOption(), addSwitch() и 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.

Перекрестные ссылки 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:

QString file;
opt.addOptionalOption("dump", &file, "<stdout>");
См. также
addOption

Перекрестные ссылки addOptionalOption().

Используется в 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

you can use code like this:

opt.addRepeatableOption('I', &includes);
opt.parse();

Перекрестные ссылки 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.

См. также
addRepeatableOption( char, QStringList* )

Перекрестные ссылки 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:

bool verbose;
opt.addSwitch("verbose", &verbose);

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

Перекрестные ссылки 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:

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

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

Перекрестные ссылки 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.

Перекрестные ссылки parse().

Используется в parse().


Объявления и описания членов классов находятся в файлах: