From: Maxim Mamontov Date: Mon, 28 Oct 2013 20:24:44 +0000 (+0200) Subject: Added command line parser prototype. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/ab73d1ffd4233fbfc97c53ba5160f6e0367196a4?ds=sidebyside;hp=-c Added command line parser prototype. --- ab73d1ffd4233fbfc97c53ba5160f6e0367196a4 diff --git a/projects/sgconf/composer.h b/projects/sgconf/composer.h new file mode 100644 index 00000000..a68bce94 --- /dev/null +++ b/projects/sgconf/composer.h @@ -0,0 +1,79 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_COMPOSER_H__ +#define __STG_SGCONF_COMPOSER_H__ + +#include "parser_state.h" + +namespace SGCONF +{ + +class COMPOSER +{ + public: + typedef PARSER_STATE (* FUNC)(int, char **, CONFIG&); + COMPOSER(int argc, char ** argv) + : m_done(false), m_result(0) + { mstate.argc = argc; m_state.argv = argv; } + COMPOSER compose(FUNC func) + { + if (m_done) + return COMPOSER(m_result); + try + { + PARSER_STATE state(func(m_state.argc, m_state.argv, m_state.config)); + if (state.result) + return COMPOSER(0); + else + return COMPOSER(state); + } + catch (const PARSER_ERROR& ex) + { + std::cerr << ex.what() << "\n"; + return COMPOSER(-1); + } + } + int exec() + { + if (m_done) + return m_result; + Usage(); + return -1; + } + + private: + bool m_done; + int m_result; + PARSER_STATE m_state; + + COMPOSER(int result) + : m_done(true), + m_result(result) + { + } + + COMPOSER(const PARSER_STATE& state) + : m_done(false), + m_result(0), + m_state(state) +}; + +} diff --git a/projects/sgconf/config.h b/projects/sgconf/config.h new file mode 100644 index 00000000..68aade1d --- /dev/null +++ b/projects/sgconf/config.h @@ -0,0 +1,41 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_CONFIG_H__ +#define __STG_SGCONF_CONFIG_H__ + +#include "stg/resetable.h" +#include "stg/os_int.h" + +#include + +namespace SGCONF +{ + +struct CONFIG +{ + RESETABLE configFile; + RESETABLE server; + RESETABLE port; + RESETABLE userName; + RESETABLE userPass; +}; + +} diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index c4ca29cb..c0debdc1 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -1052,10 +1052,69 @@ if (isMessage) return ProcessSetUser(req.server.data(), req.port.data(), req.admLogin.data(), req.admPasswd.data(), req.login.data(), conf, stat); } //----------------------------------------------------------------------------- +PARSER_STATE TryParse(const PARSERS& parsers, char ** argv, int argc) +{ +PARSERS::const_iterator it = parsers.find(*argv); +if (it != parsers.end()) + return it->second(++argv, --argc); +PARSER_STATE state; +state.argc = argc; +state.argv = argv; +state.result = false; +return state; +} +//----------------------------------------------------------------------------- +PARSER_STATE ParseCommon(int argc, char ** argv, CONFIG& config) +{ +if (pos == 0) + ++pos; + +PARSERS parsers; +parsers.add("-c", "--config", config.configFile); +parsers.add("-h", "--help", Usage, false); +parsers.add("--help-all", Usage, true); +parsers.add("-v", "--version", Version); + +while (true) + { + PARSER_STATE state(TryParse(parsers, argv, argc, config)); + if (state.argv == argv) + return state; // No-op + if (state.argc == 0) + return state; // EOF + if (state.result) + return state; // Done + argv = state.argv; + argc = state.argc; + } + +assert(0 && "Can't be here."); +return PARSER_STATE(); +} +//----------------------------------------------------------------------------- int main(int argc, char **argv) { Usage(true); exit(0); + +// Ok - succesfully parsed +// Done - don't continue, return 0 +// Error - don't continue, return -1 +// No-op - nothing changed + +return COMPOSER(argv).compose(ParseCommon) + .compose(ReadConfig) + .compose(ParseCommand) + .exec(); + + +if (argc < 2) + { + // TODO: no arguments + Usage(false); + return 1; + } + if (argc <= 2) { UsageConf(); @@ -1094,7 +1153,7 @@ std::cout << "sgconf is the Stargazer management utility.\n\n" << "General options:\n" << "\t-c, --config \t\toverride default config file (default: \"~/.config/stg/sgconf.conf\")\n" << "\t-h, --help\t\t\t\tshow this help and exit\n" - << "\t-h, --help-all\t\t\t\tshow full help and exit\n" + << "\t--help-all\t\t\t\tshow full help and exit\n" << "\t-v, --version\t\t\t\tshow version information and exit\n\n"; UsageConnection(); UsageAdmins(full); diff --git a/projects/sgconf/parser_state.h b/projects/sgconf/parser_state.h new file mode 100644 index 00000000..1482133e --- /dev/null +++ b/projects/sgconf/parser_state.h @@ -0,0 +1,37 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_PARSER_STATE_H__ +#define __STG_SGCONF_PARSER_STATE_H__ + +#include "config.h" + +namespace SGCONF +{ + +struct PARSER_STATE +{ + CONFIG config; + bool result; + int argc; + char ** argv; +}; + +} diff --git a/projects/sgconf/parsers.h b/projects/sgconf/parsers.h new file mode 100644 index 00000000..3ecb18af --- /dev/null +++ b/projects/sgconf/parsers.h @@ -0,0 +1,124 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_PARSERS_H__ +#define __STG_SGCONF_PARSERS_H__ + +namespace SGCONF +{ + +typedef void (*FUNC0)(); + +template +struct FUNC1 +{ +typedef void (*type)(T); +}; + +class PARSER +{ + public: + virtual PARSER_STATE parse(int, char **, CONFIG&) = 0; +}; + +template +class PARAM_PARSER : public PARSER +{ + public: + PARAM_PARSER(T& var) : m_var(var) {} + virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config) + { + std::istringstream stream(argv[0]); + stream >> m_var; + PARSER_STATE state; + state.argc = argc - 1; + state.argv = argv + 1; + state.config = config; + state.result = false; + return state; + } + + private: + T& m_var; +}; + +class FUNC0_PARSER +{ + public: + FUNC0_PARSER(FUNC0 func) : m_func(func) {} + virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config) + { + m_func(); + PARSER_STATE state; + state.argc = argc - 1; + state.argv = argv + 1; + state.config = config; + state.result = true; + return state; + } + + private: + FUNC0 m_func; +}; + +template +class FUNC1_PARSER +{ + public: + FUNC1_PARSER(typename FUNC1::type func, const T & arg) : m_func(func), m_arg(arg) {} + virtual PARSER_STATE parse(int argc, char ** argv, CONFIG& config) + { + m_func(m_arg); + PARSER_STATE state; + state.argc = argc - 1; + state.argv = argv + 1; + state.config = config; + state.result = true; + return state; + } + + private: + typename FUNC1::type m_func; + T m_arg; +} + +class PARSERS +{ + public: + typedef PARSER_STATE (* FUNC)(int, char **, CONFIG&); + + template + void add(const std::string & shortToken, + const std::string & fullToken, + T& var); + + template <> + void add(const std:string & shortToken, + const std::string & fullToken, + FUNC0 func); + template + void add(const std:string & shortToken, + const std::string & fullToken, + FUNC1 func, const V& v); + + private: +}; + +}