From 574508b9b6496400388aec420c6abf3ef9911868 Mon Sep 17 00:00:00 2001 From: Jonathan Marten Date: Mon, 6 Dec 2021 11:33:59 +0000 Subject: Allow kreadconfig/kwriteconfig to access the root group The default group if "--group" is not specified is "KDE". Trying to specify a null group name causes an assert in KConfigGroup::groupImpl(). Explicitly detect and give an error message for a null group name. Mention in the help text that "" can be used to specify the root group - this string comes from KConfigGroupPrivate::name(). Add the missing parser.addHelpOption() for kreadconfig. Update the help strings. Apply clang-format changes. I18N: --- src/kreadconfig/kreadconfig.cpp | 20 ++++++++++++++++---- src/kreadconfig/kwriteconfig.cpp | 16 ++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/kreadconfig/kreadconfig.cpp b/src/kreadconfig/kreadconfig.cpp index 01d44326..7e96a17a 100644 --- a/src/kreadconfig/kreadconfig.cpp +++ b/src/kreadconfig/kreadconfig.cpp @@ -41,12 +41,14 @@ int main(int argc, char **argv) QCoreApplication app(argc, argv); QCommandLineParser parser; + parser.addHelpOption(); parser.addOption( QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use instead of global config"), QStringLiteral("file"))); - parser.addOption(QCommandLineOption(QStringLiteral("group"), - QCoreApplication::translate("main", "Group to look in. Use repeatedly for nested groups."), - QStringLiteral("group"), - QStringLiteral("KDE"))); + parser.addOption( + QCommandLineOption(QStringLiteral("group"), + QCoreApplication::translate("main", "Group to look in. Use \"\" for the root group, or use repeatedly for nested groups."), + QStringLiteral("group"), + QStringLiteral("KDE"))); parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key"))); parser.addOption(QCommandLineOption(QStringLiteral("default"), QCoreApplication::translate("main", "Default value"), QStringLiteral("value"))); parser.addOption(QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate("main", "Type of variable"), QStringLiteral("type"))); @@ -75,6 +77,16 @@ int main(int argc, char **argv) } KConfigGroup cfgGroup = konfig->group(QString()); for (const QString &grp : groups) { + if (grp.isEmpty()) { + fprintf(stderr, + "%s: %s\n", + qPrintable(QCoreApplication::applicationName()), + qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"\" for the root group"))); + if (configMustDeleted) { + delete konfig; + } + return 2; + } cfgGroup = cfgGroup.group(grp); } diff --git a/src/kreadconfig/kwriteconfig.cpp b/src/kreadconfig/kwriteconfig.cpp index 20036331..af39325e 100644 --- a/src/kreadconfig/kwriteconfig.cpp +++ b/src/kreadconfig/kwriteconfig.cpp @@ -23,10 +23,11 @@ int main(int argc, char **argv) parser.addHelpOption(); parser.addOption( QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use instead of global config"), QStringLiteral("file"))); - parser.addOption(QCommandLineOption(QStringLiteral("group"), - QCoreApplication::translate("main", "Group to look in. Use repeatedly for nested groups."), - QStringLiteral("group"), - QStringLiteral("KDE"))); + parser.addOption( + QCommandLineOption(QStringLiteral("group"), + QCoreApplication::translate("main", "Group to look in. Use \"\" for the root group, or use repeatedly for nested groups."), + QStringLiteral("group"), + QStringLiteral("KDE"))); parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key"))); parser.addOption( QCommandLineOption(QStringLiteral("type"), @@ -61,6 +62,13 @@ int main(int argc, char **argv) KConfigGroup cfgGroup = konfig->group(QString()); for (const QString &grp : groups) { + if (grp.isEmpty()) { + fprintf(stderr, + "%s: %s\n", + qPrintable(QCoreApplication::applicationName()), + qPrintable(QCoreApplication::translate("main", "Group name cannot be empty, use \"\" for the root group"))); + return 2; + } cfgGroup = cfgGroup.group(grp); } -- cgit v1.2.1