2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
25 #include "parser_state.h"
27 #include "stg/common.h"
28 #include "stg/optional.h"
37 typedef void (* FUNC0)();
40 class FUNC0_ACTION : public ACTION
43 explicit FUNC0_ACTION(const F & func) : m_func(func) {}
45 std::string ParamDescription() const override { return ""; }
46 std::string DefaultDescription() const override { return ""; }
47 OPTION_BLOCK & Suboptions() override { return m_suboptions; }
48 PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) override
51 return PARSER_STATE(true, argc, argv);
56 OPTION_BLOCK m_suboptions;
61 std::unique_ptr<ACTION> MakeFunc0Action(F func)
63 return std::make_unique<FUNC0_ACTION<F>>(func);
67 class PARAM_ACTION : public ACTION
70 PARAM_ACTION(STG::Optional<T> & param,
71 const T & defaultValue,
72 const std::string & paramDescription)
74 m_defaltValue(defaultValue),
75 m_description(paramDescription),
78 explicit PARAM_ACTION(STG::Optional<T> & param)
82 PARAM_ACTION(STG::Optional<T> & param,
83 const std::string & paramDescription)
85 m_description(paramDescription),
89 std::string ParamDescription() const override { return m_description; }
90 std::string DefaultDescription() const override;
91 OPTION_BLOCK & Suboptions() override { return m_suboptions; }
92 PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) override;
93 void ParseValue(const std::string & value) override;
96 STG::Optional<T> & m_param;
98 std::string m_description;
100 OPTION_BLOCK m_suboptions;
103 template <typename T>
105 std::string PARAM_ACTION<T>::DefaultDescription() const
107 return m_hasDefault ? " (default: '" + std::to_string(m_defaltValue) + "')"
113 std::string PARAM_ACTION<std::string>::DefaultDescription() const
115 return m_hasDefault ? " (default: '" + m_defaltValue + "')"
119 template <typename T>
121 PARSER_STATE PARAM_ACTION<T>::Parse(int argc, char ** argv, void * /*data*/)
126 throw ERROR("Missing argument.");
128 if (str2x(*argv, value))
129 throw ERROR(std::string("Bad argument: '") + *argv + "'");
131 return PARSER_STATE(false, --argc, ++argv);
136 PARSER_STATE PARAM_ACTION<bool>::Parse(int argc, char ** argv, void * /*data*/)
139 return PARSER_STATE(false, argc, argv);
142 template <typename T>
144 void PARAM_ACTION<T>::ParseValue(const std::string & stringValue)
146 if (stringValue.empty())
147 throw ERROR("Missing value.");
149 if (str2x(stringValue, value))
150 throw ERROR(std::string("Bad value: '") + stringValue + "'");
156 void PARAM_ACTION<std::string>::ParseValue(const std::string & stringValue)
158 m_param = stringValue;
163 PARSER_STATE PARAM_ACTION<std::string>::Parse(int argc, char ** argv, void * /*data*/)
168 throw ERROR("Missing argument.");
170 return PARSER_STATE(false, --argc, ++argv);
173 template <typename T>
175 std::unique_ptr<ACTION> MakeParamAction(STG::Optional<T> & param,
176 const T & defaultValue,
177 const std::string & paramDescription)
179 return std::make_unique<PARAM_ACTION<T>>(param, defaultValue, paramDescription);
182 template <typename T>
184 std::unique_ptr<ACTION> MakeParamAction(STG::Optional<T> & param)
186 return std::make_unique<PARAM_ACTION<T>>(param);
189 template <typename T>
191 std::unique_ptr<ACTION> MakeParamAction(STG::Optional<T> & param,
192 const std::string & paramDescription)
194 return std::make_unique<PARAM_ACTION<T>>(param, paramDescription);
197 class KV_ACTION : public ACTION
200 KV_ACTION(const std::string & name,
201 const std::string & paramDescription)
203 m_description(paramDescription)
206 std::string ParamDescription() const override { return m_description; }
207 std::string DefaultDescription() const override { return ""; }
208 OPTION_BLOCK & Suboptions() override { return m_suboptions; }
209 PARSER_STATE Parse(int argc, char ** argv, void * data) override;
213 std::string m_description;
214 OPTION_BLOCK m_suboptions;
218 PARSER_STATE KV_ACTION::Parse(int argc, char ** argv, void * data)
223 throw ERROR("Missing argument.");
224 assert(data != NULL && "Expecting container pointer.");
225 std::map<std::string, std::string> & kvs = *static_cast<std::map<std::string, std::string>*>(data);
227 return PARSER_STATE(false, --argc, ++argv);
231 std::unique_ptr<ACTION> MakeKVAction(const std::string & name,
232 const std::string & paramDescription)
234 return std::make_unique<KV_ACTION>(name, paramDescription);
237 } // namespace SGCONF