]> git.stg.codes - stg.git/commitdiff
Added suboptions to tariffs and admins.
authorMaxim Mamontov <faust.madf@gmail.com>
Wed, 28 May 2014 09:24:00 +0000 (12:24 +0300)
committerMaxim Mamontov <faust.madf@gmail.com>
Wed, 28 May 2014 09:24:00 +0000 (12:24 +0300)
projects/sgconf/actions.h
projects/sgconf/admins.cpp
projects/sgconf/api_action.cpp
projects/sgconf/api_action.h
projects/sgconf/corps.cpp
projects/sgconf/options.cpp
projects/sgconf/services.cpp
projects/sgconf/tariffs.cpp
projects/sgconf/users.cpp
projects/sgconf/xml.cpp

index 33a11347a27fb7a3143380f5dda1a8f3d08435c5..c47f9776090b7adde79252cbb931bb26cde65cc7 100644 (file)
@@ -178,6 +178,50 @@ PARAM_ACTION<T> * MakeParamAction(RESETABLE<T> & param,
 return new PARAM_ACTION<T>(param, paramDescription);
 }
 
+class KV_ACTION : public ACTION
+{
+    public:
+        KV_ACTION(const std::string & name,
+                  std::map<std::string, std::string> & 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<std::string, std::string> & 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<std::string, std::string> & kvs,
+                         const std::string & paramDescription)
+{
+return new KV_ACTION(name, kvs, paramDescription);
+}
+
 } // namespace SGCONF
 
 #endif
index e756618ba9fd19b9ab3261c667bef036cbda373f..b560f581286269d6dc102cb18efa7630ff4ee048 100644 (file)
@@ -43,6 +43,13 @@ std::cout << Indent(level, true) << "login: " << info.login << "\n"
           << Indent(level)       << "priviledges: " << PrivToString(info.priv) << "\n";
 }
 
+std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
+{
+std::vector<SGCONF::API_ACTION::PARAM> params;
+params.push_back({"priv", "<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<API_ACTION::PARAM> params(GetAdminParams());
 blocks.Add("Admin management options")
       .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
-      .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, GetAdminFunction), "get admin")
-      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, AddAdminFunction), "add admin")
-      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, DelAdminFunction), "del admin")
-      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, ChgAdminFunction), "change admin");
+      .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
+      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
+      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
+      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");
 }
index 1ff0e5967915faa0c9a9c678dc3737081986bbee..420f5f0a91029e5d258c5c25677108b8764aeef6 100644 (file)
@@ -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<PARAM> & params,
+                               API_FUNCTION funPtr)
+    : m_commands(commands),
+      m_description(paramDescription),
+      m_argument(needArgument ? "1" : ""), // Hack
+      m_funPtr(funPtr)
+{
+std::vector<PARAM>::const_iterator it(params.begin());
+while (it != params.end())
+    {
+    m_suboptions.Add(it->name, MakeKVAction(it->name, m_params, it->shortDescr), it->longDescr);
+    ++it;
+    }
+}
index 55b384479fa8bbf0b7f005dc8dc73687d82dc2ff..e10840cfabc63931f1bc06f6ba8d20cea914ec86 100644 (file)
@@ -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<PARAM> & 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<API_ACTION::PARAM> & 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
index b48a7c71352bc769a065945224448ec3109a7ffb..aee446b2e44ef54a2f2cd3312c65910790efc64a 100644 (file)
@@ -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, "<name>", true, GetCorpFunction), "get corporation")
-      .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", true, AddCorpFunction), "add corporation")
-      .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", true, DelCorpFunction), "del corporation")
-      .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", true, ChgCorpFunction), "change corporation");
+      .Add("get-corp", SGCONF::MakeAPIAction(commands, "<name>", GetCorpFunction), "get corporation")
+      .Add("add-corp", SGCONF::MakeAPIAction(commands, "<name>", AddCorpFunction), "add corporation")
+      .Add("del-corp", SGCONF::MakeAPIAction(commands, "<name>", DelCorpFunction), "del corporation")
+      .Add("chg-corp", SGCONF::MakeAPIAction(commands, "<name>", ChgCorpFunction), "change corporation");
 }
