X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/0c109e9e61ae3624f30621b5fd328f354bfa6524..08dd72f2d8d3d7766e4fa87f01840c3ed8211091:/projects/sgconf/options.cpp diff --git a/projects/sgconf/options.cpp b/projects/sgconf/options.cpp index 71f9687c..06378d1c 100644 --- a/projects/sgconf/options.cpp +++ b/projects/sgconf/options.cpp @@ -23,121 +23,206 @@ #include "action.h" #include "parser_state.h" +#include "stg/common.h" + +#include +#include #include #include #include +#include + +namespace +{ + +template +void ReadConfigFile(const std::string & filePath, void (C::* callback)(const std::string&, const std::string&), C * obj) +{ + std::ifstream stream(filePath.c_str()); + std::string line; + size_t num = 0; + while (std::getline(stream, line)) + { + ++num; + line = Trim(line); + std::string::size_type pos = line.find_first_of('#'); + if (pos != std::string::npos) + line = line.substr(0, pos); + if (line.empty()) + continue; + pos = line.find_first_of('='); + if (pos == std::string::npos) + { + std::ostringstream error; + error << "Bad file format, missing '=' in '" << filePath << ":" << num << "'."; + throw std::runtime_error(error.str().c_str()); + } + (obj->*callback)(Trim(line.substr(0, pos)), Trim(line.substr(pos + 1, line.length() - pos - 1))); + } +} + +} // namespace anonymous + using SGCONF::OPTION; using SGCONF::OPTION_BLOCK; +using SGCONF::OPTION_BLOCKS; using SGCONF::ACTION; using SGCONF::PARSER_STATE; OPTION::OPTION(const std::string & shortName, const std::string & longName, - ACTION * action, + std::unique_ptr action, const std::string & description) : m_shortName(shortName), m_longName(longName), - m_action(action), + m_action(std::move(action)), m_description(description) { } OPTION::OPTION(const std::string & longName, - ACTION * action, + std::unique_ptr action, const std::string & description) : m_longName(longName), - m_action(action), + m_action(std::move(action)), m_description(description) { } -OPTION::OPTION(const OPTION & rhs) - : m_shortName(rhs.m_shortName), - m_longName(rhs.m_longName), - m_action(rhs.m_action), - m_description(rhs.m_description) +void OPTION::Help(size_t level) const { + if (!m_action) + throw ERROR("Option is not defined."); + std::string indent(level, '\t'); + std::cout << indent; + if (!m_shortName.empty()) + std::cout << "-" << m_shortName << ", "; + std::cout << "--" << m_longName << " " << m_action->ParamDescription() + << "\t" << m_description << m_action->DefaultDescription() << "\n"; + m_action->Suboptions().Help(level); } -OPTION::~OPTION() +bool OPTION::Check(const char * arg) const { -delete m_action; + if (arg == NULL) + return false; + + if (*arg++ != '-') + return false; + + if (*arg == '-') + return m_longName == arg + 1; + + return m_shortName == arg; } -void OPTION::Help(size_t level) const +PARSER_STATE OPTION::Parse(int argc, char ** argv, void * data) { -if (!m_action) - throw ERROR("Option is not defined."); -std::string indent(level, '\t'); -std::cout << indent << "\t"; -if (!m_shortName.empty()) - std::cout << "-" << m_shortName << ", "; -std::cout << "--" << m_longName << " " << m_action->ParamDescription() - << "\t" << m_description << m_action->DefaultDescription() << "\n"; -m_action->Suboptions().Help(level + 1); + if (!m_action) + throw ERROR("Option is not defined."); + try + { + return m_action->Parse(argc, argv, data); + } + catch (const ACTION::ERROR & ex) + { + if (m_longName.empty()) + throw ERROR("-" + m_shortName + ": " + ex.what()); + else + throw m_shortName.empty() ? ERROR("--" + m_longName + ": " + ex.what()) + : ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what()); + } } -bool OPTION::Check(const char * arg) const +void OPTION::ParseValue(const std::string & value) { -if (arg == NULL) - return false; + if (!m_action) + throw ERROR("Option is not defined."); + try + { + return m_action->ParseValue(value); + } + catch (const ACTION::ERROR & ex) + { + throw ERROR(m_longName + ": " + ex.what()); + } +} -if (*arg++ != '-') - return false; +OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName, + const std::string & longName, + std::unique_ptr action, + const std::string & description) +{ + m_options.emplace_back(shortName, longName, std::move(action), description); + return *this; +} -if (*arg == '-') - return m_longName == arg + 1; +OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName, + std::unique_ptr action, + const std::string & description) +{ + m_options.emplace_back(longName, std::move(action), description); + return *this; +} -return m_shortName == arg; +void OPTION_BLOCK::Help(size_t level) const +{ + if (m_options.empty()) + return; + if (!m_description.empty()) + std::cout << m_description << ":\n"; + for (const auto& option : m_options) + option.Help(level + 1); } -PARSER_STATE OPTION::Parse(int argc, char ** argv) +PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv, void * data) { -if (!m_action) - throw ERROR("Option is not defined."); -try + PARSER_STATE state(false, argc, argv); + if (state.argc == 0) + return state; + while (state.argc > 0 && !state.stop) { - return m_action->Parse(argc, argv); - } -catch (const ACTION::ERROR & ex) - { - throw ERROR(m_longName + ": " + ex.what()); + const auto it = std::find_if(m_options.begin(), m_options.end(), [&state](const auto& opt){ return opt.Check(*state.argv); }); + if (it != m_options.end()) + state = it->Parse(--state.argc, ++state.argv, data); + else + break; } + return state; } -void OPTION_BLOCK::Add(const std::string & shortName, - const std::string & longName, - ACTION * action, - const std::string & description) +void OPTION_BLOCK::ParseFile(const std::string & filePath) { -m_options.push_back(OPTION(shortName, longName, action, description)); + if (access(filePath.c_str(), R_OK)) + throw ERROR("File '" + filePath + "' does not exists."); + ReadConfigFile(filePath, &OPTION_BLOCK::OptionCallback, this); } -void OPTION_BLOCK::Add(const std::string & longName, - ACTION * action, - const std::string & description) +void OPTION_BLOCK::OptionCallback(const std::string & key, const std::string & value) { -m_options.push_back(OPTION(longName, action, description)); + for (auto& option : m_options) + if (option.Name() == key) + option.ParseValue(value); } -void OPTION_BLOCK::Help(size_t level) const +void OPTION_BLOCKS::Help(size_t level) const { -std::for_each(m_options.begin(), - m_options.end(), - std::bind2nd(std::mem_fun_ref(&OPTION::Help), level)); + for (const auto& block : m_blocks) + { + block.Help(level); + std::cout << "\n"; + } } -PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv) +PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv) { -PARSER_STATE state(false, argc, argv); -while (state.argc > 0 && !state.stop) + PARSER_STATE state(false, argc, argv); + auto it = m_blocks.begin(); + while (state.argc > 0 && !state.stop && it != m_blocks.end()) { - std::vector