aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/configuration.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-04-22 16:10:18 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-04-22 16:10:18 +0200
commite1f240f490324f8c6f4d8132630ad85756f6d191 (patch)
treed0b9cb561d34a361297a72823a897cbb235cbae9 /lib/configuration/configuration.cpp
parentAdd tile and cascade actions to Window menu (diff)
downloadsmolbote-e1f240f490324f8c6f4d8132630ad85756f6d191.tar.xz
Merge CommandLine functionality into Configuration
Diffstat (limited to 'lib/configuration/configuration.cpp')
-rw-r--r--lib/configuration/configuration.cpp83
1 files changed, 63 insertions, 20 deletions
diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp
index 48f051d..7032a1d 100644
--- a/lib/configuration/configuration.cpp
+++ b/lib/configuration/configuration.cpp
@@ -10,16 +10,49 @@
#include <QStandardPaths>
#include <fstream>
#include <boost/algorithm/string/predicate.hpp>
+#include <iostream>
+#include <QCoreApplication>
namespace po = boost::program_options;
+inline std::string defaultUserConfigLocation()
+{
+ // try to locate an existing config
+ QString path = QStandardPaths::locate(QStandardPaths::ConfigLocation, "smolbote/smolbote.cfg");
+
+ // it's possible there is no config, so set the path properly
+ if(path.isEmpty())
+ path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/smolbote/smolbote.cfg";
+
+ return path.toStdString();
+}
+
+constexpr const char *defaultSocketPath()
+{
+#if defined(Q_OS_UNIX)
+ // could be a path such as "/tmp/foo"
+ return "/tmp/smolbote.socket";
+#elif defined(Q_OS_WIN32)
+ // could be a pipe path such as "\\.\pipe\foo"
+ return "\\\\.\\pipe\\smolbote_socket";
+#endif
+}
+
Configuration::Configuration()
{
m_homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString();
- // create description
- desc.add_options()
+ commandLine_desc.add_options()
+ ("help,h", "Display this help.")
+ ("version,v", "Display version information.")
+
+ ("config,c", po::value<std::string>()->default_value(defaultUserConfigLocation()), "Set the configuration file.")
+ ("socket,s", po::value<std::string>()->default_value(defaultSocketPath()), "Local server socket")
+
+ ("url", po::value<std::vector<std::string>>(), "URLs")
+ ;
+ configuration_desc.add_options()
// main window ui
("mainwindow.height", po::value<int>()->default_value(720))
("mainwindow.width", po::value<int>()->default_value(1280))
@@ -82,46 +115,56 @@ Configuration::Configuration()
("downloads.path", po::value<std::string>()->default_value("~/Downloads"))
("downloads.shortcut", po::value<std::string>()->default_value("Ctrl+D"))
;
+
+ arguments_desc.add("url", -1);
}
Configuration::~Configuration() = default;
-QString Configuration::defaultUserConfigLocation()
-{
- // try to locate an existing config
- QString path = QStandardPaths::locate(QStandardPaths::ConfigLocation, "smolbote/smolbote.cfg");
-
- // it's possible there is no config, so set the path properly
- if(path.isEmpty())
- path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/smolbote/smolbote.cfg";
-
- return path;
-}
-
-bool Configuration::read(const QString &path)
+bool Configuration::parseConfigFile(const std::string &path)
{
- std::ifstream f(path.toStdString(), std::ifstream::in);
+ std::ifstream f(path, std::ifstream::in);
// parse_config_file(file, options_description, allow_unregistered)
- po::store(po::parse_config_file(f, desc, true), vm);
+ po::store(po::parse_config_file(f, configuration_desc, true), vm);
return true;
}
-bool Configuration::parse(int argc, char **argv)
+bool Configuration::parseCommandLine(int argc, char **argv)
{
try {
- po::store(po::parse_command_line(argc, argv, desc), vm);
+ auto cmd = po::command_line_parser(argc, argv);
+ po::options_description desc;
+ desc.add(commandLine_desc).add(configuration_desc);
+ cmd.options(desc);
+ cmd.positional(arguments_desc);
+ po::store(cmd.run(), vm);
} catch(const po::error &e) {
+ qWarning("Error parsing command line: %s", e.what());
return false;
}
+ if(vm.count("help")) {
+ std::cout << qUtf8Printable(qApp->applicationName()) << " " << qUtf8Printable(qApp->applicationVersion()) << ": yet another no-frills browser" << std::endl;
+ std::cout << "Usage: " << qUtf8Printable(qApp->arguments().at(0)) << " [options] URL(s)" << std::endl;
+
+ std::cout << std::endl << "Command-line Options: " << std::endl << commandLine_desc << std::endl;
+ std::cout << std::endl << "Configuration Options: " << std::endl << configuration_desc << std::endl;
+ exit(0);
+ }
+
+ if(vm.count("version")) {
+ std::cout << qUtf8Printable(qApp->applicationName()) << " " << qUtf8Printable(qApp->applicationVersion()) << std::endl;
+ exit(0);
+ }
+
return true;
}
QHash<QString, QString> Configuration::section(const std::string &prefix) const
{
QHash<QString, QString> v;
- for(auto &s : desc.options()) {
+ for(auto &s : configuration_desc.options()) {
if(boost::starts_with(s->long_name(), prefix)) {
v[s->long_name().c_str()] = QString::fromStdString(value<std::string>(s->long_name().c_str()).value());
}