diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2019-11-09 21:05:07 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2019-11-09 21:05:07 +0200 |
commit | eb311838538b80fb3280aa9ab5b57abc22925926 (patch) | |
tree | 71a705fb9594fd4b169aea175047f91287b1ba18 /src | |
parent | Fix manpages not disabled when disabled by meson (diff) | |
download | smolbote-eb311838538b80fb3280aa9ab5b57abc22925926.tar.xz |
Add configuration subcommand
--dump: Write current configuration to stdout and exit
Diffstat (limited to 'src')
-rw-r--r-- | src/builtins.cpp | 41 | ||||
-rw-r--r-- | src/builtins.h | 7 | ||||
-rw-r--r-- | src/main.cpp | 61 |
3 files changed, 74 insertions, 35 deletions
diff --git a/src/builtins.cpp b/src/builtins.cpp index 1af2dc8..ab5942c 100644 --- a/src/builtins.cpp +++ b/src/builtins.cpp @@ -7,20 +7,11 @@ */ #include "builtins.h" +#include "configuration.h" +#include "version.h" #include <QObject> #include <QVersionNumber> #include <iostream> -#include "version.h" -#include <QTranslator> - -inline const char* tr(const QTranslator *translator, const char *text) -{ - const auto t = translator->translate("builtins", text); - if(t.isEmpty()) - return text; - else - return qUtf8Printable(t); -} int builtins::version() { @@ -34,3 +25,31 @@ int builtins::build() std::cout << poi_Version << std::endl; return 0; } + +int builtins::configuration(const std::string &progname, std::vector<std::string>::const_iterator beginargs, std::vector<std::string>::const_iterator endargs) +{ + args::ArgumentParser parser("configuration"); + parser.Prog(progname); + + args::HelpFlag help(parser, "help", "Display this help message and exit.", { 'h', "help" }); + args::Flag dump(parser, "dump", "Dump currently used configuration and exit", { "dump" }); + + try { + parser.ParseArgs(beginargs, endargs); + } catch(args::Help &e) { + std::cout << parser; + return 0; + } catch(args::Error &e) { + std::cerr << e.what() << std::endl; + std::cerr << parser; + return -1; + } + + if(dump) { + Configuration conf; + std::cout << conf << std::endl; + return 0; + } + + return 0; +} diff --git a/src/builtins.h b/src/builtins.h index a3b9b07..088aa23 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -9,13 +9,18 @@ #ifndef SMOLBOTE_BUILTINS_H #define SMOLBOTE_BUILTINS_H -#include <boost/program_options.hpp> +#include <args.hxx> #include <plugininterface.h> +typedef std::function<int(const std::string &, std::vector<std::string>::const_iterator, std::vector<std::string>::const_iterator)> subcommand_func; +typedef std::unordered_map<std::string, subcommand_func> command_map; + namespace builtins { int version(); int build(); + +int configuration(const std::string &progname, std::vector<std::string>::const_iterator beginargs, std::vector<std::string>::const_iterator endargs); } #endif diff --git a/src/main.cpp b/src/main.cpp index 4d4fa8d..7889c08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,16 +18,12 @@ #include <QFile> #include <QPluginLoader> #include <QStandardPaths> -#include <args.hxx> #include <iostream> #include <memory> #include <plugininterface.h> #include <pluginloader.h> #include <spdlog/spdlog.h> -typedef std::function<void(const std::string &, std::vector<std::string>::const_iterator, std::vector<std::string>::const_iterator)> subcommand_func; -typedef std::unordered_map<std::string, subcommand_func> command_map; - // a helper function to join the keys of a command_map into a string inline std::string join_keys(const command_map &map, const std::string sep = ", ") { @@ -59,9 +55,19 @@ int main(int argc, char **argv) #endif const std::vector<std::string> args(argv + 1, argv + argc); - args::ArgumentParser parser("smolbote: yet another no-frills browser", "For more information on usage, refer to the manual page."); + args::ArgumentParser parser("smolbote: yet another no-frills browser", +#ifdef Q_OS_UNIX + "For more information on usage, refer to the manual page." +#else + "For more information on usage, refer to the manual." +#endif + ); parser.Prog(argv[0]); + const command_map commands{ + { "configuration", builtins::configuration } + }; + args::HelpFlag cmd_help(parser, "help", "Display this help message.", { 'h', "help" }); args::Flag cmd_version(parser, "version", "Display version information.", { 'v', "version" }); args::Flag cmd_build(parser, "build", "Display build commit.", { 'b', "build" }); @@ -74,19 +80,44 @@ int main(int argc, char **argv) args::ValueFlag<std::string> cmd_session(parser, "session", "Open the specified session.", { 's', "session" }); args::PositionalList<std::string> cmd_args(parser, "URL(s)", "List of URLs to open"); + cmd_args.KickOut(true); try { - /*auto next = */ parser.ParseArgs(args); + auto next = parser.ParseArgs(args); if(cmd_version) return builtins::version(); if(cmd_build) return builtins::build(); + // create and load configuration + const std::string config_path = [&]() { + std::string path; + if(cmd_config) + path = args::get(cmd_config); + else + path = std::string(CONFIG_POI_CFG_PATH); + + if(path.front() == '~') + path.replace(0, 1, QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()); + + return path; + }(); + spdlog::debug("Opening config file {}", config_path); + init_conf(config_path); + + if(cmd_args) { + const auto front = args::get(cmd_args).front(); + const auto cmd = commands.find(front); + if(cmd != commands.end()) { + return cmd->second(argv[0], next, std::end(args)); + } + } + } catch(args::Help &e) { std::cout << parser; Q_UNUSED(e); - //std::cout << "Available subcommands: " << command_keys << std::endl; + std::cout << "Available subcommands: " << join_keys(commands) << std::endl; return 0; } catch(args::Error &e) { @@ -102,22 +133,6 @@ int main(int argc, char **argv) qputenv("QTWEBENGINE_CHROMIUM_FLAGS", chromiumFlags + " --disable-in-process-stack-traces"); } - // create and load configuration - const std::string config_path = [&]() { - std::string path; - if(cmd_config) - path = args::get(cmd_config); - else - path = std::string(CONFIG_POI_CFG_PATH); - - if(path.front() == '~') - path.replace(0, 1, QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString()); - - return path; - }(); - spdlog::debug("Opening config file {}", config_path); - init_conf(config_path); - QVector<QPluginLoader *> plugins; CommandHash_t pluginCommands; |