consoleOption - Kri-7-q/C-Library GitHub Wiki

Console Option Parser

A parser to get options from command line input. It is based on the GNU getopt. But it returns parsed options in an hash table. The whole code is written in C++ with the Qt framework.
It takes the arguments from applications main() function. The parameter from main() are directly passed to the parser.
If parsing has finished the parser returns a QHash<char, QVariant> map. These map contains all found options and their values. Values are converted to a given data type and stored in a QVariant object. All options which a application expects must be defined. To define options use class OptionDefinition. Create one definition for each option. There are different types of options. Some options can take values. Others do not accept any value or they do simply switch a boolean value to true. Pack those definitions in a QList and pass that list to the constructor of the option parser.
Some applications take free arguments which are not introduced from an option. The parser takes such free arguments and stores them in a string list. A QStringList object is inserted into the QHash map with the key character '?'. If you request the value with the key '?' you will get a QVariant object. This QVariant object contains the QStringList with free arguments if there were some given through console input.

OptionDefinition class

This class has three constructors. The standard constructor should not be used. It is needed by the parser. The two other constructor can create fully initialised option definitions.


OptionDefinition();
OptionDefinition(const char option, const OptionType optionArgument, const QVariant::Type dataType = QVariant::String, const QString& formatString = QString(), const QString& longOption = QString());
OptionDefinition(const char option, bool* pSwitch, const QString& longOption = QString());

Required constructor parameter:

  • option
    A single char value which represent the option. To parse an option like '-v' this value should be the character 'c'.

  • optionArgument
    This parameter can be one of three values.
    NeedArgument if option MUST take a value. Parser stops with an error if no value is given to that option.
    OptionalArgument if this option can take a value but do not need any value.
    NoArgument if option do not take any value. If this option has a value the parser stops with an error.

  • pSwitch
    A pointer to a boolean value. This boolean will be set to true if this option was found.

Optional parameter:

  • dataType
    Specifies a data type of a given value. Standard is QString if no other is set. The data type is one of QVariant types from the Qt framework.

  • formatString
    Some data types require a format string. The types QTime, QDate and QDateTime from Qt use format strings to parse a string for it.

  • longOption
    Each option can get a long name. They can be call from command line with the single character or with the long name. For instance the option '-f' can get a long name like 'file' with that name it can be call as '--file' from command line.

OptionParser class


bool verbose = false;

QList definitionList;
definitionList << OptionDefinition('a', NoArgument);
definitionList << OptionDefinition('l', OptionalArgument, QVariant::Int, QString(), QString("length"));
definitionList << OptionDefinition('w', NeedArgument, QVariant::Double);
definitionList << OptionDefinition('v', &verbose, QString("verbose"));
definitionList << OptionDefinition('t', NeedArgument, QVariant::Time, QString("h:mm"), QString("time"));

OptionParser parser(definitionList);
OptionTable optionTable = parser.parseParameter(argc, argv, 1);

The above code example can parse an input like:


appName -a -l12 -w 0.34129 --verbose -t 12:34
appName -al12 -w0.34129 -v -t12:34
appName -al 12 -w 0.34129 --time 12:34 --verbose
appName -val12 -w0.34129 --time=12:34

This will lead to a QHash<char, QVariant> map as follows:

Key Value
a ---> QVariant()
l ---> QVariant(12)
w ---> QVariant(0.34129)
t ---> QVariant(QTime(12:34))

and boolean value 'verbose' will be set to true.

Free arguments can appear any where in the command line input. But they should not follow an option which can take a value. In that case the argument is set to the option as option argument.


appName -a hallo -w 12.567 fritz -l34 henk --verbose  

In the above example are three free arguments. The strings 'hallo', 'fritz' and 'henk' are stored in a QStringList object. These string list object is wrapped from a QVariant and is to find in the OptionTable object (QHash). It can be retrieved with the hash key character '?'.

⚠️ **GitHub.com Fallback** ⚠️