diff --git a/src/app/core/qcommandlineoption.cpp b/src/app/core/qcommandlineoption.cpp index a063a1fea..a18f3e2a9 100644 --- a/src/app/core/qcommandlineoption.cpp +++ b/src/app/core/qcommandlineoption.cpp @@ -34,7 +34,6 @@ class QCommandLineOptionPrivate : public QSharedData { public: inline QCommandLineOptionPrivate() - : hidden(false) { } void setNames(const QStringList &nameList); @@ -51,9 +50,6 @@ public: //! The list of default values used for this option. QStringList defaultValues; - - //! Show or hide in --help - bool hidden; }; #ifdef Q_CC_GNU @@ -362,29 +358,4 @@ QStringList QCommandLineOption::defaultValues() const return d->defaultValues; } -/*! - Sets whether to hide this option in the user-visible help output. - - All options are visible by default. Setting \a hidden to true for - a particular option makes it internal, i.e. not listed in the help output. - - \since 5.6 - \sa isHidden - */ -void QCommandLineOption::setHidden(bool hide) -{ - d->hidden = hide; -} - -/*! - Returns true if this option is omitted from the help output, - false if the option is listed. - - \since 5.6 - \sa setHidden() - */ -bool QCommandLineOption::isHidden() const -{ - return d->hidden; -} #endif //QT_VERSION < QT_VERSION_CHECK(5, 2, 0) diff --git a/src/app/core/qcommandlineoption.h b/src/app/core/qcommandlineoption.h index 92fd65109..abc2abeae 100644 --- a/src/app/core/qcommandlineoption.h +++ b/src/app/core/qcommandlineoption.h @@ -69,9 +69,6 @@ public: void setDefaultValues(const QStringList &defaultValues); QStringList defaultValues() const; - void setHidden(bool hidden); - bool isHidden() const; - private: QSharedDataPointer d; }; diff --git a/src/app/core/qcommandlineparser.cpp b/src/app/core/qcommandlineparser.cpp index b26868edf..814b3f1de 100644 --- a/src/app/core/qcommandlineparser.cpp +++ b/src/app/core/qcommandlineparser.cpp @@ -47,7 +47,6 @@ class QCommandLineParserPrivate public: inline QCommandLineParserPrivate() : singleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions), - optionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsOptions), builtinVersionOption(false), builtinHelpOption(false), needsParsing(true) @@ -98,9 +97,6 @@ public: //! The parsing mode for "-abc" QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode; - //! How to parse "arg -option" - QCommandLineParser::OptionsAfterPositionalArgumentsMode optionsAfterPositionalArgumentsMode; - //! Whether addVersionOption was called bool builtinVersionOption; @@ -211,10 +207,7 @@ QStringList QCommandLineParserPrivate::aliases(const QString &optionName) const platforms. These applications may not use the standard output or error channels since the output is either discarded or not accessible. - On Windows, QCommandLineParser uses message boxes to display usage information - and errors if no console window can be obtained. - - For other platforms, it is recommended to display help texts and error messages + For such GUI applications, it is recommended to display help texts and error messages using a QMessageBox. To preserve the formatting of the help text, rich text with \c
 elements should be used:
 
@@ -224,20 +217,36 @@ QStringList QCommandLineParserPrivate::aliases(const QString &optionName) const
     case CommandLineOk:
         break;
     case CommandLineError:
+#ifdef Q_OS_WIN
         QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),
                              "

" + errorMessage + "

"
                              + parser.helpText() + "
"); +#else + fputs(qPrintable(errorMessage), stderr); + fputs("\n\n", stderr); + fputs(qPrintable(parser.helpText()), stderr); +#endif return 1; case CommandLineVersionRequested: +#ifdef Q_OS_WIN QMessageBox::information(0, QGuiApplication::applicationDisplayName(), QGuiApplication::applicationDisplayName() + ' ' + QCoreApplication::applicationVersion()); +#else + printf("%s %s\n", QGuiApplication::applicationDisplayName(), + qPrintable(QCoreApplication::applicationVersion())); +#endif return 0; case CommandLineHelpRequested: +#ifdef Q_OS_WIN QMessageBox::warning(0, QGuiApplication::applicationDisplayName(), "
"
                              + parser.helpText() + "
