From: Maxim Mamontov Date: Fri, 10 Oct 2014 21:59:27 +0000 (+0300) Subject: Show config only on demand. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/9c6406d76392a55a85d3ec25586a47ab892b88fe Show config only on demand. --- diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index 07bcfb8b..08dc177e 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -78,6 +78,10 @@ class PARAM_ACTION : public ACTION m_description(paramDescription), m_hasDefault(true) {} + PARAM_ACTION(RESETABLE & param) + : m_param(param), + m_hasDefault(false) + {} PARAM_ACTION(RESETABLE & param, const std::string & paramDescription) : m_param(param), @@ -132,6 +136,14 @@ m_param = value; return PARSER_STATE(false, --argc, ++argv); } +template <> +inline +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv, void * /*data*/) +{ +m_param = true; +return PARSER_STATE(false, argc, argv); +} + template inline void PARAM_ACTION::ParseValue(const std::string & stringValue) @@ -172,6 +184,13 @@ PARAM_ACTION * MakeParamAction(RESETABLE & param, return new PARAM_ACTION(param, defaultValue, paramDescription); } +template +inline +PARAM_ACTION * MakeParamAction(RESETABLE & param) +{ +return new PARAM_ACTION(param); +} + template inline PARAM_ACTION * MakeParamAction(RESETABLE & param, diff --git a/projects/sgconf/config.h b/projects/sgconf/config.h index e52111e3..d3659a1d 100644 --- a/projects/sgconf/config.h +++ b/projects/sgconf/config.h @@ -39,6 +39,7 @@ struct CONFIG RESETABLE localPort; RESETABLE userName; RESETABLE userPass; + RESETABLE showConfig; CONFIG & operator=(const CONFIG & rhs) { @@ -56,27 +57,29 @@ struct CONFIG userName = rhs.userName; if (!rhs.userPass.empty()) userPass = rhs.userPass; + if (!rhs.showConfig.empty()) + showConfig = rhs.showConfig; return *this; } std::string Serialize() const { - std::string res("{ "); + std::string res; if (!configFile.empty()) - res += "configFile: '" + configFile.data() + "',"; + res += "configFile: '" + configFile.data() + "'\n"; if (!server.empty()) - res += " server: '" + server.data() + "',"; + res += "server: '" + server.data() + "'\n"; if (!port.empty()) - res += " port: " + x2str(port.data()) + ","; + res += "port: " + x2str(port.data()) + "\n"; if (!localAddress.empty()) - res += " local address: '" + localAddress.data() + "',"; + res += "local address: '" + localAddress.data() + "'\n"; if (!localPort.empty()) - res += " local port: " + x2str(localPort.data()) + ","; + res += "local port: " + x2str(localPort.data()) + "\n"; if (!userName.empty()) - res += " userName: '" + userName.data() + "',"; + res += "userName: '" + userName.data() + "'\n"; if (!userPass.empty()) - res += " userPass: '" + userPass.data() + "'"; - return res + " }"; + res += "userPass: '" + userPass.data() + "\n"; + return res; } }; diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 82372224..0667e259 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -256,6 +256,8 @@ SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options") .Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), ""), "\tadministrative login") .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, ""), "\tpassword for the administrative login") .Add("a", "address", SGCONF::MakeParamAction(config, ""), "connection params as a single string in format: :@:"); +blocks.Add("Debug options") + .Add("show-config", SGCONF::MakeParamAction(config.showConfig), "\t\tshow config and exit"); SGCONF::AppendXMLOptionBlock(commands, blocks); SGCONF::AppendAdminsOptionBlock(commands, blocks); SGCONF::AppendTariffsOptionBlock(commands, blocks); @@ -302,7 +304,11 @@ else config = configOverride; -std::cerr << "Config: " << config.Serialize() << std::endl; +if (!config.showConfig.empty() && config.showConfig.data()) + { + std::cout << config.Serialize() << std::endl; + return 0; + } return commands.Execute(config) ? 0 : -1; } catch (const std::exception& ex)