diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 104 |
1 files changed, 24 insertions, 80 deletions
diff --git a/src/main.cpp b/src/main.cpp index ae40f60..51087ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,10 +7,10 @@ */ #include "browser.h" +#include "commandline.h" #include "src/mainwindow/mainwindow.h" #include "version.h" -#include <QCommandLineParser> -#include <QStandardPaths> +#include <iomanip> #include <iostream> // startup time measuring @@ -29,75 +29,31 @@ int main(int argc, char **argv) timer.start(); #endif - QCommandLineParser parser; - parser.setApplicationDescription("yet another no-frills browser"); - QCommandLineOption helpOption = parser.addHelpOption(); - QCommandLineOption versionOption = parser.addVersionOption(); - - // user config, ~/.config/smolbote/smolbote.cfg or empty if there is none - QCommandLineOption configOption({ "c", "config" }, "Set configuration file.", "path"); - { - // try to locate an existing config - QString path = QStandardPaths::locate(QStandardPaths::AppConfigLocation, "smolbote.cfg"); - - // it's possible there is no config, so set the path properly - if(path.isEmpty()) - path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/smolbote.cfg"; - - configOption.setDefaultValue(path); - } - parser.addOption(configOption); - - // default config, :/poi.cfg - QCommandLineOption defaultConfigOption("default-config", "Set the default configuration file.", "path"); - parser.addOption(defaultConfigOption); - - QCommandLineOption profileOption({ "p", "profile" }, "Use this profile.", "PROFILE"); - profileOption.setDefaultValue(""); - parser.addOption(profileOption); - - QCommandLineOption socketOption("socket", "Set socket to use for IPC, leave blank for default, 'none' to disable.", "name"); - socketOption.setDefaultValue(""); - parser.addOption(socketOption); - - QCommandLineOption newWindowOption("in-new-window", "Open URL in new window"); - parser.addOption(newWindowOption); - - parser.addPositionalArgument("URL", "URL(s) to open"); - - // use parse instead of process - // process calls exit() on unknown options - parser.parse(instance.arguments()); - - if(parser.isSet(helpOption)) { - parser.showHelp(0); - } - - if(parser.isSet(versionOption)) { - parser.showVersion(); - } + CommandLine parser; + parser.parseCommandLine(instance); #ifdef QT_DEBUG - qDebug("config=%s", qUtf8Printable(parser.value(configOption))); - qDebug("default-config=%s", qUtf8Printable(parser.value(defaultConfigOption))); - qDebug("socket=%s", qUtf8Printable(parser.value(socketOption))); - qDebug("profile=%s", qUtf8Printable(parser.value(profileOption))); + qDebug("config=%s", qUtf8Printable(parser.value(parser.configOption))); + qDebug("default-config=%s", qUtf8Printable(parser.value(parser.defaultConfigOption))); + qDebug("profile=%s", qUtf8Printable(parser.value(parser.profileOption))); + qDebug("socket=%s", qUtf8Printable(parser.value(parser.socketOption))); + #endif std::shared_ptr<Configuration> config = std::make_shared<Configuration>(); // first load the default configuration - if(parser.isSet(defaultConfigOption)) { + if(!parser.value(parser.defaultConfigOption).isEmpty()) { qDebug("Reading default configuration [%s]: %s", - qUtf8Printable(parser.value(defaultConfigOption)), - config->read(parser.value(defaultConfigOption)) ? "ok" : "failed"); + qUtf8Printable(parser.value(parser.defaultConfigOption)), + config->read(parser.value(parser.defaultConfigOption)) ? "ok" : "failed"); } // then load in the user configuration, which will overwrite it - if(parser.isSet(configOption)) { + if(!parser.value(parser.configOption).isEmpty()) { qDebug("Reading configuration [%s]: %s", - qUtf8Printable(parser.value(configOption)), - config->read(parser.value(configOption)) ? "ok" : "failed"); + qUtf8Printable(parser.value(parser.configOption)), + config->read(parser.value(parser.configOption)) ? "ok" : "failed"); } // parse command-line overrides @@ -106,27 +62,15 @@ int main(int argc, char **argv) // 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; + if(!parser.opts.isEmpty()) { + int _argc = parser.opts.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]))); + _argv[i] = qUtf8Printable(QString("--%1=%2").arg(parser.opts[i - 1].names().at(0), parser.value(parser.opts[i - 1]))); } qDebug("Parsing command-line overrides: %s", config->parse(_argc, _argv) ? "ok" : "failed"); @@ -134,22 +78,22 @@ int main(int argc, char **argv) // check for other instances // if we socket hasn't been disabled (socket is not none) - if(parser.value(socketOption) != "none") { - bool bindOk = instance.bindLocalSocket(parser.value(socketOption)); + if(parser.value(parser.socketOption) != "none") { + bool bindOk = instance.bindLocalSocket(parser.value(parser.socketOption)); if(bindOk) { qDebug("Connected to local socket: %s", qUtf8Printable(instance.serverName())); } else { // pass arguments to new instance - return instance.sendMessage(parser.value(profileOption), parser.isSet(newWindowOption), parser.positionalArguments()); + return instance.sendMessage(parser.value(parser.profileOption), parser.isSet(parser.newWindowOption), parser.positionalArguments()); } } instance.setConfiguration(config); - if(parser.isSet(profileOption)) - instance.createSession(parser.value(profileOption), parser.isSet(newWindowOption), parser.positionalArguments()); + if(parser.isSet(parser.profileOption)) + instance.createSession(parser.value(parser.profileOption), parser.isSet(parser.newWindowOption), parser.positionalArguments()); else - instance.createSession(QString::fromStdString(config->value<std::string>("browser.profile").value()), parser.isSet(newWindowOption), parser.positionalArguments()); + instance.createSession(QString::fromStdString(config->value<std::string>("browser.profile").value()), parser.isSet(parser.newWindowOption), parser.positionalArguments()); #ifdef QT_DEBUG qDebug("Startup complete in %lldms", timer.elapsed()); |