QCommandLineParser: warn if defining a duplicate option
Fixes: QTBUG-74907 Change-Id: I3741a5241515dfaf4353458a9ef13ceaeb9fea0b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
bcd2fa484a
commit
90c86d738e
|
|
@ -362,8 +362,10 @@ bool QCommandLineParser::addOption(const QCommandLineOption &option)
|
|||
|
||||
if (!optionNames.isEmpty()) {
|
||||
for (const QString &name : optionNames) {
|
||||
if (d->nameHash.contains(name))
|
||||
if (d->nameHash.contains(name)) {
|
||||
qWarning() << "QCommandLineParser: already having an option named" << name;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
d->commandLineOptionList.append(option);
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ private slots:
|
|||
|
||||
// In-process tests
|
||||
void testInvalidOptions();
|
||||
void testDuplicateOption();
|
||||
void testPositionalArguments();
|
||||
void testBooleanOption_data();
|
||||
void testBooleanOption();
|
||||
|
|
@ -104,6 +105,15 @@ void tst_QCommandLineParser::testInvalidOptions()
|
|||
QVERIFY(!parser.addOption(QCommandLineOption(QStringLiteral("-v"), QStringLiteral("Displays version information."))));
|
||||
}
|
||||
|
||||
void tst_QCommandLineParser::testDuplicateOption()
|
||||
{
|
||||
QCoreApplication app(empty_argc, empty_argv);
|
||||
QCommandLineParser parser;
|
||||
QVERIFY(parser.addOption(QCommandLineOption(QStringLiteral("h"), QStringLiteral("Hostname."), QStringLiteral("hostname"))));
|
||||
QTest::ignoreMessage(QtWarningMsg, "QCommandLineParser: already having an option named \"h\"");
|
||||
parser.addHelpOption();
|
||||
}
|
||||
|
||||
void tst_QCommandLineParser::testPositionalArguments()
|
||||
{
|
||||
QCoreApplication app(empty_argc, empty_argv);
|
||||
|
|
|
|||
Loading…
Reference in New Issue