From 1bc1ae8cd09851faa05345f5a6b105b02fe75dd2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 19 Nov 2020 18:22:36 +0200 Subject: Drop args.hxx dependency Replace args.hxx with QCommandLineParser. --- src/cmd/cmd.hpp | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/cmd/cmd.hpp (limited to 'src/cmd/cmd.hpp') diff --git a/src/cmd/cmd.hpp b/src/cmd/cmd.hpp new file mode 100644 index 0000000..37bb3ce --- /dev/null +++ b/src/cmd/cmd.hpp @@ -0,0 +1,89 @@ +/* + * 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/cgit/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_CMD_HPP +#define SMOLBOTE_CMD_HPP + +#include "version.h" +#include +#include +#include +#include + +namespace command_line +{ +template +using subcommand_fn = std::function; +template +using map = std::unordered_map>; + +// a helper function to join the keys of a command_map into a string +template +[[nodiscard]] inline QString join_keys(const map &map, const QString sep = ", ") +{ + QString k; + for(auto it = map.cbegin(); it != map.cend(); ++it) { + k += QString::fromStdString(it->first); + if(std::next(it) != map.cend()) { + k += sep; + } + } + return k; +} + +template +[[nodiscard]] std::function process(T &app, const map &map, const std::string &d) +{ + const QCommandLineOption build({ "b", "build" }, "Display build information."); + const QCommandLineOption config({ "c", "config" }, "Set the configuration file.", "file"); + const QCommandLineOption noRemote("no-remote", "Do not accept or send remote commands."); + + QCommandLineParser parser; + parser.setApplicationDescription("smolbote: yet another no-frills browser"); + parser.setOptionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsPositionalArguments); + parser.addHelpOption(); + parser.addVersionOption(); + + parser.addOption(build); + parser.addOption(config); + parser.addOption(noRemote); + + parser.addPositionalArgument("subcommand", QString("%1; default: %2").arg(join_keys(map), QString::fromStdString(d))); + parser.addPositionalArgument("urls", "List of URLs to open."); + + parser.process(app); + + if(parser.isSet(build)) { + std::cout << app.applicationName().toStdString() << " " << poi_Version << std::endl; + exit(0); + } + + if constexpr(requires { app.loadConfiguration(parser.value(config)); }) { + app.loadConfiguration(parser.value(config)); + } else { + qDebug("warning: cannot init configuration"); + } + + if constexpr(requires { app.enableRemote(true); }) { + app.enableRemote(!parser.isSet(noRemote)); + } + + const auto args = parser.positionalArguments(); + + if(args.count() >= 1) { + auto i = map.find(args.first().toStdString()); + if(i != map.end()) + return std::bind(i->second, args, std::placeholders::_1); + } + + return std::bind(map.at(d), args, std::placeholders::_1); +} + +} // namespace + +#endif -- cgit v1.2.1