From ce050716c2e4f1dd56fbd3fa3290bfe258ea1af6 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sat, 27 Jan 2018 13:44:22 +0100 Subject: Passing unknown command line parameters to the Configuration - no longer compiling CookiesForm that we don't use --- src/main.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 70f22be..973ac58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,6 @@ #include "src/mainwindow/mainwindow.h" #include "version.h" #include -#include #include #include @@ -66,7 +65,9 @@ int main(int argc, char **argv) parser.addPositionalArgument("URL", "URL(s) to open"); - parser.process(instance); + // use parse instead of process + // process calls exit() on unknown options + parser.parse(instance.arguments()); #ifdef QT_DEBUG qDebug("config=%s", qUtf8Printable(parser.value(configOption))); @@ -83,6 +84,7 @@ int main(int argc, char **argv) qUtf8Printable(parser.value(defaultConfigOption)), config->read(parser.value(defaultConfigOption)) ? "ok" : "failed"); } + // then load in the user configuration, which will overwrite it if(parser.isSet(configOption)) { qDebug("Reading configuration [%s]: %s", @@ -90,6 +92,38 @@ int main(int argc, char **argv) config->read(parser.value(configOption)) ? "ok" : "failed"); } + // parse command-line overrides + // we assume the users knows what they're doing, so we only pass the unknown options to program_options + // passing any unknown options though will cause it to fail, so we need to filter out the regular options + // unfortunately, QCommandLineParser will only give us the unknown option + // names, so we need to build a list, add them as options, reparse, and then + // we get their values + if(!parser.unknownOptionNames().isEmpty()) { + int _argc = parser.unknownOptionNames().length() + 1; + const char* _argv[_argc]; + + // program_options requires 0 to be the program name, otherwise it seems to fail + _argv[0] = qUtf8Printable(instance.arguments().at(0)); + + // create a list of unknown QCommandLineOption's + // parser.addOptions() takes a list, so this is a QList + QList opts; + for (const QString &opt : parser.unknownOptionNames()) { + QCommandLineOption o(opt, "dummy desc", "dummy value"); + opts.append(o); + } + + // add list and reparse to set the new options + parser.addOptions(opts); + parser.parse(instance.arguments()); + + for(int i = 1; i < _argc; ++i) { + _argv[i] = qUtf8Printable(QString("--%1=%2").arg(opts[i-1].names().at(0), parser.value(opts[i-1]))); + } + + qDebug("Parsing command-line overrides: %s", config->parse(_argc, _argv) ? "ok" : "failed"); + } + // check for other instances // if we socket hasn't been disabled (socket is not none) if(parser.value(socketOption) != "none") { -- cgit v1.2.1