From: Maxim Mamontov Date: Wed, 28 May 2014 09:24:00 +0000 (+0300) Subject: Added suboptions to tariffs and admins. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/d3b6a58593b94c9ff41c30a7517086d607f28f10 Added suboptions to tariffs and admins. --- diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 33a11347..c47f9776 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -178,6 +178,50 @@ PARAM_ACTION * MakeParamAction(RESETABLE & param, return new PARAM_ACTION(param, paramDescription); } +class KV_ACTION : public ACTION +{ + public: + KV_ACTION(const std::string & name, + std::map & kvs, + const std::string & paramDescription) + : m_name(name), + m_kvs(kvs), + m_description(paramDescription) + {} + + virtual ACTION * Clone() const { return new KV_ACTION(*this); } + + virtual std::string ParamDescription() const { return m_description; } + virtual std::string DefaultDescription() const { return ""; } + virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } + virtual PARSER_STATE Parse(int argc, char ** argv); + + private: + std::string m_name; + std::map & m_kvs; + std::string m_description; + OPTION_BLOCK m_suboptions; +}; + +inline +PARSER_STATE KV_ACTION::Parse(int argc, char ** argv) +{ +if (argc == 0 || + argv == NULL || + *argv == NULL) + throw ERROR("Missing argument."); +m_kvs[m_name] = *argv; +return PARSER_STATE(false, --argc, ++argv); +} + +inline +KV_ACTION * MakeKVAction(const std::string & name, + std::map & kvs, + const std::string & paramDescription) +{ +return new KV_ACTION(name, kvs, paramDescription); +} + } // namespace SGCONF #endif diff --git a/projects/sgconf/admins.cpp b/projects/sgconf/admins.cpp index e756618b..b560f581 100644 --- a/projects/sgconf/admins.cpp +++ b/projects/sgconf/admins.cpp @@ -43,6 +43,13 @@ std::cout << Indent(level, true) << "login: " << info.login << "\n" << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n"; } +std::vector GetAdminParams() +{ +std::vector params; +params.push_back({"priv", "", "priviledges"}); +return params; +} + void SimpleCallback(bool result, const std::string & reason, void * /*data*/) @@ -146,10 +153,11 @@ return false; void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) { +std::vector params(GetAdminParams()); blocks.Add("Admin management options") .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list") - .Add("get-admin", SGCONF::MakeAPIAction(commands, "", true, GetAdminFunction), "get admin") - .Add("add-admin", SGCONF::MakeAPIAction(commands, "", true, AddAdminFunction), "add admin") - .Add("del-admin", SGCONF::MakeAPIAction(commands, "", true, DelAdminFunction), "del admin") - .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", true, ChgAdminFunction), "change admin"); + .Add("get-admin", SGCONF::MakeAPIAction(commands, "", GetAdminFunction), "get admin") + .Add("add-admin", SGCONF::MakeAPIAction(commands, "", params, AddAdminFunction), "add admin") + .Add("del-admin", SGCONF::MakeAPIAction(commands, "", DelAdminFunction), "del admin") + .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", params, ChgAdminFunction), "change admin"); } diff --git a/projects/sgconf/api_action.cpp b/projects/sgconf/api_action.cpp index 1ff0e596..420f5f0a 100644 --- a/projects/sgconf/api_action.cpp +++ b/projects/sgconf/api_action.cpp @@ -1,5 +1,6 @@ #include "api_action.h" +#include "actions.h" #include "parser_state.h" SGCONF::PARSER_STATE SGCONF::API_ACTION::Parse(int argc, char ** argv) @@ -19,3 +20,21 @@ m_suboptions.Parse(state.argc, state.argv); m_commands.Add(m_funPtr, m_argument, m_params); return state; } + +SGCONF::API_ACTION::API_ACTION(COMMANDS & commands, + const std::string & paramDescription, + bool needArgument, + const std::vector & params, + API_FUNCTION funPtr) + : m_commands(commands), + m_description(paramDescription), + m_argument(needArgument ? "1" : ""), // Hack + m_funPtr(funPtr) +{ +std::vector::const_iterator it(params.begin()); +while (it != params.end()) + { + m_suboptions.Add(it->name, MakeKVAction(it->name, m_params, it->shortDescr), it->longDescr); + ++it; + } +} diff --git a/projects/sgconf/api_action.h b/projects/sgconf/api_action.h index 55b38447..e10840cf 100644 --- a/projects/sgconf/api_action.h +++ b/projects/sgconf/api_action.h @@ -61,17 +61,18 @@ class COMMANDS class API_ACTION : public ACTION { public: + struct PARAM + { + std::string name; + std::string shortDescr; + std::string longDescr; + }; + API_ACTION(COMMANDS & commands, const std::string & paramDescription, bool needArgument, - const OPTION_BLOCK& suboptions, - API_FUNCTION funPtr) - : m_commands(commands), - m_description(paramDescription), - m_argument(needArgument ? "1" : ""), // Hack - m_suboptions(suboptions), - m_funPtr(funPtr) - {} + const std::vector & params, + API_FUNCTION funPtr); API_ACTION(COMMANDS & commands, const std::string & paramDescription, bool needArgument, @@ -101,10 +102,18 @@ class API_ACTION : public ACTION inline ACTION * MakeAPIAction(COMMANDS & commands, const std::string & paramDescription, - bool needArgument, + const std::vector & params, + API_FUNCTION funPtr) +{ +return new API_ACTION(commands, paramDescription, true, params, funPtr); +} + +inline +ACTION * MakeAPIAction(COMMANDS & commands, + const std::string & paramDescription, API_FUNCTION funPtr) { -return new API_ACTION(commands, paramDescription, needArgument, funPtr); +return new API_ACTION(commands, paramDescription, true, funPtr); } inline diff --git a/projects/sgconf/corps.cpp b/projects/sgconf/corps.cpp index b48a7c71..aee446b2 100644 --- a/projects/sgconf/corps.cpp +++ b/projects/sgconf/corps.cpp @@ -126,8 +126,8 @@ void SGCONF::AppendCorpsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) { blocks.Add("Corporation management options") .Add("get-corps", SGCONF::MakeAPIAction(commands, GetCorpsFunction), "\tget corporation list") - .Add("get-corp", SGCONF::MakeAPIAction(commands, "", true, GetCorpFunction), "get corporation") - .Add("add-corp", SGCONF::MakeAPIAction(commands, "", true, AddCorpFunction), "add corporation") - .Add("del-corp", SGCONF::MakeAPIAction(commands, "", true, DelCorpFunction), "del corporation") - .Add("chg-corp", SGCONF::MakeAPIAction(commands, "", true, ChgCorpFunction), "change corporation"); + .Add("get-corp", SGCONF::MakeAPIAction(commands, "", GetCorpFunction), "get corporation") + .Add("add-corp", SGCONF::MakeAPIAction(commands, "", AddCorpFunction), "add corporation") + .Add("del-corp", SGCONF::MakeAPIAction(commands, "", DelCorpFunction), "del corporation") + .Add("chg-corp", SGCONF::MakeAPIAction(commands, "", ChgCorpFunction), "change corporation"); } diff --git a/projects/sgconf/options.cpp b/projects/sgconf/options.cpp index 772bee6d..f5e423ad 100644 --- a/projects/sgconf/options.cpp +++ b/projects/sgconf/options.cpp @@ -122,7 +122,7 @@ 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 @@ -152,7 +152,8 @@ 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()); } } @@ -191,7 +192,8 @@ 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)); diff --git a/projects/sgconf/services.cpp b/projects/sgconf/services.cpp index ba490467..719435c7 100644 --- a/projects/sgconf/services.cpp +++ b/projects/sgconf/services.cpp @@ -128,8 +128,8 @@ void SGCONF::AppendServicesOptionBlock(COMMANDS & commands, OPTION_BLOCKS & bloc { blocks.Add("Service management options") .Add("get-services", SGCONF::MakeAPIAction(commands, GetServicesFunction), "\tget service list") - .Add("get-service", SGCONF::MakeAPIAction(commands, "", true, GetServiceFunction), "get service") - .Add("add-service", SGCONF::MakeAPIAction(commands, "", true, AddServiceFunction), "add service") - .Add("del-service", SGCONF::MakeAPIAction(commands, "", true, DelServiceFunction), "del service") - .Add("chg-service", SGCONF::MakeAPIAction(commands, "", true, ChgServiceFunction), "change service"); + .Add("get-service", SGCONF::MakeAPIAction(commands, "", GetServiceFunction), "get service") + .Add("add-service", SGCONF::MakeAPIAction(commands, "", AddServiceFunction), "add service") + .Add("del-service", SGCONF::MakeAPIAction(commands, "", DelServiceFunction), "del service") + .Add("chg-service", SGCONF::MakeAPIAction(commands, "", ChgServiceFunction), "change service"); } diff --git a/projects/sgconf/tariffs.cpp b/projects/sgconf/tariffs.cpp index 4d4feb9a..75cd169f 100644 --- a/projects/sgconf/tariffs.cpp +++ b/projects/sgconf/tariffs.cpp @@ -91,6 +91,21 @@ for (size_t i = 0; i < info.dirPrice.size(); ++i) PrintDirPriceData(i, info.dirPrice[i], level + 1); } +std::vector GetTariffParams() +{ +std::vector params; +params.push_back({"fee", "", "\t\ttariff fee"}); +params.push_back({"free", "", "\tprepaid traff"}); +params.push_back({"passive-cost", "", "\tpassive cost"}); +params.push_back({"traff-type", "", "\ttraff type (up, dow, up+down, max)"}); +params.push_back({"period", "", "\ttarification period (daily, monthly)"}); +params.push_back({"times", "", "coma-separated day time-spans for each direction"}); +params.push_back({"day-prices", "", "coma-separated day prices for each direction"}); +params.push_back({"night-prices", "", "coma-separated day prices for each direction"}); +params.push_back({"thresholds", "", "coma-separated thresholds for each direction"}); +return params; +} + void SimpleCallback(bool result, const std::string & reason, void * /*data*/) @@ -193,10 +208,11 @@ return false; void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) { +std::vector params(GetTariffParams()); blocks.Add("Tariff management options") .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list") - .Add("get-tariff", SGCONF::MakeAPIAction(commands, "", true, GetTariffFunction), "get tariff") - .Add("add-tariff", SGCONF::MakeAPIAction(commands, "", true, AddTariffFunction), "add tariff") - .Add("del-tariff", SGCONF::MakeAPIAction(commands, "", true, DelTariffFunction), "del tariff") - .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", true, ChgTariffFunction), "change tariff"); + .Add("get-tariff", SGCONF::MakeAPIAction(commands, "", GetTariffFunction), "get tariff") + .Add("add-tariff", SGCONF::MakeAPIAction(commands, "", params, AddTariffFunction), "add tariff") + .Add("del-tariff", SGCONF::MakeAPIAction(commands, "", DelTariffFunction), "del tariff") + .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "", params, ChgTariffFunction), "change tariff"); } diff --git a/projects/sgconf/users.cpp b/projects/sgconf/users.cpp index 25159924..3e366066 100644 --- a/projects/sgconf/users.cpp +++ b/projects/sgconf/users.cpp @@ -184,10 +184,10 @@ void SGCONF::AppendUsersOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) { blocks.Add("User management options") .Add("get-users", SGCONF::MakeAPIAction(commands, GetUsersFunction), "\tget user list") - .Add("get-user", SGCONF::MakeAPIAction(commands, "", true, GetUserFunction), "get user") - .Add("add-user", SGCONF::MakeAPIAction(commands, "", true, AddUserFunction), "add user") - .Add("del-user", SGCONF::MakeAPIAction(commands, "", true, DelUserFunction), "del user") - .Add("chg-user", SGCONF::MakeAPIAction(commands, "", true, ChgUserFunction), "change user") - .Add("check-user", SGCONF::MakeAPIAction(commands, "", true, CheckUserFunction), "check user existance and credentials") - .Add("send-message", SGCONF::MakeAPIAction(commands, "", true, SendMessageFunction), "send message"); + .Add("get-user", SGCONF::MakeAPIAction(commands, "", GetUserFunction), "get user") + .Add("add-user", SGCONF::MakeAPIAction(commands, "", AddUserFunction), "add user") + .Add("del-user", SGCONF::MakeAPIAction(commands, "", DelUserFunction), "del user") + .Add("chg-user", SGCONF::MakeAPIAction(commands, "", ChgUserFunction), "change user") + .Add("check-user", SGCONF::MakeAPIAction(commands, "", CheckUserFunction), "check user existance and credentials") + .Add("send-message", SGCONF::MakeAPIAction(commands, "", SendMessageFunction), "send message"); } diff --git a/projects/sgconf/xml.cpp b/projects/sgconf/xml.cpp index 5a7d2afd..c54fcd02 100644 --- a/projects/sgconf/xml.cpp +++ b/projects/sgconf/xml.cpp @@ -99,5 +99,5 @@ return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok; void SGCONF::AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) { blocks.Add("Raw XML") - .Add("r", "raw", SGCONF::MakeAPIAction(commands, "", true, RawXMLFunction), "\tmake raw XML request"); + .Add("r", "raw", SGCONF::MakeAPIAction(commands, "", RawXMLFunction), "\tmake raw XML request"); }