SNode.C
Loading...
Searching...
No Matches
SubCommand.h
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef UTILS_SUBCOMMAND_H
43#define UTILS_SUBCOMMAND_H
44
45#ifndef DOXYGEN_SHOULD_SKIP_THIS
46
47#include <functional>
48#include <map>
49#include <memory>
50#include <string>
51#include <utility>
52
53#ifdef __GNUC__
54#pragma GCC diagnostic push
55#pragma GCC diagnostic ignored "-Wfloat-equal"
56#ifdef __has_warning
57#if __has_warning("-Wweak-vtables")
58#pragma GCC diagnostic ignored "-Wweak-vtables"
59#endif
60#if __has_warning("-Wcovered-switch-default")
61#pragma GCC diagnostic ignored "-Wcovered-switch-default"
62#endif
63#if __has_warning("-Wmissing-noreturn")
64#pragma GCC diagnostic ignored "-Wmissing-noreturn"
65#endif
66#if __has_warning("-Wnrvo")
67#pragma GCC diagnostic ignored "-Wnrvo"
68#endif
69#endif
70#endif
71#include "utils/CLI11.hpp" // IWYU pragma: export
72#ifdef __GNUC__
73#pragma GCC diagnostic pop
74#endif
75
76#endif // DOXYGEN_SHOULD_SKIP_THIS
77
78namespace utils {
79 class SubCommand;
80
81 class AppWithPtr : public CLI::App {
82 public:
83 AppWithPtr(const std::string& description, const std::string& name, SubCommand* t, bool manage);
84
85 const SubCommand* getPtr() const;
86
87 ~AppWithPtr() override;
88
90
91 private:
93 bool manage;
94 };
95
96 class SubCommand {
97 protected:
98 SubCommand(SubCommand* parent, std::shared_ptr<utils::AppWithPtr> appWithPtr, const std::string& group, bool final); // = true);
99
100 template <typename ConcretSubCommand>
101 SubCommand(SubCommand* parent, ConcretSubCommand* concretSubCommand, const std::string& group, bool final = true);
102
103 public:
104 SubCommand(const SubCommand&) = delete;
106
107 SubCommand& operator=(const SubCommand&) = delete;
109
110 virtual ~SubCommand();
111
112 std::string getName() const;
113 std::string version() const;
114
115 void parse(int argc, char* argv[]);
116
117 protected:
118 SubCommand* description(const std::string& description);
119 SubCommand* footer(const std::string& footer);
120
121 public:
122 CLI::Option* setConfig(const std::string& defaultConfigFile) const;
123 CLI::Option* setLogFile(const std::string& defaultLogFile) const;
124 CLI::Option* setVersionFlag(const std::string& version) const;
125
126 bool hasParent() const;
128
129 SubCommand* allowExtras(bool allow = true);
130
131 SubCommand* required(bool required = true, bool force = true);
132 SubCommand* required(SubCommand* subCommand, bool required = true);
133 SubCommand* required(CLI::Option* option, bool required = true);
134
135 SubCommand* needs(SubCommand* subCommand, bool needs = true);
136 SubCommand* disabled(SubCommand* subCommand, bool disabled = true);
137
138 SubCommand* setRequireCallback(const std::function<void(void)>& callback);
139 SubCommand* finalCallback(const std::function<void()>& finalCallback);
140
141 std::string configToStr() const;
142 std::string help(const CLI::App* helpApp, const CLI::AppFormatMode& mode) const;
143
144 template <typename NewSubCommand, typename... Args>
145 NewSubCommand* newSubCommand(Args&&... args);
146
147 template <typename RequestedSubCommand>
148 RequestedSubCommand* getSubCommand();
149
150 template <typename RequestedSubCommand>
151 RequestedSubCommand* getSubCommand() const;
152
153 CLI::Option* getOption(const std::string& name) const;
154
155 CLI::Option* addOption(const std::string& name,
156 const std::string& description,
157 const std::string& typeName,
158 const CLI::Validator& validator) const;
159
160 template <typename ValueTypeT>
161 CLI::Option* addOption(const std::string& name,
162 const std::string& description,
163 const std::string& typeName,
164 ValueTypeT defaultValue,
165 const CLI::Validator& validator) const;
166
167 template <typename ValueTypeT>
168 CLI::Option* addOptionVariable(const std::string& name,
169 ValueTypeT& variable,
170 const std::string& description,
171 const std::string& typeName,
172 const CLI::Validator& additionalValidator) const;
173
174 template <typename ValueTypeT>
175 CLI::Option* addOptionVariable(const std::string& name,
176 ValueTypeT& variable,
177 const std::string& description,
178 const std::string& typeName,
179 ValueTypeT defaultValue,
180 const CLI::Validator& additionalValidator) const;
181
182 CLI::Option* addOptionFunction(const std::string& name,
183 std::function<void(const std::string&)>& callback,
184 const std::string& description,
185 const std::string& typeName,
186 const CLI::Validator& validator) const;
187
188 template <typename ValueTypeT>
189 CLI::Option* addOptionFunction(const std::string& name,
190 std::function<void(const std::string&)>& callback,
191 const std::string& description,
192 const std::string& typeName,
193 ValueTypeT defaultValue,
194 const CLI::Validator& validator) const;
195
196 CLI::Option* addFlag(const std::string& name,
197 const std::string& description,
198 const std::string& typeName,
199 const CLI::Validator& validator) const;
200
201 template <typename ValueTypeT>
202 CLI::Option* addFlag(const std::string& name,
203 const std::string& description,
204 const std::string& typeName,
205 ValueTypeT defaultValue,
206 const CLI::Validator& validator) const;
207
208 CLI::Option* addFlagFunction(const std::string& name,
209 const std::function<void()>& callback,
210 const std::string& description,
211 const std::string& typeName,
212 const CLI::Validator& validator);
213
214 CLI::Option* addFlagFunction(const std::string& name,
215 const std::function<void()>& callback,
216 const std::string& description,
217 const std::string& typeName,
218 const std::string& defaultValue,
219 const CLI::Validator& validator);
220
221 protected:
222 template <typename ValueTypeT>
223 CLI::Option* setDefaultValue(CLI::Option* option, const ValueTypeT& value, bool clear = true) const;
224
225 CLI::Option* setConfigurable(CLI::Option* option, bool configurable) const;
226
227 public:
228 static CLI::App* getHelpTriggerApp();
229 static CLI::App* getShowConfigTriggerApp();
230 static CLI::App* getCommandlineTriggerApp();
231
232 static std::shared_ptr<CLI::Formatter> sectionFormatter;
233
234 protected:
236
237 static CLI::App* helpTriggerApp;
240
241 private:
242 CLI::Option* initialize(CLI::Option* option, const std::string& typeName, const CLI::Validator& validator, bool configurable) const;
243
246
247 bool final;
248
249 CLI::Option* helpOpt = nullptr;
250 CLI::Option* showConfigOpt = nullptr;
251 CLI::Option* commandlineOpt = nullptr;
252
254 };
255
256 template <typename ConcretSubCommand>
257 inline SubCommand::SubCommand(SubCommand* parent, ConcretSubCommand* concretSubCommand, const std::string& group, bool final)
258 : SubCommand(parent,
259 std::make_shared<utils::AppWithPtr>(
260 std::string(ConcretSubCommand::DESCRIPTION), std::string(ConcretSubCommand::NAME), concretSubCommand, true),
261 group,
262 final) {
263 }
264
265 template <typename NewSubCommand, typename... Args>
266 NewSubCommand* SubCommand::newSubCommand(Args&&... args) {
267 return !final ? new NewSubCommand(this, std::forward(args)...) : nullptr;
268 }
269
270 template <typename RequestedSubCommand>
271 RequestedSubCommand* SubCommand::getSubCommand() {
272 auto* appWithPtr = subCommandApp->get_subcommand_no_throw(std::string(RequestedSubCommand::NAME));
273
274 AppWithPtr* subCommandApp = dynamic_cast<utils::AppWithPtr*>(appWithPtr);
275
276 return subCommandApp != nullptr ? dynamic_cast<RequestedSubCommand*>(subCommandApp->getPtr()) : nullptr;
277 }
278
279 template <typename RequestedSubCommand>
280 RequestedSubCommand* SubCommand::getSubCommand() const {
281 auto* appWithPtr = subCommandApp->get_subcommand_no_throw(std::string(RequestedSubCommand::NAME));
282
283 AppWithPtr* subCommandApp = dynamic_cast<utils::AppWithPtr*>(appWithPtr);
284
285 return subCommandApp != nullptr ? dynamic_cast<RequestedSubCommand*>(subCommandApp->getPtr()) : nullptr;
286 }
287
288 template <typename ValueTypeT>
289 CLI::Option* SubCommand::addOption(const std::string& name,
290 const std::string& description,
291 const std::string& typeName,
292 ValueTypeT defaultValue,
293 const CLI::Validator& additionalValidator) const {
294 return setDefaultValue(addOption(name, description, typeName, additionalValidator), defaultValue);
295 }
296
297 template <typename ValueTypeT>
298 CLI::Option* SubCommand::addOptionVariable(const std::string& name,
299 ValueTypeT& variable,
300 const std::string& description,
301 const std::string& typeName,
302 const CLI::Validator& additionalValidator) const {
303 return initialize(
304 subCommandApp->add_option(name, variable, description), typeName, additionalValidator, !subCommandApp->get_disabled());
305 }
306
307 template <typename ValueTypeT>
308 CLI::Option* SubCommand::addOptionVariable(const std::string& name,
309 ValueTypeT& variable,
310 const std::string& description,
311 const std::string& typeName,
312 ValueTypeT defaultValue,
313 const CLI::Validator& additionalValidator) const {
314 return setDefaultValue(addOptionVariable(name, variable, description, typeName, additionalValidator), defaultValue);
315 }
316
317 template <typename ValueTypeT>
318 CLI::Option* SubCommand::addOptionFunction(const std::string& name,
319 std::function<void(const std::string&)>& callback,
320 const std::string& description,
321 const std::string& typeName,
322 ValueTypeT defaultValue,
323 const CLI::Validator& validator) const {
324 return setDefaultValue(addOptionFunction(name, callback, description, typeName, validator), defaultValue);
325 }
326
327 template <typename ValueTypeT>
328 CLI::Option* SubCommand::addFlag(const std::string& name,
329 const std::string& description,
330 const std::string& typeName,
331 ValueTypeT defaultValue,
332 const CLI::Validator& additionalValidator) const {
333 return setDefaultValue(addFlag(name, description, typeName, additionalValidator), defaultValue);
334 }
335
336 template <typename ValueTypeT>
337 CLI::Option* SubCommand::setDefaultValue(CLI::Option* option, const ValueTypeT& value, bool clear) const {
338 try {
339 option->default_val(value);
340
341 if (clear) {
342 option->clear();
343 }
344 } catch (const CLI::ParseError&) {
345 option = nullptr;
346 }
347
348 return option;
349 }
350
351} // namespace utils
352
353#endif // UTILS_SUBCOMMAND_H
ConfigDb(SubCommand *parent)
CLI::Option * hostOpt
static constexpr std::string_view NAME
static constexpr std::string_view DESCRIPTION
ConfigDb & setHost(const std::string &host)
std::string getHost()
void * operator new(std::size_t count)=delete
static void init(int argc, char *argv[])
Definition SNodeC.cpp:54
~SNodeC()=delete
static TickStatus tick(const utils::Timeval &timeOut=0)
Definition SNodeC.cpp:68
static void free()
Definition SNodeC.cpp:72
static void stop()
Definition SNodeC.cpp:64
SNodeC()=delete
static State state()
Definition SNodeC.cpp:76
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:60
Timer(Timer &&timer) noexcept
Definition Timer.cpp:58
virtual ~Timer()
Definition Timer.cpp:78
Timer & operator=(Timer &&timer) noexcept
Definition Timer.cpp:67
Timer(core::TimerEventReceiver *timerEventReceiver)
Definition Timer.cpp:52
void restart()
Definition Timer.cpp:90
TimerEventReceiver * timerEventReceiver
Definition Timer.h:73
void cancel()
Definition Timer.cpp:84
void removeTimerEventReceiver()
Definition Timer.cpp:96
Timer()=default
Timer & operator=(const Timer &)=delete
Timer & operator=(Timer &&timer) noexcept=default
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
~Timer() override
Definition Timer.cpp:54
static Timer intervalTimer(const std::function< void(const std::function< void()> &)> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:61
Timer(Timer &&timer) noexcept=default
static Timer intervalTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:65
MariaDBClientASyncAPI(const MariaDBClientASyncAPI &)=default
virtual MariaDBCommandSequence & execute_async(MariaDBCommand *mariaDBCommand)=0
virtual void execute_sync(MariaDBCommandSync *mariaDBCommand)=0
MariaDBClientSyncAPI(const MariaDBClientSyncAPI &)=default
MariaDBClient(const MariaDBConnectionDetails &details, const std::function< void(const MariaDBState &)> &onStateChanged)
void execute_sync(MariaDBCommandSync *mariaDBCommand) final
MariaDBCommandSequence & execute_async(MariaDBCommand *mariaDBCommand) final
std::function< void(const MariaDBState &)> onStateChanged
MariaDBConnection * mariaDBConnection
MariaDBConnectionDetails details
MariaDBCommandSequence & operator=(MariaDBCommandSequence &&mariaDBCommandSequence)=default
void execute_sync(MariaDBCommandSync *mariaDBCommand) final
std::deque< MariaDBCommand * > & sequence()
MariaDBCommandSequence & execute_async(MariaDBCommand *mariaDBCommand) final
std::deque< MariaDBCommand * > commandSequence
MariaDBCommandSequence(MariaDBCommandSequence &&mariaDBCommandSequence) noexcept=default
const SubCommand * getPtr() const
SubCommand * getPtr()
~AppWithPtr() override
AppWithPtr(const std::string &description, const std::string &name, SubCommand *t, bool manage)
SubCommand * ptr
Definition SubCommand.h:92
CLI::Option * enforceLogFileOpt
Definition Config.h:88
std::string pidDirectory
Definition Config.h:81
bool parse1(int argc, char *argv[])
Definition Config.cpp:468
CLI::Option * logFileOpt
Definition Config.h:84
CLI::Option * versionOpt
Definition Config.h:92
bool parse2(int argc, char *argv[], bool parse1=false)
Definition Config.cpp:547
CLI::Option * verboseLevelOpt
Definition Config.h:90
CLI::Option * killOpt
Definition Config.h:94
CLI::Option * aliasOpt
Definition Config.h:95
bool bootstrap(int argc, char *argv[])
Definition Config.cpp:500
std::string applicationName
Definition Config.h:80
CLI::Option * userNameOpt
Definition Config.h:86
CLI::Option * daemonizeOpt
Definition Config.h:83
CLI::Option * monochromLogOpt
Definition Config.h:85
ConfigRoot * addRootOptions(const std::string &applicationName, const std::string &userName, const std::string &groupName, const std::string &configDirectory, const std::string &logDirectory, const std::string &pidDirectory)
Definition Config.cpp:366
CLI::Option * writeConfigOpt
Definition Config.h:93
CLI::Option * groupNameOpt
Definition Config.h:87
CLI::Option * quietOpt
Definition Config.h:91
CLI::Option * logLevelOpt
Definition Config.h:89
~ConfigRoot() override
Definition Config.cpp:363
static std::string applicationName
Definition Config.h:123
Config(const Config &)=delete
static const std::string & getApplicationName()
Definition Config.cpp:786
static std::string configDirectory
Definition Config.h:127
static std::string pidDirectory
Definition Config.h:129
static std::shared_ptr< AppWithPtr > configRootApp
Definition Config.h:125
~Config()=delete
static void terminate()
Definition Config.cpp:782
static void parse()
Definition Config.cpp:778
static int argc
Definition Config.h:120
static std::string logDirectory
Definition Config.h:128
static bool init(int argc, char *argv[])
Definition Config.cpp:652
static ConfigRoot configRoot
Definition Config.h:117
Config & operator=(const Config &)=delete
Config()=delete
static char ** argv
Definition Config.h:121
static bool bootstrap()
Definition Config.cpp:774
static int getLogLevel()
Definition Config.cpp:790
static int getVerboseLevel()
Definition Config.cpp:794
std::string version() const
NewSubCommand * newSubCommand(Args &&... args)
Definition SubCommand.h:266
SubCommand * description(const std::string &description)
CLI::Option * addOptionFunction(const std::string &name, std::function< void(const std::string &)> &callback, const std::string &description, const std::string &typeName, ValueTypeT defaultValue, const CLI::Validator &validator) const
Definition SubCommand.h:318
SubCommand * required(SubCommand *subCommand, bool required=true)
CLI::Option * addOptionVariable(const std::string &name, ValueTypeT &variable, const std::string &description, const std::string &typeName, ValueTypeT defaultValue, const CLI::Validator &additionalValidator) const
Definition SubCommand.h:308
SubCommand * allowExtras(bool allow=true)
SubCommand * required(bool required=true, bool force=true)
CLI::Option * addOptionFunction(const std::string &name, std::function< void(const std::string &)> &callback, const std::string &description, const std::string &typeName, const CLI::Validator &validator) const
SubCommand * footer(const std::string &footer)
RequestedSubCommand * getSubCommand()
Definition SubCommand.h:271
SubCommand(SubCommand *parent, ConcretSubCommand *concretSubCommand, const std::string &group, bool final=true)
Definition SubCommand.h:257
SubCommand * disabled(SubCommand *subCommand, bool disabled=true)
SubCommand(SubCommand *parent, std::shared_ptr< utils::AppWithPtr > appWithPtr, const std::string &group, bool final)
static CLI::App * showConfigTriggerApp
Definition SubCommand.h:238
SubCommand * getParent()
CLI::Option * addFlag(const std::string &name, const std::string &description, const std::string &typeName, ValueTypeT defaultValue, const CLI::Validator &validator) const
Definition SubCommand.h:328
CLI::Option * addOption(const std::string &name, const std::string &description, const std::string &typeName, ValueTypeT defaultValue, const CLI::Validator &validator) const
Definition SubCommand.h:289
SubCommand * required(CLI::Option *option, bool required=true)
bool hasParent() const
CLI::Option * addFlagFunction(const std::string &name, const std::function< void()> &callback, const std::string &description, const std::string &typeName, const CLI::Validator &validator)
RequestedSubCommand * getSubCommand() const
Definition SubCommand.h:280
CLI::Option * setDefaultValue(CLI::Option *option, const ValueTypeT &value, bool clear=true) const
Definition SubCommand.h:337
CLI::Option * setVersionFlag(const std::string &version) const
CLI::Option * addFlagFunction(const std::string &name, const std::function< void()> &callback, const std::string &description, const std::string &typeName, const std::string &defaultValue, const CLI::Validator &validator)
CLI::Option * commandlineOpt
Definition SubCommand.h:251
CLI::Option * helpOpt
Definition SubCommand.h:249
SubCommand * finalCallback(const std::function< void()> &finalCallback)
AppWithPtr * subCommandApp
Definition SubCommand.h:244
virtual ~SubCommand()
static std::shared_ptr< CLI::Formatter > sectionFormatter
Definition SubCommand.h:232
SubCommand * parent
Definition SubCommand.h:245
static CLI::App * helpTriggerApp
Definition SubCommand.h:237
CLI::Option * addOption(const std::string &name, const std::string &description, const std::string &typeName, const CLI::Validator &validator) const
CLI::Option * getOption(const std::string &name) const
SubCommand & operator=(const SubCommand &)=delete
SubCommand(const SubCommand &)=delete
static CLI::App * getHelpTriggerApp()
static CLI::App * getCommandlineTriggerApp()
SubCommand & operator=(SubCommand &&)=delete
CLI::Option * setConfig(const std::string &defaultConfigFile) const
CLI::Option * showConfigOpt
Definition SubCommand.h:250
static std::map< std::string, std::string > aliases
Definition SubCommand.h:235
std::string help(const CLI::App *helpApp, const CLI::AppFormatMode &mode) const
SubCommand * needs(SubCommand *subCommand, bool needs=true)
void parse(int argc, char *argv[])
std::string configToStr() const
static CLI::App * commandlineTriggerApp
Definition SubCommand.h:239
CLI::Option * setLogFile(const std::string &defaultLogFile) const
static CLI::App * getShowConfigTriggerApp()
CLI::Option * setConfigurable(CLI::Option *option, bool configurable) const
SubCommand(SubCommand &&)=delete
std::string getName() const
CLI::Option * addFlag(const std::string &name, const std::string &description, const std::string &typeName, const CLI::Validator &validator) const
CLI::Option * initialize(CLI::Option *option, const std::string &typeName, const CLI::Validator &validator, bool configurable) const
SubCommand * setRequireCallback(const std::function< void(void)> &callback)
CLI::Option * addOptionVariable(const std::string &name, ValueTypeT &variable, const std::string &description, const std::string &typeName, const CLI::Validator &additionalValidator) const
Definition SubCommand.h:298
Timeval(const std::initializer_list< time_t > &initList) noexcept
Definition Timeval.cpp:59
int main(int argc, char *argv[])
Definition Timer.h:59
TickStatus
Definition TickStatus.h:51
State
Definition State.h:51
@ RUNNING
Definition State.h:51
@ INITIALIZED
Definition State.h:51
@ STOPPING
Definition State.h:51
State eventLoopState()
Definition State.cpp:52
Definition Config.h:54