From 53594eabccfb7cd94f4b4d42745af1c0c21fe1ec Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 7 Dec 2018 20:47:51 +0100 Subject: Configuration: parse command line after parsing config file - Split CommandLine off Configuration --- lib/configuration/commandline.cpp | 59 +++++++++++++++++++++++++++ lib/configuration/commandline.h | 79 +++++++++++++++++++++++++++++++++++++ lib/configuration/configuration.cpp | 22 +---------- lib/configuration/configuration.h | 13 +----- lib/configuration/meson.build | 2 +- 5 files changed, 142 insertions(+), 33 deletions(-) create mode 100644 lib/configuration/commandline.cpp create mode 100644 lib/configuration/commandline.h (limited to 'lib') diff --git a/lib/configuration/commandline.cpp b/lib/configuration/commandline.cpp new file mode 100644 index 0000000..4581c84 --- /dev/null +++ b/lib/configuration/commandline.cpp @@ -0,0 +1,59 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "commandline.h" +#include "config.h" +#include + +namespace po = boost::program_options; + +inline std::string defaultUserConfigLocation() +{ +#ifdef CONFIG_PATH_CONFIG + return CONFIG_PATH_CONFIG; +#else + // 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(); +#endif +} + +CommandLine::CommandLine(int argc, char **argv) + : m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()) +{ + m_description.add_options() + ("help,h", "Display command-line options list.") + ("version,v", "Display version information.") + ("build", "Display build commit.") + + ("config,c", po::value()->default_value(defaultUserConfigLocation()), "Set the configuration file.") + ("no-remote", "Do not accept or send remote commands.") + + ("session,s", po::value(), "Open the selected session.") + ("pick-session", "Show all available sessions and select which one to open.") + + ("args", po::value>(), "Command(s) and/or URL(s).") + ; + + m_arguments.add("args", -1); + + try { + auto cmd = po::command_line_parser(argc, argv); + cmd.allow_unregistered(); + cmd.options(m_description); + cmd.positional(m_arguments); + po::store(cmd.run(), vm); + } catch(const po::error &e) { + qWarning("Error parsing cmd: %s", e.what()); + } +} diff --git a/lib/configuration/commandline.h b/lib/configuration/commandline.h new file mode 100644 index 0000000..68361cb --- /dev/null +++ b/lib/configuration/commandline.h @@ -0,0 +1,79 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_COMMANDLINE_H +#define SMOLBOTE_COMMANDLINE_H + +#include +#include +#include + +class CommandLine +{ +public: + CommandLine(int argc, char **argv); + + bool exists(const char *path) const + { + return (vm.count(path) > 0); + } + + template + std::optional value(const char *path) const + { + if(vm.count(path) == 0) { + return std::nullopt; + } + + if constexpr(std::is_same_v) { + return std::optional(QString::fromStdString(this->value(path).value())); + //return std::optional(vm[path].as()); + + } else if constexpr(std::is_same_v) { + QStringList r; + for(const std::string &item : this->value>(path).value()) { + r.append(QString::fromStdString(item)); + } + return std::optional(r); + + } else if constexpr(std::is_same_v) { + + if(vm[path].value().type() == typeid(int)) { + return std::optional(std::to_string(vm[path].as())); + } + + if(vm[path].value().type() == typeid(bool)) { + return std::optional(vm[path].as() ? "true" : "false"); + } + + std::string r = vm[path].as(); + + // check if it's a path + if(r.front() == '~') { + r.replace(0, 1, m_homePath); + } + + return std::optional(r); + + } else + return std::optional(vm[path].as()); + } + + const boost::program_options::options_description description() const + { + return m_description; + } + +private: + const std::string m_homePath; + boost::program_options::options_description m_description; + boost::program_options::positional_options_description m_arguments; + boost::program_options::variables_map vm; +}; + +#endif // SMOLBOTE_COMMANDLINE_H diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index 98ab20a..5ddb960 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -36,22 +36,6 @@ Configuration::Configuration(QObject *parent) : QObject(parent) , m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()) { - commandLine_desc.add_options() - ("help,h", "Display command-line options list.") - ("version,v", "Display version information.") - ("build", "Display build commit.") - - ("config,c", po::value()->default_value(defaultUserConfigLocation()), "Set the configuration file.") - ("no-remote", "Do not accept or send remote commands.") - - ("session,s", po::value(), "Open the selected session.") - ("pick-session", "Show all available sessions and select which one to open.") - - ("args", po::value>(), "Command(s) and/or URL(s).") - ; - - arguments_desc.add("args", -1); - configuration_desc.add_options() ("browser.stylesheet", po::value()) ("browser.locale", po::value(), "Set Qt localization.") @@ -148,10 +132,8 @@ bool Configuration::parse(int argc, char **argv) { try { 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); + cmd.allow_unregistered(); + cmd.options(configuration_desc); po::store(cmd.run(), vm); } catch(const po::error &e) { qWarning("Error parsing command line: %s", e.what()); diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h index 1524536..fd18fc9 100644 --- a/lib/configuration/configuration.h +++ b/lib/configuration/configuration.h @@ -29,11 +29,6 @@ public: bool parse(const std::string &path); bool parse(int argc, char **argv); - const std::vector> &options() - { - return configuration_desc.options(); - } - bool exists(const char *path) { return vm.count(path) ? true : false; @@ -110,11 +105,7 @@ public: } QHash section(const std::string &prefix) const; - const boost::program_options::options_description commandlineOptions() const - { - return commandLine_desc; - } - const boost::program_options::options_description configurationOptions() const + const boost::program_options::options_description description() const { return configuration_desc; } @@ -123,9 +114,7 @@ signals: void settingChanged(const std::string &path, const QString &value); private: - 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; const std::string m_homePath; diff --git a/lib/configuration/meson.build b/lib/configuration/meson.build index 79daa78..e93eca9 100644 --- a/lib/configuration/meson.build +++ b/lib/configuration/meson.build @@ -4,7 +4,7 @@ configuration_moc = qt5.preprocess( dependencies: dep_qt5 ) -configuration_lib = static_library('configuration', ['configuration.cpp', configuration_moc], +configuration_lib = static_library('configuration', ['commandline.cpp', 'configuration.cpp', configuration_moc], dependencies: [dep_boost, dep_qt5, dep_genheaders] ) -- cgit v1.2.1