diff options
Diffstat (limited to 'src/corelib/tools/qcommandlineparser.cpp')
-rw-r--r-- | src/corelib/tools/qcommandlineparser.cpp | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp index 505ab5f46d..f503ed7452 100644 --- a/src/corelib/tools/qcommandlineparser.cpp +++ b/src/corelib/tools/qcommandlineparser.cpp @@ -179,6 +179,10 @@ QStringList QCommandLineParserPrivate::aliases(const QString &optionName) const Example: \snippet code/src_corelib_tools_qcommandlineparser_main.cpp 0 + If your compiler supports the C++11 standard, the three addOption() calls in + the above example can be simplified: + \snippet code/src_corelib_tools_qcommandlineparser_main.cpp cxx11 + Known limitation: the parsing of Qt options inside QCoreApplication and subclasses happens before QCommandLineParser exists, so it can't take it into account. This means any option value that looks like a builtin Qt option, will be treated by @@ -342,6 +346,24 @@ bool QCommandLineParser::addOption(const QCommandLineOption &option) } /*! + \since 5.4 + + Adds the options \a options to look for while parsing. + + Returns \c false if adding any of the options failed; otherwise returns \c false. + + Cf. addOption() for when it may fail. +*/ +bool QCommandLineParser::addOptions(const QList<QCommandLineOption> &options) +{ + // should be optimized (but it's no worse than what was possible before) + bool result = true; + for (QList<QCommandLineOption>::const_iterator it = options.begin(), end = options.end(); it != end; ++it) + result &= addOption(*it); + return result; +} + +/*! Adds the \c{-v} / \c{--version} option, which displays the version string of the application. This option is handled automatically by QCommandLineParser. @@ -495,10 +517,8 @@ void QCommandLineParser::process(const QStringList &arguments) ::exit(EXIT_FAILURE); } - if (d->builtinVersionOption && isSet(QStringLiteral("version"))) { - printf("%s %s\n", qPrintable(QCoreApplication::applicationName()), qPrintable(QCoreApplication::applicationVersion())); - ::exit(EXIT_SUCCESS); - } + if (d->builtinVersionOption && isSet(QStringLiteral("version"))) + showVersion(); if (d->builtinHelpOption && isSet(QStringLiteral("help"))) showHelp(EXIT_SUCCESS); @@ -888,6 +908,22 @@ QStringList QCommandLineParser::unknownOptionNames() const } /*! + Displays the version information from QCoreApplication::applicationVersion(), + and exits the application. + This is automatically triggered by the --version option, but can also + be used to display the version when not using process(). + The exit code is set to EXIT_SUCCESS (0). + + \sa addVersionOption() + \since 5.4 +*/ +Q_NORETURN void QCommandLineParser::showVersion() +{ + fprintf(stdout, "%s %s\n", qPrintable(QCoreApplication::applicationName()), qPrintable(QCoreApplication::applicationVersion())); + ::exit(EXIT_SUCCESS); +} + +/*! 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 |