Files from Qt 5.4.1.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-04-02 17:56:17 +03:00
parent fb879c7ae1
commit 0c023a9db9
4 changed files with 23 additions and 129 deletions

View File

@ -34,7 +34,6 @@ class QCommandLineOptionPrivate : public QSharedData
{ {
public: public:
inline QCommandLineOptionPrivate() inline QCommandLineOptionPrivate()
: hidden(false)
{ } { }
void setNames(const QStringList &nameList); void setNames(const QStringList &nameList);
@ -51,9 +50,6 @@ public:
//! The list of default values used for this option. //! The list of default values used for this option.
QStringList defaultValues; QStringList defaultValues;
//! Show or hide in --help
bool hidden;
}; };
#ifdef Q_CC_GNU #ifdef Q_CC_GNU
@ -362,29 +358,4 @@ QStringList QCommandLineOption::defaultValues() const
return d->defaultValues; 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) #endif //QT_VERSION < QT_VERSION_CHECK(5, 2, 0)

View File

@ -69,9 +69,6 @@ public:
void setDefaultValues(const QStringList &defaultValues); void setDefaultValues(const QStringList &defaultValues);
QStringList defaultValues() const; QStringList defaultValues() const;
void setHidden(bool hidden);
bool isHidden() const;
private: private:
QSharedDataPointer<QCommandLineOptionPrivate> d; QSharedDataPointer<QCommandLineOptionPrivate> d;
}; };

View File

