diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2014-07-30 15:47:15 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2014-08-06 22:17:22 +0200 |
commit | 7f78d547ca843ad58b016b72418f5b9ab47c8148 (patch) | |
tree | 575f902ebf83a973521f55b4d2158a473c3abe6e /src/corelib/doc | |
parent | a2ad0ba630da3552b3c2d3d2f09db0a43e4fcc9d (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/doc')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineoption.cpp | 11 | ||||
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_tools_qcommandlineparser_main.cpp | 17 |
2 files changed, 28 insertions, 0 deletions
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 <QCommandLineOption> +#include <QCommandLineParser> 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>.", "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>.", "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 <directory>."), + QCoreApplication::translate("main", "directory")}, + }); +//! [cxx11] +} |