diff options
| author | Aleix Pol <aleixpol@kde.org> | 2023-09-18 17:05:37 +0200 |
|---|---|---|
| committer | Aleix Pol <aleixpol@kde.org> | 2023-09-21 22:05:06 +0200 |
| commit | 52a5a89ea482b986560f7c50737a8634ebbcf152 (patch) | |
| tree | 1c986c2c6a84fa6addedb1f267ae49826773141a /src/corelib/tools/qcommandlineparser.cpp | |
| parent | 64c50224b95591fc62f43d97debb515e7f415796 (diff) | |
QCommandLineParser: Warn invalid value calls
If the QCommandLineOption doesn't have a valueName, the parser won't
read the argument, therefore returning an empty value. If the developers
are calling ::value on the option, they clearly think it's expected to
get a value but won't ever be getting one, so we better warn them about
it.
Change-Id: I434b94c0b817b5d9d137c17f32b92af363f93eb8
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src/corelib/tools/qcommandlineparser.cpp')
| -rw-r--r-- | src/corelib/tools/qcommandlineparser.cpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp index 94370f49386..2880eedf77a 100644 --- a/src/corelib/tools/qcommandlineparser.cpp +++ b/src/corelib/tools/qcommandlineparser.cpp @@ -820,7 +820,7 @@ bool QCommandLineParser::isSet(const QString &name) const that option is returned. If the option wasn't specified on the command line, the default value is returned. - An empty string is returned if the option does not take a value. + If the option does not take a value, a warning is printed, and an empty string is returned. \sa values(), QCommandLineOption::setDefaultValue(), QCommandLineOption::setDefaultValues() */ @@ -861,8 +861,14 @@ QStringList QCommandLineParser::values(const QString &optionName) const if (it != d->nameHash.cend()) { const qsizetype optionOffset = *it; QStringList values = d->optionValuesHash.value(optionOffset); - if (values.isEmpty()) - values = d->commandLineOptionList.at(optionOffset).defaultValues(); + if (values.isEmpty()) { + const auto &option = d->commandLineOptionList.at(optionOffset); + if (option.valueName().isEmpty()) { + qWarning("QCommandLineParser: option not expecting values: \"%ls\"", + qUtf16Printable(optionName)); + } + values = option.defaultValues(); + } return values; } |