"); return 0; +#else + parser.showHelp(); + Q_UNREACHABLE(); +#endif } \endcode @@ -296,41 +305,6 @@ void QCommandLineParser::setSingleDashWordOptionMode(QCommandLineParser::SingleD d->singleDashWordOptionMode = singleDashWordOptionMode; } -/*! - \enum QCommandLineParser::OptionsAfterPositionalArgumentsMode - - This enum describes the way the parser interprets options that - occur after positional arguments. - - \value ParseAsOptions \c{application argument --opt -t} is interpreted as setting - the options \c{opt} and \c{t}, just like \c{application --opt -t argument} would do. - This is the default parsing mode. In order to specify that \c{--opt} and \c{-t} - are positional arguments instead, the user can use \c{--}, as in - \c{application argument -- --opt -t}. - - \value ParseAsPositionalArguments \c{application argument --opt} is interpreted as - having two positional arguments, \c{argument} and \c{--opt}. - This mode is useful for executables that aim to launch other executables - (e.g. wrappers, debugging tools, etc.) or that support internal commands - followed by options for the command. \c{argument} is the name of the command, - and all options occurring after it can be collected and parsed by another - command line parser, possibly in another executable. - - \sa setOptionsAfterPositionalArgumentsMode() - - \since 5.5 -*/ - -/*! - Sets the parsing mode to \a parsingMode. - This must be called before process() or parse(). - \since 5.5 -*/ -void QCommandLineParser::setOptionsAfterPositionalArgumentsMode(QCommandLineParser::OptionsAfterPositionalArgumentsMode parsingMode) -{ - d->optionsAfterPositionalArgumentsMode = parsingMode; -} - /*! Adds the option \a option to look for while parsing. @@ -515,41 +489,6 @@ QString QCommandLineParser::errorText() const return QString(); } -enum MessageType { UsageMessage, ErrorMessage }; - -#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT) -// Return whether to use a message box. Use handles if a console can be obtained -// or we are run with redirected handles (for example, by QProcess). -static inline bool displayMessageBox() -{ - if (GetConsoleWindow()) - return false; - STARTUPINFO startupInfo; - startupInfo.cb = sizeof(STARTUPINFO); - GetStartupInfo(&startupInfo); - return !(startupInfo.dwFlags & STARTF_USESTDHANDLES); -} -#endif // Q_OS_WIN && !QT_BOOTSTRAPPED && !Q_OS_WIN && !Q_OS_WINRT - -static void showParserMessage(const QString &message, MessageType type) -{ -#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT) - if (displayMessageBox()) { - const UINT flags = MB_OK | MB_TOPMOST | MB_SETFOREGROUND - | (type == UsageMessage ? MB_ICONINFORMATION : MB_ICONERROR); - QString title; - if (QCoreApplication::instance()) - title = QCoreApplication::instance()->property("applicationDisplayName").toString(); - if (title.isEmpty()) - title = QCoreApplication::applicationName(); - MessageBoxW(0, reinterpret_cast(message.utf16()), - reinterpret_cast(title.utf16()), flags); - return; - } -#endif // Q_OS_WIN && !QT_BOOTSTRAPPED && !Q_OS_WIN && !Q_OS_WINRT - fputs(qPrintable(message), type == UsageMessage ? stdout : stderr); -} - /*! Processes the command line \a arguments. @@ -566,7 +505,7 @@ static void showParserMessage(const QString &message, MessageType type) void QCommandLineParser::process(const QStringList &arguments) { if (!d->parse(arguments)) { - showParserMessage(errorText() + QLatin1Char('\n'), ErrorMessage); + fprintf(stderr, "%s\n", qPrintable(errorText())); ::exit(EXIT_FAILURE); } @@ -673,7 +612,7 @@ bool QCommandLineParserPrivate::parse(const QStringList &args) const QLatin1Char dashChar('-'); const QLatin1Char assignChar('='); - bool forcePositional = false; + bool doubleDashFound = false; errorText.clear(); positionalArgumentList.clear(); optionNames.clear(); @@ -691,7 +630,7 @@ bool QCommandLineParserPrivate::parse(const QStringList &args) for (; argumentIterator != args.end() ; ++argumentIterator) { QString argument = *argumentIterator; - if (forcePositional) { + if (doubleDashFound) { positionalArgumentList.append(argument); } else if (argument.startsWith(doubleDashString)) { if (argument.length() > 2) { @@ -703,7 +642,7 @@ bool QCommandLineParserPrivate::parse(const QStringList &args) error = true; } } else { - forcePositional = true; + doubleDashFound = true; } } else if (argument.startsWith(dashChar)) { if (argument.size() == 1) { // single dash ("stdin") @@ -755,8 +694,6 @@ bool QCommandLineParserPrivate::parse(const QStringList &args) } } else { positionalArgumentList.append(argument); - if (optionsAfterPositionalArgumentsMode == QCommandLineParser::ParseAsPositionalArguments) - forcePositional = true; } if (argumentIterator == args.end()) break; @@ -974,9 +911,7 @@ QStringList QCommandLineParser::unknownOptionNames() const */ Q_NORETURN void QCommandLineParser::showVersion() { - showParserMessage(QCoreApplication::applicationName() + QLatin1Char(' ') - + QCoreApplication::applicationVersion() + QLatin1Char('\n'), - UsageMessage); + fprintf(stdout, "%s %s\n", qPrintable(QCoreApplication::applicationName()), qPrintable(QCoreApplication::applicationVersion())); ::exit(EXIT_SUCCESS); } @@ -993,7 +928,7 @@ Q_NORETURN void QCommandLineParser::showVersion() */ Q_NORETURN void QCommandLineParser::showHelp(int exitCode) { - showParserMessage(d->helpText(), UsageMessage); + fprintf(stdout, "%s", qPrintable(d->helpText())); ::exit(exitCode); } @@ -1097,8 +1032,6 @@ QString QCommandLineParserPrivate::helpText() const ++longestOptionNameString; for (int i = 0; i < commandLineOptionList.count(); ++i) { const QCommandLineOption &option = commandLineOptionList.at(i); - if (option.isHidden()) - continue; text += wrapText(optionNameList.at(i), longestOptionNameString, option.description()); } if (!positionalArgumentDefinitions.isEmpty()) { diff --git a/src/app/core/qcommandlineparser.h b/src/app/core/qcommandlineparser.h index c67b36bf1..568b25cbc 100644 --- a/src/app/core/qcommandlineparser.h +++ b/src/app/core/qcommandlineparser.h @@ -22,7 +22,6 @@ #include #include -#include #include "qcommandlineoption.h" #if QT_VERSION < QT_VERSION_CHECK(5, 2, 0) @@ -48,12 +47,6 @@ public: }; void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode); - enum OptionsAfterPositionalArgumentsMode { - ParseAsOptions, - ParseAsPositionalArguments - }; - void setOptionsAfterPositionalArgumentsMode(OptionsAfterPositionalArgumentsMode mode); - bool addOption(const QCommandLineOption &commandLineOption); bool addOptions(const QList &options);