From 7f78d547ca843ad58b016b72418f5b9ab47c8148 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 30 Jul 2014 15:47:15 +0200 Subject: QCommandLineParser: support extremely concise option configuration in C++11 The goal of this commit to make the code in the test work: QCommandLineParser parser; parser.addOptions({ { "a", "The A option." }, { { "v", "verbose" }, "The verbose option." }, { { "i", "infile" }, "The input file.", "value" }, }); For this, QCommandLineParser needs a version of addOption that can take a list of options. That's what addOptions() is for. More importantly, the QCommandLineOption ctors mustn't be explicit. OTOH, any implicit conversion from QString or QStringList to QCommandLineOption is also undesirable. To solve this dilemma, add new QCommandLineOption ctors that just take one argument and are explicit, and make the existing ctors implicit. In order to avoid ambiguities, remove the default values of their resp. 2nd arguments. The new ctors are by intention not \since 5.4, as they are completely transparent to the user. Et voila, even better than getopt_long(3). [ChangeLog][QtCore][QCommandLineParser] Added addOptions() method. Change-Id: I5e779f3406cd0f6c8ec6ecbf6c8074af226de300 Reviewed-by: Thiago Macieira --- .../qcommandlineparser/tst_qcommandlineparser.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp index ce30710f7f..dcc93eaa86 100644 --- a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp +++ b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp @@ -77,6 +77,7 @@ private slots: void testStdinArgument(); void testSingleDashWordOptionModes_data(); void testSingleDashWordOptionModes(); + void testCpp11StyleInitialization(); // QProcess-based tests using qcommandlineparser_test_helper void testVersionOption(); @@ -450,6 +451,27 @@ void tst_QCommandLineParser::testSingleDashWordOptionModes() QCOMPARE(parser.unknownOptionNames(), QStringList()); } +void tst_QCommandLineParser::testCpp11StyleInitialization() +{ +#if defined(Q_COMPILER_INITIALIZER_LISTS) && defined(Q_COMPILER_UNIFORM_INIT) + QCoreApplication app(empty_argc, empty_argv); + + QCommandLineParser parser; + // primarily check that this compiles: + parser.addOptions({ + { "a", "The A option." }, + { { "v", "verbose" }, "The verbose option." }, + { { "i", "infile" }, "The input file.", "value" }, + }); + // but do a very basic functionality test, too: + QVERIFY(parser.parse({"tst_QCommandLineParser", "-a", "-vvv", "--infile=in.txt"})); + QCOMPARE(parser.optionNames(), (QStringList{"a", "v", "v", "v", "infile"})); + QCOMPARE(parser.value("infile"), QString("in.txt")); +#else + QSKIP("This test requires C++11 uniform initialization support in the compiler."); +#endif +} + void tst_QCommandLineParser::testVersionOption() { #ifdef QT_NO_PROCESS -- cgit v1.2.3