aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration
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
parentAdd tile and cascade actions to Window menu (diff)
downloadsmolbote-e1f240f490324f8c6f4d8132630ad85756f6d191.tar.xz
Merge CommandLine functionality into Configuration
Diffstat (limited to 'lib/configuration')
-rw-r--r--lib/configuration/configuration.cpp83
-rw-r--r--lib/configuration/configuration.h23
2 files changed, 80 insertions, 26 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());
}
diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h
index 5b59766..5bb9f5d 100644
--- a/lib/configuration/configuration.h
+++ b/lib/configuration/configuration.h
@@ -23,10 +23,8 @@ public:
explicit Configuration();
~Configuration();
- static QString defaultUserConfigLocation();
-
- bool read(const QString &path);
- bool parse(int argc, char **argv);
+ bool parseConfigFile(const std::string &path);
+ bool parseCommandLine(int argc, char **argv);
template <typename T>
std::optional<T> value(const char *path) const
@@ -66,13 +64,26 @@ public:
const std::vector<boost::shared_ptr<boost::program_options::option_description>> &options()
{
- return desc.options();
+ return configuration_desc.options();
}
QHash<QString, QString> section(const std::string &prefix) const;
+ QStringList positionalArguments() const
+ {
+ QStringList l;
+ if(vm.count("url")) {
+ std::vector<std::string> urls = vm["url"].as<std::vector<std::string>>();
+ for(const std::string &s : urls) {
+ l.append(QString::fromStdString(s));
+ }
+ }
+ return l;
+ }
private:
- boost::program_options::options_description desc;
+ boost::program_options::options_description commandLine_desc;
+ boost::program_options::options_description configuration_desc;
+ boost::program_options::positional_options_description arguments_desc;
boost::program_options::variables_map vm;
std::string m_homePath;