virtual std::string DefaultDescription() const = 0;
virtual OPTION_BLOCK & Suboptions() = 0;
virtual PARSER_STATE Parse(int argc, char ** argv) = 0;
+ virtual void ParseValue(const std::string &) {}
class ERROR : public std::runtime_error
{
virtual std::string DefaultDescription() const;
virtual OPTION_BLOCK & Suboptions() { return m_suboptions; }
virtual PARSER_STATE Parse(int argc, char ** argv);
+ virtual void ParseValue(const std::string & value);
private:
RESETABLE<T> & m_param;
return PARSER_STATE(false, --argc, ++argv);
}
+template <typename T>
+inline
+void PARAM_ACTION<T>::ParseValue(const std::string & stringValue)
+{
+if (stringValue.empty())
+ throw ERROR("Missing value.");
+T value;
+if (str2x(stringValue, value))
+ throw ERROR(std::string("Bad value: '") + stringValue + "'");
+m_param = value;
+}
+
+template <>
+inline
+void PARAM_ACTION<std::string>::ParseValue(const std::string & stringValue)
+{
+m_param = stringValue;
+}
+
template <>
inline
PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv)
RESETABLE<std::string> userName;
RESETABLE<std::string> userPass;
+ CONFIG & operator=(const CONFIG & rhs)
+ {
+ if (!rhs.configFile.empty())
+ configFile = rhs.configFile;
+ if (!rhs.server.empty())
+ server = rhs.server;
+ if (!rhs.port.empty())
+ port = rhs.port;
+ if (!rhs.userName.empty())
+ userName = rhs.userName;
+ if (!rhs.userPass.empty())
+ userPass = rhs.userPass;
+ return *this;
+ }
+
std::string Serialize() const
{
- std::string res("{ ");
- if (!configFile.empty())
- res += "configFile: '" + configFile.data() + "'";
- if (!server.empty())
- res += ", server: '" + server.data() + "'";
- if (!port.empty())
- res += ", port: " + x2str(port.data());
- if (!userName.empty())
- res += ", userName: '" + userName.data() + "'";
- if (!userPass.empty())
- res += ", userPass: '" + userPass.data() + "'";
- return res + " }";
+ std::string res("{ ");
+ if (!configFile.empty())
+ res += "configFile: '" + configFile.data() + "'";
+ if (!server.empty())
+ res += ", server: '" + server.data() + "'";
+ if (!port.empty())
+ res += ", port: " + x2str(port.data());
+ if (!userName.empty())
+ res += ", userName: '" + userName.data() + "'";
+ if (!userPass.empty())
+ res += ", userPass: '" + userPass.data() + "'";
+ return res + " }";
}
};
void Version();
+void ReadUserConfigFile(SGCONF::OPTION_BLOCK & block)
+{
+std::vector<std::string> paths;
+const char * configHome = getenv("XDG_CONFIG_HOME");
+if (configHome == NULL)
+ {
+ const char * home = getenv("HOME");
+ if (home == NULL)
+ return;
+ paths.push_back(std::string(home) + "/.config/sgconf/sgconf.conf");
+ paths.push_back(std::string(home) + "/.sgconf/sgconf.conf");
+ }
+else
+ paths.push_back(std::string(configHome) + "/sgconf/sgconf.conf");
+for (std::vector<std::string>::const_iterator it = paths.begin(); it != paths.end(); ++it)
+ if (access(it->c_str(), R_OK) == 0)
+ {
+ block.ParseFile(*it);
+ return;
+ }
+}
+
} // namespace anonymous
namespace SGCONF
.Add("h", "help", SGCONF::MakeFunc0Action(bind0(Method1Adapt(&SGCONF::OPTION_BLOCKS::Help, blocks), 0)), "\t\tshow this help and exit")
.Add("help-all", SGCONF::MakeFunc0Action(UsageAll), "\t\tshow full help and exit")
.Add("v", "version", SGCONF::MakeFunc0Action(Version), "\t\tshow version information and exit");
-blocks.Add("Connection options")
+SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options")
.Add("s", "server", SGCONF::MakeParamAction(config.server, std::string("localhost"), "<address>"), "\t\thost to connect")
.Add("p", "port", SGCONF::MakeParamAction(config.port, uint16_t(5555), "<port>"), "\t\tport to connect")
.Add("u", "username", SGCONF::MakeParamAction(config.userName, std::string("admin"), "<username>"), "\tadministrative login")
return -1;
}
+try
+{
+SGCONF::CONFIG configOverride(config);
+
+if (config.configFile.empty())
+ {
+ const char * mainConfigFile = "/etc/sgconf/sgconf.conf";
+ if (access(mainConfigFile, R_OK) == 0)
+ block.ParseFile(mainConfigFile);
+ ReadUserConfigFile(block);
+ }
+else
+ {
+ block.ParseFile(config.configFile.data());
+ }
+
+config = configOverride;
+}
+catch (const SGCONF::OPTION::ERROR& ex)
+{
+std::cerr << ex.what() << "\n";
+return -1;
+}
+
std::cerr << "Config: " << config.Serialize() << std::endl;
return 0;
#include "action.h"
#include "parser_state.h"
+#include "stg/common.h"
+
+#include <fstream>
+#include <sstream>
#include <iostream>
#include <functional>
#include <algorithm>
+#include <unistd.h>
+
+namespace
+{
+
+template <class C>
+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;
}
}
+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());
+ }
+}
+
OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
const std::string & longName,
ACTION * action,
return state;
}
+void OPTION_BLOCK::ParseFile(const std::string & filePath)
+{
+if (access(filePath.c_str(), R_OK))
+ throw ERROR("File '" + filePath + "' does not exists.");
+ReadConfigFile(filePath, &OPTION_BLOCK::OptionCallback, this);
+}
+
+void OPTION_BLOCK::OptionCallback(const std::string & key, const std::string & value)
+{
+for (std::vector<OPTION>::iterator it = m_options.begin(); it != m_options.end(); ++it)
+ if (it->Name() == key)
+ it->ParseValue(value);
+}
+
void OPTION_BLOCKS::Help(size_t level) const
{
std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
#include <string>
#include <vector>
#include <list>
+#include <utility>
#include <stdexcept>
#include <cstddef> // size_t
void Help(size_t level = 0) const;
PARSER_STATE Parse(int argc, char ** argv);
+ void ParseValue(const std::string & value);
bool Check(const char * arg) const;
+ const std::string & Name() const { return m_longName; }
class ERROR : public std::runtime_error
{
void Help(size_t level) const;
PARSER_STATE Parse(int argc, char ** argv);
+ void ParseFile(const std::string & filePath);
+
+ class ERROR : public std::runtime_error
+ {
+ public:
+ ERROR(const std::string & message)
+ : std::runtime_error(message.c_str()) {}
+ };
private:
std::vector<OPTION> m_options;
std::string m_description;
+
+ void OptionCallback(const std::string & key, const std::string & value);
};
class OPTION_BLOCKS
return TrimR(TrimL(val));
}
//---------------------------------------------------------------------------
+std::string Trim(const std::string & val)
+{
+std::string res(val);
+return TrimR(TrimL(res));
+}
+//---------------------------------------------------------------------------
std::string ToLower(const std::string & value)
{
std::string res;
std::string & TrimL(std::string & val);
std::string & TrimR(std::string & val);
std::string & Trim(std::string & val);
+std::string Trim(const std::string & val);
std::string ToLower(const std::string & value);
std::string ToUpper(const std::string & value);