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.h | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/configuration/commandline.h (limited to 'lib/configuration/commandline.h') 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 -- cgit v1.2.1