aboutsummaryrefslogtreecommitdiff
path: root/src/commandline.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-27 19:58:22 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-27 19:58:22 +0100
commit63aef9f68f9943e9b1556d2c05953519dc3cea43 (patch)
treecba7003367acbe8c9326967a16eb629570a14c71 /src/commandline.cpp
parenthelp and version option work again (diff)
downloadsmolbote-63aef9f68f9943e9b1556d2c05953519dc3cea43.tar.xz
Split cmd parsing from main into CommandLine class
Diffstat (limited to 'src/commandline.cpp')
-rw-r--r--src/commandline.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/commandline.cpp b/src/commandline.cpp
new file mode 100644
index 0000000..adfdf52
--- /dev/null
+++ b/src/commandline.cpp
@@ -0,0 +1,117 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: https://neueland.iserlohn-fortress.net/smolbote.hg
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "commandline.h"
+#include "version.h"
+#include <QStandardPaths>
+#include <iomanip>
+#include <iostream>
+
+void printOption(const QCommandLineOption &option)
+{
+ std::cout << "- "
+ << std::setw(20) << std::left << option.names().join(", ").toStdString()
+ << std::setw(40) << std::left << option.description().toStdString()
+ << std::setw(30) << std::left << option.defaultValues().join(", ").toStdString()
+ << std::endl;
+}
+
+QString defaultUserConfigLocation()
+{
+ // 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";
+
+ return path;
+}
+
+CommandLine::CommandLine()
+ : QCommandLineParser()
+ , helpOption(addHelpOption())
+ , versionOption(addVersionOption())
+ , configOption({ "c", "config" }, "Set configuration file.", "path", defaultUserConfigLocation())
+ , defaultConfigOption("default-config", "Set the default configuration file.", "path", "")
+ , profileOption({ "p", "profile" }, "Use this profile.", "profile", "")
+ , socketOption("socket", "Local server socket", "name", "")
+ , newWindowOption("in-new-window", "Open URL in new window")
+{
+ setApplicationDescription("yet another no-frills browser");
+
+ addOption(configOption);
+ addOption(defaultConfigOption);
+ addOption(profileOption);
+ addOption(socketOption);
+ addOption(newWindowOption);
+
+ addPositionalArgument("URL", "URL(s) to open");
+}
+
+void CommandLine::parseCommandLine(const QCoreApplication &app)
+{
+ QCommandLineParser::parse(app.arguments());
+
+ application = const_cast<QCoreApplication *>(&app);
+
+ if(isSet(helpOption)) {
+ printHelp(0);
+ }
+
+ if(isSet(versionOption)) {
+ printVersion();
+ }
+
+ // create a list of unknown QCommandLineOption's
+ // parser.addOptions() takes a list, so this is a QList
+
+ for(const QString &opt : unknownOptionNames()) {
+ QCommandLineOption o(opt, "dummy desc", "dummy value");
+ opts.append(o);
+ }
+
+ // add list and reparse to set the new options
+ addOptions(opts);
+ parse(app.arguments());
+}
+void CommandLine::printHelp(int exitCode)
+{
+ std::cout << "Usage: " << application->arguments().at(0).toStdString() << " [options] URL" << std::endl;
+ std::cout << application->applicationName().toStdString() << " " << SMOLBOTE_DESCRIBE << ": "
+ << applicationDescription().toStdString() << std::endl
+ << std::endl;
+
+ std::cout << "Options: " << std::endl;
+ printOption(helpOption);
+ printOption(versionOption);
+ printOption(configOption);
+ printOption(defaultConfigOption);
+ printOption(profileOption);
+ printOption(socketOption);
+ printOption(newWindowOption);
+ std::cout << std::endl;
+
+ std::cout << "You can also overwrite configuration options using the syntax: " << std::endl
+ << "--browser.setting.path=value" << std::endl
+ << std::endl;
+
+ std::cout << "Arguments: " << std::endl;
+ std::cout << "- "
+ << std::setw(20) << std::left << "URL"
+ << std::setw(40) << std::left << "URL(s) to open"
+ << std::endl
+ << std::endl;
+
+ exit(0);
+}
+void CommandLine::printVersion()
+{
+ std::cout << application->applicationName().toStdString() << " " << SMOLBOTE_DESCRIBE << std::endl;
+ exit(0);
+}