/* * 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/smolbote.hg * * SPDX-License-Identifier: GPL-3.0 */ #ifndef SMOLBOTE_CONFIGURATION_H #define SMOLBOTE_CONFIGURATION_H #include #include #include #include #include #include #include class Configuration { public: explicit Configuration(); ~Configuration(); 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; } template std::optional value(const char *path) const { // if setting doesn't exist, we crash // in debug builds, check if setting exists if(vm.count(path) == 0) { #ifdef QT_DEBUG qWarning("value(%s) does not exist, probably crashing now", path); #endif return std::nullopt; } if constexpr(std::is_same_v) { std::string r; try { r = vm[path].as(); } catch(boost::bad_any_cast &) { // try int try { r = std::to_string(vm[path].as()); } catch(boost::bad_any_cast &) { // try bool, and crash if not that either r = vm[path].as() ? "true" : "false"; } } // 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()); } 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; } 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 { return configuration_desc; } 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; std::string m_homePath; }; #endif // SMOLBOTE_CONFIGURATION_H