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
<< 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*/)
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");
}
#include "api_action.h"
+#include "actions.h"
#include "parser_state.h"
SGCONF::PARSER_STATE SGCONF::API_ACTION::Parse(int argc, char ** 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;
+ }
+}
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,
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
{
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");
}
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
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());
}
}
{
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));
{
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");
}
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*/)
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");
}
{
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");
}
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");
}