4#ifndef QCOMMANDLINEPARSER_H
5#define QCOMMANDLINEPARSER_H
7#include <QtCore/qstringlist.h>
9#include <QtCore/qcoreapplication.h>
10#include <QtCore/qcommandlineoption.h>
16class QCommandLineParserPrivate;
17class QCoreApplication;
21 Q_DECLARE_TR_FUNCTIONS(QCommandLineParser)
24 ~QCommandLineParser();
38 bool addOption(
const QCommandLineOption &commandLineOption);
39 bool addOptions(
const QList<QCommandLineOption> &options);
45 void addPositionalArgument(
const QString &name,
const QString &description,
const QString &syntax = QString());
48 void process(
const QStringList &arguments);
49 void process(
const QCoreApplication &app);
51 bool parse(
const QStringList &arguments);
54 bool isSet(
const QString &name)
const;
58 bool isSet(
const QCommandLineOption &option)
const;
77 Q_DISABLE_COPY(QCommandLineParser)
79 QCommandLineParserPrivate *
const d;
QStringList aliases(const QString &name) const
bool parseOptionValue(const QString &optionName, const QString &argument, QStringList::const_iterator *argumentIterator, QStringList::const_iterator argsEnd)
Parse the value for a given option, if it was defined to expect one.
QList< QCommandLineOption > commandLineOptionList
The command line options used for parsing.
QList< PositionalArgumentDefinition > positionalArgumentDefinitions
bool registerFoundOption(const QString &optionName)
NameHash_t nameHash
Hash mapping option names to their offsets in commandLineOptionList and optionArgumentList.
bool builtinVersionOption
Whether addVersionOption was called.
QStringList optionNames
Names of options found on the command line.
QString helpText(bool includeQtOptions) const
QCommandLineParserPrivate()
QString description
Application description.
bool needsParsing
True if parse() needs to be called.
void checkParsed(const char *method)
bool parse(const QStringList &args)
bool builtinHelpOption
Whether addHelpOption was called.
QStringList unknownOptionNames
Names of options which were unknown.
QStringList positionalArgumentList
Arguments which did not belong to any option.
Q_NORETURN void showHelp(int exitCode, bool includeQtOptions)
QHash< qsizetype, QStringList > optionValuesHash
Option values found (only for options with a value)
QString errorText
Error text set when parse() returns false.
The QCommandLineParser class provides a means for handling the command line options.
QString value(const QString &name) const
Returns the option value found for the given option name optionName, or an empty string if not found.
bool parse(const QStringList &arguments)
Parses the command line arguments.
void 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.
QString applicationDescription() const
Returns the application description set in setApplicationDescription().
QStringList positionalArguments() const
Returns a list of positional arguments.
QStringList unknownOptionNames() const
Returns a list of unknown option names.
void process(const QCoreApplication &app)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode)
Sets the parsing mode to singleDashWordOptionMode.
Q_NORETURN void showVersion()
Displays the version information from QCoreApplication::applicationVersion(), and exits the applicati...
QStringList values(const QString &name) const
Returns a list of option values found for the given option name optionName, or an empty list if not f...
void setApplicationDescription(const QString &description)
Sets the application description shown by helpText().
bool addOptions(const QList< QCommandLineOption > &options)
bool addOption(const QCommandLineOption &commandLineOption)
Adds the option option to look for while parsing.
Q_NORETURN void showHelp(int exitCode=0)
Displays the help information, and exits the application.
QString errorText() const
Returns a translated error text for the user.
SingleDashWordOptionMode
This enum describes the way the parser interprets command-line options that use a single dash followe...
@ ParseAsCompactedShortOptions
bool isSet(const QString &name) const
Checks whether the option name was passed to the application.
static void showMessageAndExit(MessageType type, const QString &message, int exitCode=0)
QString helpText() const
Returns a string containing the complete help information.
QStringList optionNames() const
Returns a list of option names that were found.
void clearPositionalArguments()
Clears the definitions of additional arguments from the help text.
OptionsAfterPositionalArgumentsMode
This enum describes the way the parser interprets options that occur after positional arguments.
@ ParseAsPositionalArguments
void setOptionsAfterPositionalArgumentsMode(OptionsAfterPositionalArgumentsMode mode)
Sets the parsing mode to parsingMode.
QCommandLineOption addVersionOption()
Adds the {-v} / {–version} option, which displays the version string of the application.
QCommandLineOption addHelpOption()
Adds help options to the command-line parser.
QT_REQUIRE_CONFIG(commandlineparser)
Q_DECLARE_TYPEINFO(QCommandLineParserPrivate::PositionalArgumentDefinition, Q_RELOCATABLE_TYPE)
static QString wrapText(const QString &names, int optionNameMaxWidth, const QString &description)
QHash< QString, qsizetype > NameHash_t
void process(const QChar &ch)
Documentation for positional arguments.