1 #ifndef __STG_SGCONF_API_ACTION_H__
2 #define __STG_SGCONF_API_ACTION_H__
15 typedef bool (* API_FUNCTION) (const CONFIG &,
17 const std::map<std::string, std::string> &);
22 COMMAND(API_FUNCTION funPtr,
23 const std::string & arg,
24 const std::map<std::string, std::string> & options)
29 bool Execute(const SGCONF::CONFIG & config) const
31 return m_funPtr(config, m_arg, m_options);
35 API_FUNCTION m_funPtr;
37 std::map<std::string, std::string> m_options;
43 void Add(API_FUNCTION funPtr,
44 const std::string & arg,
45 const std::map<std::string, std::string> & options) { m_commands.push_back(COMMAND(funPtr, arg, options)); }
46 bool Execute(const SGCONF::CONFIG & config) const
48 std::vector<COMMAND>::const_iterator it(m_commands.begin());
50 while (it != m_commands.end() && res)
52 res = res && it->Execute(config);
58 std::vector<COMMAND> m_commands;
61 class API_ACTION : public ACTION
66 PARAM(const std::string & n,
67 const std::string & s,
68 const std::string & l)
74 std::string shortDescr;
75 std::string longDescr;
78 API_ACTION(COMMANDS & commands,
79 const std::string & paramDescription,
81 const std::vector<PARAM> & params,
83 API_ACTION(COMMANDS & commands,
84 const std::string & paramDescription,
87 : m_commands(commands),
88 m_description(paramDescription),
89 m_argument(needArgument ? "1" : ""), // Hack
93 std::string ParamDescription() const override { return m_description; }
94 std::string DefaultDescription() const override { return ""; }
95 OPTION_BLOCK & Suboptions() override { return m_suboptions; }
96 PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) override;
99 COMMANDS & m_commands;
100 std::string m_description;
101 std::string m_argument;
102 OPTION_BLOCK m_suboptions;
103 std::map<std::string, std::string> m_params;
104 API_FUNCTION m_funPtr;
108 std::unique_ptr<ACTION> MakeAPIAction(COMMANDS & commands,
109 const std::string & paramDescription,
110 const std::vector<API_ACTION::PARAM> & params,
113 return std::make_unique<API_ACTION>(commands, paramDescription, true, params, funPtr);
117 std::unique_ptr<ACTION> MakeAPIAction(COMMANDS & commands,
118 const std::vector<API_ACTION::PARAM> & params,
121 return std::make_unique<API_ACTION>(commands, "", false, params, funPtr);
125 std::unique_ptr<ACTION> MakeAPIAction(COMMANDS & commands,
126 const std::string & paramDescription,
129 return std::make_unique<API_ACTION>(commands, paramDescription, true, funPtr);
133 std::unique_ptr<ACTION> MakeAPIAction(COMMANDS & commands,
136 return std::make_unique<API_ACTION>(commands, "", false, funPtr);