@ -47,7 +47,6 @@ class QCommandLineParserPrivate
public: public:
inline QCommandLineParserPrivate() inline QCommandLineParserPrivate()
: singleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions), : singleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions),
optionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsOptions),
builtinVersionOption(false), builtinVersionOption(false),
builtinHelpOption(false), builtinHelpOption(false),
needsParsing(true) needsParsing(true)
@ -98,9 +97,6 @@ public:
//! The parsing mode for "-abc" //! The parsing mode for "-abc"
QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode; QCommandLineParser::SingleDashWordOptionMode singleDashWordOptionMode;
//! How to parse "arg -option"
QCommandLineParser::OptionsAfterPositionalArgumentsMode optionsAfterPositionalArgumentsMode;
//! Whether addVersionOption was called //! Whether addVersionOption was called
bool builtinVersionOption; 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 platforms. These applications may not use the standard output or error channels
since the output is either discarded or not accessible. since the output is either discarded or not accessible.
On Windows, QCommandLineParser uses message boxes to display usage information For such GUI applications, it is recommended to display help texts and error messages
and errors if no console window can be obtained.
For other platforms, it is recommended to display help texts and error messages
using a QMessageBox. To preserve the formatting of the help text, rich text using a QMessageBox. To preserve the formatting of the help text, rich text
with \c <pre> elements should be used: with \c <pre> elements should be used:
@ -224,20 +217,36 @@ QStringList QCommandLineParserPrivate::aliases(const QString &optionName) const
case CommandLineOk: case CommandLineOk:
break; break;
case CommandLineError: case CommandLineError:
#ifdef Q_OS_WIN
QMessageBox::warning(0, QGuiApplication::applicationDisplayName(), QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),
"<html><head/><body><h2>" + errorMessage + "</h2><pre>" "<html><head/><body><h2>" + errorMessage + "</h2><pre>"
+ parser.helpText() + "</pre></body></html>"); + parser.helpText() + "</pre></body></html>");
#else
fputs(qPrintable(errorMessage), stderr);
fputs("\n\n", stderr);
fputs(qPrintable(parser.helpText()), stderr);
#endif
return 1; return 1;
case CommandLineVersionRequested: case CommandLineVersionRequested:
#ifdef Q_OS_WIN
QMessageBox::information(0, QGuiApplication::applicationDisplayName(), QMessageBox::information(0, QGuiApplication::applicationDisplayName(),
QGuiApplication::applicationDisplayName() + ' ' QGuiApplication::applicationDisplayName() + ' '
+ QCoreApplication::applicationVersion()); + QCoreApplication::applicationVersion());
#else
printf("%s %s\n", QGuiApplication::applicationDisplayName(),
qPrintable(QCoreApplication::applicationVersion()));
#endif
return 0; return 0;
case CommandLineHelpRequested: case CommandLineHelpRequested:
#ifdef Q_OS_WIN
QMessageBox::warning(0, QGuiApplication::applicationDisplayName(), QMessageBox::warning(0, QGuiApplication::applicationDisplayName(),
"<html><head/><body><pre>" "<html><head/><body><pre>"
+ parser.helpText() + "</pre></body></html>"); + parser.helpText() + "</pre></body></html>");
return 0; return 0;
#else
parser.showHelp();
Q_UNREACHABLE();
#endif
} }
\endcode \endcode
@ -296,41 +305,6 @@ void QCommandLineParser::setSingleDashWordOptionMode(QCommandLineParser::SingleD
d->singleDashWordOptionMode = singleDashWordOptionMode; 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. Adds the option \a option to look for while parsing.
@ -515,41 +489,6 @@ QString QCommandLineParser::errorText() const
return QString(); 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<const wchar_t *>(message.utf16()),
reinterpret_cast<const wchar_t *>(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. Processes the command line \a arguments.
@ -566,7 +505,7 @@ static void showParserMessage(const QString &message, MessageType type)
void QCommandLineParser::process(const QStringList &arguments) void QCommandLineParser::process(const QStringList &arguments)
{ {
if (!d->parse(arguments)) { if (!d->parse(arguments)) {
showParserMessage(errorText() + QLatin1Char('\n'), ErrorMessage); fprintf(stderr, "%s\n", qPrintable(errorText()));
::exit(EXIT_FAILURE); ::exit(EXIT_FAILURE);
} }
@ -673,7 +612,7 @@ bool QCommandLineParserPrivate::parse(const QStringList &args)
const QLatin1Char dashChar('-'); const QLatin1Char dashChar('-');
const QLatin1Char assignChar('='); const QLatin1Char assignChar('=');
bool forcePositional = false; bool doubleDashFound = false;
errorText.clear(); errorText.clear();
positionalArgumentList.clear(); positionalArgumentList.clear();
optionNames.clear(); optionNames.clear();
@ -691,7 +630,7 @@ bool QCommandLineParserPrivate::parse(const QStringList &args)
for (; argumentIterator != args.end() ; ++argumentIterator) { for (; argumentIterator != args.end() ; ++argumentIterator) {
QString argument = *argumentIterator; QString argument = *argumentIterator;
if (forcePositional) { if (doubleDashFound) {
positionalArgumentList.append(argument); positionalArgumentList.append(argument);
} else if (argument.startsWith(doubleDashString)) { } else if (argument.startsWith(doubleDashString)) {
if (argument.length() > 2) { if (argument.length() > 2) {
@ -703,7 +642,7 @@ bool QCommandLineParserPrivate::parse(const QStringList &args)
error = true; error = true;
} }
} else { } else {
forcePositional = true; doubleDashFound = true;
} }
} else if (argument.startsWith(dashChar)) { } else if (argument.startsWith(dashChar)) {
if (argument.size() == 1) { // single dash ("stdin") if (argument.size() == 1) { // single dash ("stdin")
@ -755,8 +694,6 @@ bool QCommandLineParserPrivate::parse(const QStringList &args)
} }
} else { } else {
positionalArgumentList.append(argument); positionalArgumentList.append(argument);
if (optionsAfterPositionalArgumentsMode == QCommandLineParser::ParseAsPositionalArguments)
forcePositional = true;
} }
if (argumentIterator == args.end()) if (argumentIterator == args.end())
break; break;
@ -974,9 +911,7 @@ QStringList QCommandLineParser::unknownOptionNames() const
*/ */
Q_NORETURN void QCommandLineParser::showVersion() Q_NORETURN void QCommandLineParser::showVersion()
{ {
showParserMessage(QCoreApplication::applicationName() + QLatin1Char(' ') fprintf(stdout, "%s %s\n", qPrintable(QCoreApplication::applicationName()), qPrintable(QCoreApplication::applicationVersion()));
+ QCoreApplication::applicationVersion() + QLatin1Char('\n'),
UsageMessage);
::exit(EXIT_SUCCESS); ::exit(EXIT_SUCCESS);
} }
@ -993,7 +928,7 @@ Q_NORETURN void QCommandLineParser::showVersion()
*/ */
Q_NORETURN void QCommandLineParser::showHelp(int exitCode) Q_NORETURN void QCommandLineParser::showHelp(int exitCode)
{ {
showParserMessage(d->helpText(), UsageMessage); fprintf(stdout, "%s", qPrintable(d->helpText()));
::exit(exitCode); ::exit(exitCode);
} }
@ -1097,8 +1032,6 @@ QString QCommandLineParserPrivate::helpText() const
++longestOptionNameString; ++longestOptionNameString;
for (int i = 0; i < commandLineOptionList.count(); ++i) { for (int i = 0; i < commandLineOptionList.count(); ++i) {
const QCommandLineOption &option = commandLineOptionList.at(i); const QCommandLineOption &option = commandLineOptionList.at(i);
if (option.isHidden())
continue;
text += wrapText(optionNameList.at(i), longestOptionNameString, option.description()); text += wrapText(optionNameList.at(i), longestOptionNameString, option.description());
} }
if (!positionalArgumentDefinitions.isEmpty()) { if (!positionalArgumentDefinitions.isEmpty()) {

View File

@ -22,7 +22,6 @@
#include <QStringList> #include <QStringList>
#include <QCoreApplication> #include <QCoreApplication>
#include <QtGlobal>
#include "qcommandlineoption.h" #include "qcommandlineoption.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 2, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 2, 0)
@ -48,12 +47,6 @@ public:
}; };
void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode); void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode);
enum OptionsAfterPositionalArgumentsMode {
ParseAsOptions,
ParseAsPositionalArguments
};
void setOptionsAfterPositionalArgumentsMode(OptionsAfterPositionalArgumentsMode mode);
bool addOption(const QCommandLineOption &commandLineOption); bool addOption(const QCommandLineOption &commandLineOption);
bool addOptions(const QList<QCommandLineOption> &options); bool addOptions(const QList<QCommandLineOption> &options);