commit b58af75ce35f81f575068f3ce54f1912bfae122b Author: Adam Piatyszek Date: Wed Sep 18 09:28:57 2019 +0100 QCommandLineParser: allow overriding the default executable in usage Implement setApplicationExecutable(const QString&) method to override the default executable name used by helpText(). Change-Id: I5ef695219978b6f56b9a94198a25fe6f6c9c0696 diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp index 279d6565da..ed1d275f47 100644 --- a/src/corelib/tools/qcommandlineparser.cpp +++ b/src/corelib/tools/qcommandlineparser.cpp @@ -60,7 +60,8 @@ class QCommandLineParserPrivate { public: inline QCommandLineParserPrivate() - : singleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions), + : executable(QCoreApplication::instance()->arguments().constFirst()), // default executable name + singleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions), optionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsOptions), builtinVersionOption(false), builtinHelpOption(false), @@ -100,6 +101,9 @@ public: //! Application description QString description; + //! Application executable name + QString executable; + //! Documentation for positional arguments struct PositionalArgumentDefinition { @@ -456,6 +460,25 @@ QString QCommandLineParser::applicationDescription() const return d->description; } +/*! + Sets the application \a executable shown by helpText(). +*/ +void QCommandLineParser::setApplicationExecutable(const QString &executable) +{ + d->executable = executable; +} + +/*! + Returns the application \a executable name used by helpText(). + + If no custom application \a executable was set with setApplicationExecutable(), + the absolute path to the default executable will be returned. +*/ +QString QCommandLineParser::applicationExecutable() const +{ + return d->executable; +} + /*! Defines an additional argument to the application, for the benefit of the help text. @@ -1105,8 +1128,7 @@ QString QCommandLineParserPrivate::helpText() const { const QLatin1Char nl('\n'); QString text; - QString usage; - usage += QCoreApplication::instance()->arguments().constFirst(); // executable name + QString usage = executable; // executable name if (!commandLineOptionList.isEmpty()) usage += QLatin1Char(' ') + QCommandLineParser::tr("[options]"); for (const PositionalArgumentDefinition &arg : positionalArgumentDefinitions) diff --git a/src/corelib/tools/qcommandlineparser.h b/src/corelib/tools/qcommandlineparser.h index 4584c384cc..9b7ce87a57 100644 --- a/src/corelib/tools/qcommandlineparser.h +++ b/src/corelib/tools/qcommandlineparser.h @@ -78,6 +78,8 @@ public: QCommandLineOption addHelpOption(); void setApplicationDescription(const QString &description); QString applicationDescription() const; + void setApplicationExecutable(const QString &executable); + QString applicationExecutable() const; void addPositionalArgument(const QString &name, const QString &description, const QString &syntax = QString()); void clearPositionalArguments();