diff options
author | David Faure <faure+bluesystems@kde.org> | 2013-06-14 19:47:00 +0200 |
---|---|---|
committer | David Faure <faure+bluesystems@kde.org> | 2013-06-19 09:44:32 +0200 |
commit | f78950d3648c884384faccc48f5b3a18cb7491ab (patch) | |
tree | 5edb38673c37a8c815940944b789c1868b8fe181 | |
parent | 9287b0ae695e8fc8268c0957ba22cc654126c0b4 (diff) | |
download | qtbase-cli_parser.tar.gz |
QCommandLineParser: stop parsing options after "--".cli_parser
Change-Id: I9cc23a1b7293ba3e9cf5ff062668e17e8856cf5a
-rw-r--r-- | src/corelib/tools/qcommandlineparser.cpp | 30 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp | 16 |
2 files changed, 25 insertions, 21 deletions
diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp index fb72073d65..152f979cde 100644 --- a/src/corelib/tools/qcommandlineparser.cpp +++ b/src/corelib/tools/qcommandlineparser.cpp @@ -58,7 +58,6 @@ class QCommandLineParserPrivate public: inline QCommandLineParserPrivate() : builtinVersionOption(false), - parseAfterDoubleDash(true), needsParsing(true) { } @@ -94,15 +93,6 @@ public: //! Documentation for remaining arguments QString remainingArgsHelpText; - /* - Boolean variable whether or not to stop the command line argument - parsing after the double dash occurrence without any options names involved - ('--'). - - Set to \c true by default. - */ - bool parseAfterDoubleDash; - //! True if parse() needs to be called bool needsParsing; }; @@ -355,10 +345,9 @@ void QCommandLineParserPrivate::ensureParsed(const char *method) Returns true if the command line parsing was successful; otherwise returns false. - Any results from a previous parse operation are removed. If - \c m_bStopParsingAtDoubleDash is \c true the parser will not look for - further options once it encounters the option "--"; this does not - include when "--" follows an option that requires a value. + Any results from a previous parse operation are removed. + The parser will not look for further options once it encounters the option + "--"; this does not include when "--" follows an option that requires a value. Options that were successfully recognized, and their values, are removed from the input list. If \c m_bRemoveUnknownLongNames is @@ -379,6 +368,7 @@ void QCommandLineParserPrivate::parse(const QStringList &args) const QLatin1Char slashChar('/'); const QLatin1Char assignChar('='); + bool doubleDashFound = false; remainingArgumentList.clear(); optionNames.clear(); unknownOptionNames.clear(); @@ -390,7 +380,9 @@ void QCommandLineParserPrivate::parse(const QStringList &args) for (QStringList::const_iterator argumentIterator = arguments.begin(); argumentIterator != arguments.end() ; ++argumentIterator) { QString argument = *argumentIterator; - if (argument.startsWith(doubleDashString)) { + if (doubleDashFound) { + remainingArgumentList.append(argument); + } else if (argument.startsWith(doubleDashString)) { if (argument.length() > 2) { QString optionName = argument.mid(2).section(assignChar, 0, 0); @@ -411,12 +403,8 @@ void QCommandLineParserPrivate::parse(const QStringList &args) } else { unknownOptionNames.append(optionName); } - } - else { - if (parseAfterDoubleDash == true) - remainingArgumentList.append(argument); - else - break; + } else { + doubleDashFound = true; } } diff --git a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp index 7dbc308192..9587ffac39 100644 --- a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp +++ b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp @@ -61,6 +61,7 @@ private slots: void testValueNotSet(); void testMultipleValuesOption(); void testUnknownOptionErrorHandling(); + void testDoubleDash(); void testProcessNotCalled(); // QProcess-based tests using qcommandlineparser_test_helper @@ -213,6 +214,21 @@ void tst_QCommandLineParser::testUnknownOptionErrorHandling() QCOMPARE(parser.unknownOptionNames(), QStringList() << "foobar"); } +void tst_QCommandLineParser::testDoubleDash() +{ + QCoreApplication app(empty_argc, empty_argv); + QCommandLineParser parser; + parser.addOption(QCommandLineOption(QStringList() << "o" << "output", QStringLiteral("Output file"), QStringLiteral("filename"))); + parser.parse(QStringList() << "tst_qcommandlineparser" << "--output" << "foo"); + QCOMPARE(parser.value("output"), QString("foo")); + QCOMPARE(parser.remainingArguments(), QStringList()); + QCOMPARE(parser.unknownOptionNames(), QStringList()); + parser.parse(QStringList() << "tst_qcommandlineparser" << "--" << "--output" << "bar" << "-b" << "bleh"); + QCOMPARE(parser.value("output"), QString()); + QCOMPARE(parser.remainingArguments(), QStringList() << "--output" << "bar" << "-b" << "bleh"); + QCOMPARE(parser.unknownOptionNames(), QStringList()); +} + void tst_QCommandLineParser::testProcessNotCalled() { QCoreApplication app(empty_argc, empty_argv); |