summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2014-07-30 15:47:15 +0200
committerMarc Mutz <marc.mutz@kdab.com>2014-08-06 22:17:22 +0200
commit7f78d547ca843ad58b016b72418f5b9ab47c8148 (patch)
tree575f902ebf83a973521f55b4d2158a473c3abe6e /src/corelib/tools
parenta2ad0ba630da3552b3c2d3d2f09db0a43e4fcc9d (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/tools')
-rw-r--r--src/corelib/tools/qcommandlineoption.cpp47
-rw-r--r--src/corelib/tools/qcommandlineoption.h6
-rw-r--r--src/corelib/tools/qcommandlineparser.cpp22
-rw-r--r--src/corelib/tools/qcommandlineparser.h1
4 files changed, 74 insertions, 2 deletions
diff --git a/src/corelib/tools/qcommandlineoption.cpp b/src/corelib/tools/qcommandlineoption.cpp
index ccf9211b52..02e922d323 100644
--- a/src/corelib/tools/qcommandlineoption.cpp
+++ b/src/corelib/tools/qcommandlineoption.cpp
@@ -96,6 +96,41 @@ public:
*/
/*!
+ Constructs a command line option object with the name \a name.
+
+ The name can be either short or long. If the name is one character in
+ length, it is considered a short name. Option names must not be empty,
+ must not start with a dash or a slash character, must not contain a \c{=}
+ and cannot be repeated.
+
+ \sa setDescription(), setValueName(), setDefaultValues()
+*/
+QCommandLineOption::QCommandLineOption(const QString &name)
+ : d(new QCommandLineOptionPrivate)
+{
+ d->setNames(QStringList(name));
+}
+
+/*!
+ Constructs a command line option object with the names \a names.
+
+ This overload allows to set multiple names for the option, for instance
+ \c{o} and \c{output}.
+
+ The names can be either short or long. Any name in the list that is one
+ character in length is a short name. Option names must not be empty,
+ must not start with a dash or a slash character, must not contain a \c{=}
+ and cannot be repeated.
+
+ \sa setDescription(), setValueName(), setDefaultValues()
+*/
+QCommandLineOption::QCommandLineOption(const QStringList &names)
+ : d(new QCommandLineOptionPrivate)
+{
+ d->setNames(names);
+}
+
+/*!
Constructs a command line option object with the given arguments.
The name of the option is set to \a name.
@@ -110,6 +145,12 @@ public:
In addition, the \a valueName can be set if the option expects a value.
The default value for the option is set to \a defaultValue.
+ In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
+ and later, it no longer is and can be used for C++11-style uniform
+ initialization:
+
+ \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init
+
\sa setDescription(), setValueName(), setDefaultValues()
*/
QCommandLineOption::QCommandLineOption(const QString &name, const QString &description,
@@ -141,6 +182,12 @@ QCommandLineOption::QCommandLineOption(const QString &name, const QString &descr
In addition, the \a valueName can be set if the option expects a value.
The default value for the option is set to \a defaultValue.
+ In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
+ and later, it no longer is and can be used for C++11-style uniform
+ initialization:
+
+ \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init-list
+
\sa setDescription(), setValueName(), setDefaultValues()
*/
QCommandLineOption::QCommandLineOption(const QStringList &names, const QString &description,
diff --git a/src/corelib/tools/qcommandlineoption.h b/src/corelib/tools/qcommandlineoption.h
index 49223cda37..0230586c29 100644
--- a/src/corelib/tools/qcommandlineoption.h
+++ b/src/corelib/tools/qcommandlineoption.h
@@ -52,10 +52,12 @@ class QCommandLineOptionPrivate;
class Q_CORE_EXPORT QCommandLineOption
{
public:
- explicit QCommandLineOption(const QString &name, const QString &description = QString(),
+ explicit QCommandLineOption(const QString &name);
+ explicit QCommandLineOption(const QStringList &names);
+ /*implicit*/ QCommandLineOption(const QString &name, const QString &description,
const QString &valueName = QString(),
const QString &defaultValue = QString());
- explicit QCommandLineOption(const QStringList &names, const QString &description = QString(),
+ /*implicit*/ QCommandLineOption(const QStringList &names, const QString &description,
const QString &valueName = QString(),
const QString &defaultValue = QString());
QCommandLineOption(const QCommandLineOption &other);
diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp
index 168701d13c..f503ed7452 100644
--- a/src/corelib/tools/qcommandlineparser.cpp
+++ b/src/corelib/tools/qcommandlineparser.cpp
@@ -179,6 +179,10 @@ QStringList QCommandLineParserPrivate::aliases(const QString &optionName) const
Example:
\snippet code/src_corelib_tools_qcommandlineparser_main.cpp 0
+ If your compiler supports the C++11 standard, the three addOption() calls in
+ the above example can be simplified:
+ \snippet code/src_corelib_tools_qcommandlineparser_main.cpp cxx11
+
Known limitation: the parsing of Qt options inside QCoreApplication and subclasses
happens before QCommandLineParser exists, so it can't take it into account. This
means any option value that looks like a builtin Qt option, will be treated by
@@ -342,6 +346,24 @@ bool QCommandLineParser::addOption(const QCommandLineOption &option)
}
/*!
+ \since 5.4
+
+ Adds the options \a options to look for while parsing.
+
+ Returns \c false if adding any of the options failed; otherwise returns \c false.
+
+ Cf. addOption() for when it may fail.
+*/
+bool QCommandLineParser::addOptions(const QList<QCommandLineOption> &options)
+{
+ // should be optimized (but it's no worse than what was possible before)
+ bool result = true;
+ for (QList<QCommandLineOption>::const_iterator it = options.begin(), end = options.end(); it != end; ++it)
+ result &= addOption(*it);
+ return result;
+}
+
+/*!
Adds the \c{-v} / \c{--version} option, which displays the version string of the application.
This option is handled automatically by QCommandLineParser.
diff --git a/src/corelib/tools/qcommandlineparser.h b/src/corelib/tools/qcommandlineparser.h
index 946d4dd238..ee606d6cf4 100644
--- a/src/corelib/tools/qcommandlineparser.h
+++ b/src/corelib/tools/qcommandlineparser.h
@@ -66,6 +66,7 @@ public:
void setSingleDashWordOptionMode(SingleDashWordOptionMode parsingMode);
bool addOption(const QCommandLineOption &commandLineOption);
+ bool addOptions(const QList<QCommandLineOption> &options);
QCommandLineOption addVersionOption();
QCommandLineOption addHelpOption();