![]() |
CopperSpice API
1.8.1
|
Provides a way to process command line options. More...
Public Types | |
enum | SingleDashWordOptionMode |
The QCommandLineParser class provides a way to process command line options.
QCoreApplication provides the command line arguments as a simple list of strings. QCommandLineParser provides the ability to define a set of options, parse the command-line arguments, and store which options have actually been used, as well as option values.
Any argument that does not start with a dash is stored as a "positional argument". The parser handles short names, long names, more than one name for the same option, and option values.
Short options are single letters. The option v
would be specified by passing -v
on the command line. In the default parsing mode, short options can be written in a compact form, for instance -abc
is equivalent to -a -b -c
. The parsing mode for can be set to ParseAsLongOptions, in which case -abc
will be parsed as the long option abc
.
Long options are more than one letter long and can not be compacted together. The long option verbose
would be passed as –verbose
or -verbose
.
Passing values for options can be done using an equal sign or a space.
The parser does not support optional values - if an option is set to require a value, one must be present. If such an option is placed last and has no value, the option will be treated as if it had not been specified.
The parser does not automatically support negating or disabling long options by using the format –disable-option
or –no-option
. However, it is possible to handle this case explicitly by making an option with no-option
as one of its names, and handling the option explicitly.
The parsing of CopperSpice options inside QCoreApplication and subclasses happens before QCommandLineParser exists, so it can not take it into account. This means any option value that looks like a builtin option, will be treated by QCoreApplication as a builtin CopperSpice option.
The previous example will lead to QApplication seeing the -reverse option set, and removing it from QCoreApplication::arguments() before QCommandLineParser defines the profile
option and parses the command line.
In practice, additional error checking needs to be performed on the positional arguments and option values. For example, ranges of numbers should be checked.
It is then advisable to introduce a function to do the command line parsing which takes a struct or class receiving the option values returning an enumeration representing the result. The dnslookup example of the CsNetwork library illustrates the following.
In the main function, help should be printed to the standard output if the help option was passed and the application should return the exit code 0. If an error was detected, the error message should be printed to the standard error output and the application should return an exit code other than 0.
A special case to consider is a GUI application for Windows or mobile platforms. These applications should not use the standard output or error channels since the output is either discarded or not accessible. Instead it is recommended to display help texts and error messages using a QMessageBox.
To preserve the formatting of the help text, use rich text format with <pre>
elements.
This enum describes the way the parser interprets command line options that use a single dash followed by multiple letters, as in "-abc".
Constant | Value | Description |
---|---|---|
QCommandLineParser::ParseAsCompactedShortOptions | 0 | -abc is interpreted as -a -b -c , i.e. as three short options that have been compacted on the command-line, if none of the options take a value. If a takes a value, then it is interpreted as -a bc , i.e. the short option a followed by the value bc . This is typically used in tools that behave like compilers, in order to handle options such as -DDEFINE=VALUE or -I/include/path . This is the default parsing mode. New applications are recommended to use this mode. |
QCommandLineParser::ParseAsLongOptions | 1 | -abc is interpreted as –abc , i.e. as the long option named abc . This is how the CopperSpice tools (uic, rcc...) have always been parsing arguments. This mode should be used for preserving compatibility in applications that were parsing arguments in such a way. |
QCommandLineParser::QCommandLineParser | ( | ) |
Constructs a command line parser object.
QCommandLineParser::~QCommandLineParser | ( | ) |
Destroys the command line parser object.
QCommandLineOption QCommandLineParser::addHelpOption | ( | ) |
Adds the help options -h
and –help
. On Windows and additional option for -?
will be added. These options will be processed automatically by QCommandLineParser.
Use setApplicationDescription() to add change the application description, which is displayed when the user asks for help.
bool QCommandLineParser::addOption | ( | const QCommandLineOption & | option | ) |
Adds the option to look for while parsing. Returns true
if adding the option was successful, otherwise returns false
.
Adding the option fails if there is no name attached to the option, or the option has a name that clashes with an option name added before.
void QCommandLineParser::addPositionalArgument | ( | const QString & | name, |
const QString & | description, | ||
const QString & | syntax = QString() |
||
) |
Defines an additional argument to the application, for the benefit of the help text.
The argument name and description will appear under the Arguments:
section of the help. If syntax is specified, it will be appended to the Usage line, otherwise the name will be appended.
QCommandLineOption QCommandLineParser::addVersionOption | ( | ) |
Adds the -v
/ –version
option, which displays the version string of the application. This option is handled automatically by QCommandLineParser.
You can set the actual version string by using QCoreApplication::setApplicationVersion().
Returns the option instance which can be used to call isSet().
QString QCommandLineParser::applicationDescription | ( | ) | const |
Returns the application description set in setApplicationDescription().
void QCommandLineParser::clearPositionalArguments | ( | ) |
Clears the definitions of additional arguments from the help text.
This is only needed for the special case of tools which support multiple commands with different options. Once the actual command has been identified, the options for this command can be defined, and the help text for the command can be adjusted accordingly.
QString QCommandLineParser::errorText | ( | ) | const |
Returns a translated error text for the user. This should only be called when parse() returns false
.
QString QCommandLineParser::helpText | ( | ) | const |
Returns a string containing the complete help information.
bool QCommandLineParser::isSet | ( | const QCommandLineOption & | option | ) | const |
Checks whether the option was passed to the application. Returns true
if the option was set, false otherwise.
This is the recommended way to check for options with no values.
bool QCommandLineParser::isSet | ( | const QString & | name | ) | const |
Checks whether the option name was passed to the application.
Returns true
if the option name was set, false otherwise.
The name provided can be any long or short name of any option that was added with addOption()
. All the options names are treated as being equivalent. If the name is not recognized or that option was not present, false is returned.
QStringList QCommandLineParser::optionNames | ( | ) | const |
Returns a list of option names that were found.
This returns a list of all the recognized option names found by the parser, in the order in which they were found. For any long options that were in the form {–option=value}, the value part will have been dropped.
The names in this list do not include the preceding dash characters. Names may appear more than once in this list if they were encountered more than once by the parser.
Any entry in the list can be used with value()
or with values()
to get any relevant option values.
bool QCommandLineParser::parse | ( | const QStringList & | arguments | ) |
Parses the command line arguments. The first element must be the name of the executable. Returns false
in case of a parse error (unknown option or missing value), returns true
otherwise.
Most programs do not need to call this method since a simple call to process() is enough. The application will have to take care of the error handling, using errorText() if parse() returns false
. This can be useful for instance to show a graphical error message in graphical programs.
Calling parse() instead of process() can also be useful in order to ignore unknown options temporarily, because more option definitions will be provided later on (depending on one of the arguments), before calling process().
QStringList QCommandLineParser::positionalArguments | ( | ) | const |
Returns a list of positional arguments.
These are all of the arguments that were not recognized as part of an option.
void QCommandLineParser::process | ( | const QCoreApplication & | app | ) |
The command line is obtained from the QCoreApplication instance app.
void QCommandLineParser::process | ( | const QStringList & | arguments | ) |
Processes the command line arguments. In addition to parsing the options (like parse()), this function also handles the builtin options and handles errors. The builtin options are –version
if addVersionOption was called and –help
if addHelpOption was called.
When invoking one of these options, or when an error happens (for instance an unknown option was passed), the current process will then stop, using the exit() function.
void QCommandLineParser::setApplicationDescription | ( | const QString & | description | ) |
Sets the application description shown by helpText().
void QCommandLineParser::setSingleDashWordOptionMode | ( | SingleDashWordOptionMode | parsingMode | ) |
void QCommandLineParser::showHelp | ( | int | exitCode = 0 | ) |
Displays the help information, and exits the application. This is automatically triggered by the –help option, but can also be used to display the help when the user is not invoking the application correctly. The exit code is set to exitCode. It should be set to 0 if the user requested to see the help, and to any other value in case of an error.
QStringList QCommandLineParser::unknownOptionNames | ( | ) | const |
Returns a list of unknown option names.
This list will include both long an short name options that were not recognized. For any long options that were in the form {–option=value}, the value part will have been dropped and only the long name is added.
The names in this list do not include the preceding dash characters. Names may appear more than once in this list if they were encountered more than once by the parser.
QString QCommandLineParser::value | ( | const QCommandLineOption & | option | ) | const |
Returns the option value found for the given option, or an empty string if not found.
For options found by the parser, the last value found for that option is returned. If the option was not specified on the command line, the default value is returned.
An empty string is returned if the option does not take a value.
Returns the option value found for the given option name optionName, or an empty string if not found.
The name provided can be any long or short name of any option that was added with addOption()
. All the option names are treated as being equivalent. If the name is not recognized or that option was not present, an empty string is returned.
For options found by the parser, the last value found for that option is returned. If the option was not specified on the command line, the default value is returned.
An empty string is returned if the option does not take a value.
QStringList QCommandLineParser::values | ( | const QCommandLineOption & | option | ) | const |
Returns a list of option values found for the given option, or an empty list if not found.
For options found by the parser, the list will contain an entry for each time the option was encountered by the parser. If the option was not specified on the command line, the default values are returned.
An empty list is returned if the option does not take a value.
QStringList QCommandLineParser::values | ( | const QString & | optionName | ) | const |
Returns a list of option values found for the given option name optionName, or an empty list if not found.
The name provided can be any long or short name of any option that was added with addOption()
. All the options names are treated as being equivalent. If the name is not recognized or that option was not present, an empty list is returned.
For options found by the parser, the list will contain an entry for each time the option was encountered by the parser. If the option was not specified on the command line, the default values are returned.
An empty list is returned if the option does not take a value.