summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2021-02-26 23:06:15 +0100
committerDavid Faure <david.faure@kdab.com>2021-03-01 23:03:00 +0100
commit101581484bd278ce4a5f329ffb13c9e0b0e4c131 (patch)
treeabe01427f1ac53e939fb2acfb625d797e4a82c11
parentb579d8a05bb6d4113feebece17d443e676266ab8 (diff)
QCommandLineParser: fix crash if there's no QCoreApplication
It'll display <executable_name> instead in the help output. Fixes: QTBUG-91430 Change-Id: Ib6211b24cdaa4683a4f62c90b5a1a20ba69f1cff Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/tools/qcommandlineparser.cpp7
-rw-r--r--tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp29
2 files changed, 33 insertions, 3 deletions
diff --git a/src/corelib/tools/qcommandlineparser.cpp b/src/corelib/tools/qcommandlineparser.cpp
index 8e575be635..f294449b5f 100644
--- a/src/corelib/tools/qcommandlineparser.cpp
+++ b/src/corelib/tools/qcommandlineparser.cpp
@@ -1125,10 +1125,11 @@ QString QCommandLineParserPrivate::helpText(bool includeQtOptions) const
const QLatin1Char nl('\n');
QString text;
QString usage;
- usage += QCoreApplication::instance()->arguments().constFirst(); // executable name
+ // executable name
+ usage += qApp ? QCoreApplication::arguments().constFirst() : QStringLiteral("<executable_name>");
QList<QCommandLineOption> options = commandLineOptionList;
- if (includeQtOptions)
- QCoreApplication::instance()->d_func()->addQtOptions(&options);
+ if (includeQtOptions && qApp)
+ qApp->d_func()->addQtOptions(&options);
if (!options.isEmpty())
usage += QLatin1Char(' ') + QCommandLineParser::tr("[options]");
for (const PositionalArgumentDefinition &arg : positionalArgumentDefinitions)
diff --git a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp
index f8366d4142..aea3550452 100644
--- a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp
+++ b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp
@@ -64,6 +64,7 @@ private slots:
void testDefaultValue();
void testProcessNotCalled();
void testEmptyArgsList();
+ void testNoApplication();
void testMissingOptionValue();
void testStdinArgument_data();
void testStdinArgument();
@@ -393,6 +394,34 @@ void tst_QCommandLineParser::testEmptyArgsList()
QVERIFY(!parser.parse(QStringList())); // invalid call, argv[0] is missing
}
+void tst_QCommandLineParser::testNoApplication()
+{
+ QCommandLineOption option(QStringLiteral("param"), QStringLiteral("Pass parameter to the backend."));
+ option.setValueName("key=value");
+ QCommandLineParser parser;
+ QVERIFY(parser.addOption(option));
+ {
+ QVERIFY(parser.parse(QStringList() << "tst" << "--param" << "key1=value1"));
+ QVERIFY(parser.isSet("param"));
+ QCOMPARE(parser.values("param"), QStringList() << "key1=value1");
+ QCOMPARE(parser.value("param"), QString("key1=value1"));
+ }
+ {
+ QVERIFY(parser.parse(QStringList() << "tst" << "--param" << "key1=value1" << "--param" << "key2=value2"));
+ QVERIFY(parser.isSet("param"));
+ QCOMPARE(parser.values("param"), QStringList() << "key1=value1" << "key2=value2");
+ QCOMPARE(parser.value("param"), QString("key2=value2"));
+ }
+
+ const QString expected =
+ "Usage: <executable_name> [options]\n"
+ "\n"
+ "Options:\n"
+ " --param <key=value> Pass parameter to the backend.\n";
+
+ QCOMPARE(parser.helpText(), expected);
+}
+
void tst_QCommandLineParser::testMissingOptionValue()
{
QCoreApplication app(empty_argc, empty_argv);