X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/05072df6840a3e6622d235cd48e0ced723e05b26..be3d58dbca4e5aeb16c15983ca59a1db56b9fd7a:/projects/sgconf/options.cpp?ds=sidebyside diff --git a/projects/sgconf/options.cpp b/projects/sgconf/options.cpp index a079e1b1..49e04d64 100644 --- a/projects/sgconf/options.cpp +++ b/projects/sgconf/options.cpp @@ -23,10 +23,47 @@ #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; @@ -80,12 +117,12 @@ void OPTION::Help(size_t level) const if (!m_action) throw ERROR("Option is not defined."); std::string indent(level, '\t'); -std::cout << indent << "\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 + 1); +m_action->Suboptions().Help(level); } bool OPTION::Check(const char * arg) const @@ -97,25 +134,42 @@ if (*arg++ != '-') return false; if (*arg == '-') +{ return m_longName == arg + 1; +} return m_shortName == arg; } -PARSER_STATE OPTION::Parse(int argc, char ** argv) +PARSER_STATE OPTION::Parse(int argc, char ** argv, void * data) { if (!m_action) throw ERROR("Option is not defined."); try { - return m_action->Parse(argc, argv); + return m_action->Parse(argc, argv, data); } catch (const ACTION::ERROR & ex) { if (m_longName.empty()) throw ERROR("-" + m_shortName + ": " + ex.what()); else - throw ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what()); + throw m_shortName.empty() ? ERROR("--" + m_longName + ": " + ex.what()) + : ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what()); + } +} + +void OPTION::ParseValue(const std::string & value) +{ +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()); } } @@ -140,20 +194,23 @@ void OPTION_BLOCK::Help(size_t level) const { if (m_options.empty()) return; -std::cout << m_description << ":\n"; +if (!m_description.empty()) + std::cout << m_description << ":\n"; std::for_each(m_options.begin(), m_options.end(), std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1)); } -PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv) +PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv, void * data) { PARSER_STATE state(false, argc, argv); +if (state.argc == 0) + return state; while (state.argc > 0 && !state.stop) { std::vector