index 772bee6db6d368b4a90b40425ff96ec9ef228306..f5e423adc46051bcd93e3b68d29d882d5f5436b6 100644 (file)
@@ -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));
index ba490467932403d7fbad7d4efdae4ddb841d4cc0..719435c7fea638dad9926ab80ee12d40786d1cf2 100644 (file)
@@ -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, "<name>", true, GetServiceFunction), "get service")
-      .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", true, AddServiceFunction), "add service")
-      .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", true, DelServiceFunction), "del service")
-      .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", true, ChgServiceFunction), "change service");
+      .Add("get-service", SGCONF::MakeAPIAction(commands, "<name>", GetServiceFunction), "get service")
+      .Add("add-service", SGCONF::MakeAPIAction(commands, "<name>", AddServiceFunction), "add service")
+      .Add("del-service", SGCONF::MakeAPIAction(commands, "<name>", DelServiceFunction), "del service")
+      .Add("chg-service", SGCONF::MakeAPIAction(commands, "<name>", ChgServiceFunction), "change service");
 }
index 4d4feb9a9aaeab66152d741cca9b47879d60bffa..75cd169fae66b89b5d687c3956104df02f902e99 100644 (file)
@@ -91,6 +91,21 @@ for (size_t i = 0; i < info.dirPrice.size(); ++i)
     PrintDirPriceData(i, info.dirPrice[i], level + 1);
 }
 
+std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
+{
+std::vector<SGCONF::API_ACTION::PARAM> params;
+params.push_back({"fee", "<fee>", "\t\ttariff fee"});
+params.push_back({"free", "<free mb>", "\tprepaid traff"});
+params.push_back({"passive-cost", "<cost>", "\tpassive cost"});
+params.push_back({"traff-type", "<type>", "\ttraff type (up, dow, up+down, max)"});
+params.push_back({"period", "<period>", "\ttarification period (daily, monthly)"});
+params.push_back({"times", "<hh:mm-hh:mm, ...>", "coma-separated day time-spans for each direction"});
+params.push_back({"day-prices", "<price/price, ...>", "coma-separated day prices for each direction"});
+params.push_back({"night-prices", "<price/price, ...>", "coma-separated day prices for each direction"});
+params.push_back({"thresholds", "<threshold, ...>", "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<API_ACTION::PARAM> params(GetTariffParams());
 blocks.Add("Tariff management options")
       .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
-      .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, GetTariffFunction), "get tariff")
-      .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, AddTariffFunction), "add tariff")
-      .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, DelTariffFunction), "del tariff")
-      .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", true, ChgTariffFunction), "change tariff");
+      .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", GetTariffFunction), "get tariff")
+      .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, AddTariffFunction), "add tariff")
+      .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", DelTariffFunction), "del tariff")
+      .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, ChgTariffFunction), "change tariff");
 }
index 251599240ae88a06ef41abd613a128f6d9c4b33c..3e3660662f256cfc0d21a9d8e2443b095f15f7c6 100644 (file)
@@ -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, "<login>", true, GetUserFunction), "get user")
-      .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", true, AddUserFunction), "add user")
-      .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", true, DelUserFunction), "del user")
-      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", true, ChgUserFunction), "change user")
-      .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", true, CheckUserFunction), "check user existance and credentials")
-      .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", true, SendMessageFunction), "send message");
+      .Add("get-user", SGCONF::MakeAPIAction(commands, "<login>", GetUserFunction), "get user")
+      .Add("add-user", SGCONF::MakeAPIAction(commands, "<login>", AddUserFunction), "add user")
+      .Add("del-user", SGCONF::MakeAPIAction(commands, "<login>", DelUserFunction), "del user")
+      .Add("chg-user", SGCONF::MakeAPIAction(commands, "<login>", ChgUserFunction), "change user")
+      .Add("check-user", SGCONF::MakeAPIAction(commands, "<login>", CheckUserFunction), "check user existance and credentials")
+      .Add("send-message", SGCONF::MakeAPIAction(commands, "<login>", SendMessageFunction), "send message");
 }
index 5a7d2afdaa513fab3b3e9df906fd3dab6d6bb697..c54fcd02bbd17fb10878da556a956131462a18b2 100644 (file)
@@ -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, "<xml>", true, RawXMLFunction), "\tmake raw XML request");
+      .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", RawXMLFunction), "\tmake raw XML request");
 }