aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-27 13:44:22 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-27 13:44:22 +0100
commitce050716c2e4f1dd56fbd3fa3290bfe258ea1af6 (patch)
tree08fd568718b2d664003ae65513b940942a808b0e /src/main.cpp
parentUsing boost::program_options instead of libconfig (diff)
downloadsmolbote-ce050716c2e4f1dd56fbd3fa3290bfe258ea1af6.tar.xz
Passing unknown command line parameters to the Configuration
- no longer compiling CookiesForm that we don't use
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp38
1 files changed, 36 insertions, 2 deletions
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 <QCommandLineParser>
-#include <QDir>
#include <QStandardPaths>
#include <iostream>
@@ -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<QCommandLineOption> 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") {