From f78950d3648c884384faccc48f5b3a18cb7491ab Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 14 Jun 2013 19:47:00 +0200 Subject: QCommandLineParser: stop parsing options after "--". Change-Id: I9cc23a1b7293ba3e9cf5ff062668e17e8856cf5a --- src/corelib/tools/qcommandlineparser.cpp | 30 +++++++--------------- .../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); -- cgit v1.2.1