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 --- .../code/src_corelib_tools_qcommandlineoption.cpp | 11 +++++++++++ .../code/src_corelib_tools_qcommandlineparser_main.cpp | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) (limited to 'src/corelib/doc') diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp index 67d5f41b38..6d6d7fe2ed 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp @@ -39,6 +39,7 @@ ****************************************************************************/ #include +#include int main() { @@ -48,4 +49,14 @@ QCommandLineOption verboseOption("verbose", "Verbose mode. Prints out more infor QCommandLineOption outputOption(QStringList() << "o" << "output", "Write generated data into .", "file"); //! [0] +//! [cxx11-init] +QCommandLineParser parser; +parser.addOption({"verbose", "Verbose mode. Prints out more information."}); +//! [cxx11-init] + +//! [cxx11-init-list] +QCommandLineParser parser; +parser.addOption({{"o", "output"}, "Write generated data into .", "file"}); +//! [cxx11-init-list] + } diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp index 257a138d0d..26bc43f194 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp @@ -82,3 +82,20 @@ int main(int argc, char *argv[]) } //! [0] + +void f() { +//! [cxx11] + parser.addOptions({ + // A boolean option with a single name (-p) + {"p", + QCoreApplication::translate("main", "Show progress during copy")}, + // A boolean option with multiple names (-f, --force) + {{"f", "force"}, + QCoreApplication::translate("main", "Overwrite existing files.")}, + // An option with a value + {{"t", "target-directory"}, + QCoreApplication::translate("main", "Copy all source files into ."), + QCoreApplication::translate("main", "directory")}, + }); +//! [cxx11] +} -- cgit v1.2.3