From e1f240f490324f8c6f4d8132630ad85756f6d191 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 22 Apr 2018 16:10:18 +0200 Subject: Merge CommandLine functionality into Configuration --- lib/configuration/configuration.cpp | 83 ++++++++++++++++++++++++++++--------- lib/configuration/configuration.h | 23 +++++++--- 2 files changed, 80 insertions(+), 26 deletions(-) (limited to 'lib') 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 #include #include +#include +#include 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()->default_value(defaultUserConfigLocation()), "Set the configuration file.") + ("socket,s", po::value()->default_value(defaultSocketPath()), "Local server socket") + + ("url", po::value>(), "URLs") + ; + configuration_desc.add_options() // main window ui ("mainwindow.height", po::value()->default_value(720)) ("mainwindow.width", po::value()->default_value(1280)) @@ -82,46 +115,56 @@ Configuration::Configuration() ("downloads.path", po::value()->default_value("~/Downloads")) ("downloads.shortcut", po::value()->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 Configuration::section(const std::string &prefix) const { QHash 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(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 std::optional value(const char *path) const @@ -66,13 +64,26 @@ public: const std::vector> &options() { - return desc.options(); + return configuration_desc.options(); } QHash section(const std::string &prefix) const; + QStringList positionalArguments() const + { + QStringList l; + if(vm.count("url")) { + std::vector urls = vm["url"].as>(); + 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; -- cgit v1.2.1