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 <set>
51#include <string>
52#include <utility>
53
54#ifdef __GNUC__
55#pragma GCC diagnostic push
56#pragma GCC diagnostic ignored "-Wfloat-equal"
57#ifdef __has_warning
58#if __has_warning("-Wweak-vtables")
59#pragma GCC diagnostic ignored "-Wweak-vtables"
60#endif
61#if __has_warning("-Wcovered-switch-default")
62#pragma GCC diagnostic ignored "-Wcovered-switch-default"
63#endif
64#if __has_warning("-Wmissing-noreturn")
65#pragma GCC diagnostic ignored "-Wmissing-noreturn"
66#endif
67#if __has_warning("-Wnrvo")
68#pragma GCC diagnostic ignored "-Wnrvo"
69#endif
70#endif
71#endif
72#include "utils/CLI11.hpp" // IWYU pragma: export
73#ifdef __GNUC__
74#pragma GCC diagnostic pop
75#endif
76
77#endif // DOXYGEN_SHOULD_SKIP_THIS
78
79namespace utils {
80 class SubCommand;
81
82 class AppWithPtr : public CLI::App {
83 public:
84 AppWithPtr(const std::string& description, const std::string& name, SubCommand* t);
85
86 ~AppWithPtr() override;
87
88 const SubCommand* getPtr() const;
90
91 void validate();
92
93 private:
95 };
96
97 class SubCommand {
98 protected:
99 SubCommand(SubCommand* parent, std::shared_ptr<utils::AppWithPtr> appWithPtr, const std::string& group, bool final = false);
100
101 template <typename ConcretSubCommand>
102 SubCommand(SubCommand* parent, ConcretSubCommand* concretSubCommand, const std::string& group, bool final = false);
103
104 public:
105 SubCommand(const SubCommand&) = delete;
107
108 SubCommand& operator=(const SubCommand&) = delete;
110
111 virtual ~SubCommand();
112
113 std::string getName() const;
114 std::string version() const;
115
116 protected:
117 void parse(int argc, char* argv[]) const;
118
119 SubCommand* description(const std::string& description);
120 SubCommand* footer(const std::string& footer);
121
122 void removeSubCommand();
123
124 public:
125 std::string configToStr() const;
126 std::string help(const CLI::App* helpApp, const CLI::AppFormatMode& mode) const;
127
128 bool hasParent() const;
129 SubCommand* getParent() const;
130
131 SubCommand* allowExtras(bool allow = true);
132
133 SubCommand* forceUnrequired(bool unrequired = true);
134 SubCommand* required(bool required = true);
135 SubCommand* required(CLI::Option* option, bool required = true);
136
137 bool getRequired() const;
138
139 SubCommand* needs(SubCommand* subCommand, bool needs = true);
140
141 SubCommand* setRequireCallback(const std::function<void(void)>& callback);
142 SubCommand* finalCallback(const std::function<void()>& finalCallback);
143
144 void addSubCommandApp(std::shared_ptr<utils::AppWithPtr> subCommand);
145
146 template <typename NewSubCommand, typename... Args>
147 NewSubCommand* newSubCommand(Args&&... args);
148
149 template <typename RequestedSubCommand>
150 RequestedSubCommand* getSubCommand();
151
152 template <typename RequestedSubCommand>
153 RequestedSubCommand* getSubCommand() const;
154
155 protected:
156 CLI::Option* addConfigFlag(const std::string& defaultConfigFile) const;
157 CLI::Option* addLogFileFlag(const std::string& defaultLogFile) const;
158 CLI::Option* addVersionFlag(const std::string& version) const;
159
160 public:
161 CLI::Option* addOption(const std::string& name,
162 const std::string& description,
163 const std::string& typeName,
164 const CLI::Validator& validator) const;
165
166 template <typename ValueTypeT>
167 CLI::Option* addOption(const std::string& name,
168 const std::string& description,
169 const std::string& typeName,
170 ValueTypeT defaultValue,
171 const CLI::Validator& validator) const;
172
173 template <typename ValueTypeT>
174 CLI::Option* addOptionVariable(const std::string& name,
175 ValueTypeT& variable,
176 const std::string& description,
177 const std::string& typeName,
178 const CLI::Validator& additionalValidator) const;
179
180 template <typename ValueTypeT>
181 CLI::Option* addOptionVariable(const std::string& name,
182 ValueTypeT& variable,
183 const std::string& description,
184 const std::string& typeName,
185 ValueTypeT defaultValue,
186 const CLI::Validator& additionalValidator) const;
187
188 CLI::Option* addOptionFunction(const std::string& name,
189 const std::function<void(const std::string&)>& callback,
190 const std::string& description,
191 const std::string& typeName,
192 const CLI::Validator& validator) const;
193
194 template <typename ValueTypeT>
195 CLI::Option* addOptionFunction(const std::string& name,
196 const std::function<void(const std::string&)>& callback,
197 const std::string& description,
198 const std::string& typeName,
199 ValueTypeT defaultValue,
200 const CLI::Validator& validator) const;
201
202 CLI::Option* addFlag(const std::string& name,
203 const std::string& description,
204 const std::string& typeName,
205 const CLI::Validator& validator) const;
206
207 template <typename ValueTypeT>
208 CLI::Option* addFlag(const std::string& name,
209 const std::string& description,
210 const std::string& typeName,
211 ValueTypeT defaultValue,
212 const CLI::Validator& validator) const;
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 CLI::Validator& validator) const;
219
220 CLI::Option* addFlagFunction(const std::string& name,
221 const std::function<void()>& callback,
222 const std::string& description,
223 const std::string& typeName,
224 const std::string& defaultValue,
225 const CLI::Validator& validator) const;
226
227 CLI::Option* getOption(const std::string& name) const;
228
229 template <typename ValueTypeT>
230 CLI::Option* setDefaultValue(CLI::Option* option, const ValueTypeT& value, bool clear = true) const;
231
232 CLI::Option* setConfigurable(CLI::Option* option, bool configurable) const;
233
234 protected:
235 static CLI::App* getHelpTriggerApp();
236 static CLI::App* getShowConfigTriggerApp();
237 static CLI::App* getCommandlineTriggerApp();
238
240
241 static CLI::App* helpTriggerApp;
244
245 private:
246 CLI::Option* initialize(CLI::Option* option, const std::string& typeName, const CLI::Validator& validator, bool configurable) const;
247
249 std::string name;
251
252 protected:
253 CLI::Option* helpOpt = nullptr;
254 CLI::Option* showConfigOpt = nullptr;
255 CLI::Option* commandlineOpt = nullptr;
256
257 private:
259 // Owns only SubCommand objects allocated by newSubCommand().
260 // ConfigSection base subobjects registered through multiple inheritance are
261 // attached to the CLI tree but must not be deleted through this set.
263
265 bool requiredForced = false;
266
267 bool final;
268 };
269
270 template <typename ConcretSubCommand>
271 inline SubCommand::SubCommand(SubCommand* parent, ConcretSubCommand* concretSubCommand, const std::string& group, bool final)
272 : SubCommand(parent,
273 std::make_shared<utils::AppWithPtr>(
274 std::string(ConcretSubCommand::DESCRIPTION), std::string(ConcretSubCommand::NAME), concretSubCommand),
275 group,
276 final) {
277 }
278
279 template <typename NewSubCommand, typename... Args>
280 NewSubCommand* SubCommand::newSubCommand(Args&&... args) {
281 return !final ? dynamic_cast<NewSubCommand*>(*childSubCommands.insert(new NewSubCommand(this, std::forward(args)...)).first)
282 : nullptr;
283 }
284
285 template <typename RequestedSubCommand>
286 RequestedSubCommand* SubCommand::getSubCommand() {
287 auto* appWithPtr = subCommandApp->get_subcommand_no_throw(std::string(RequestedSubCommand::NAME));
288
289 AppWithPtr* subCommandApp = nullptr;
290
291 if (appWithPtr != nullptr) {
292 subCommandApp = dynamic_cast<utils::AppWithPtr*>(appWithPtr);
293 }
294
295 return subCommandApp != nullptr ? dynamic_cast<RequestedSubCommand*>(subCommandApp->getPtr()) : nullptr;
296 }
297
298 template <typename RequestedSubCommand>
299 RequestedSubCommand* SubCommand::getSubCommand() const {
300 auto* appWithPtr = subCommandApp->get_subcommand_no_throw(std::string(RequestedSubCommand::NAME));
301
302 AppWithPtr* subCommandApp = nullptr;
303
304 if (appWithPtr != nullptr) {
305 subCommandApp = dynamic_cast<utils::AppWithPtr*>(appWithPtr);
306 }
307
308 return subCommandApp != nullptr ? dynamic_cast<RequestedSubCommand*>(subCommandApp->getPtr()) : nullptr;
309 }
310
311 template <typename ValueTypeT>
312 CLI::Option* SubCommand::addOption(const std::string& name,
313 const std::string& description,
314 const std::string& typeName,
315 ValueTypeT defaultValue,
316 const CLI::Validator& additionalValidator) const {
317 return setDefaultValue(addOption(name, description, typeName, additionalValidator), defaultValue);
318 }
319
320 template <typename ValueTypeT>
321 CLI::Option* SubCommand::addOptionVariable(const std::string& name,
322 ValueTypeT& variable,
323 const std::string& description,
324 const std::string& typeName,
325 const CLI::Validator& additionalValidator) const {
326 return initialize(
327 subCommandApp->add_option(name, variable, description), typeName, additionalValidator, !subCommandApp->get_disabled());
328 }
329
330 template <typename ValueTypeT>
331 CLI::Option* SubCommand::addOptionVariable(const std::string& name,
332 ValueTypeT& variable,
333 const std::string& description,
334 const std::string& typeName,
335 ValueTypeT defaultValue,
336 const CLI::Validator& additionalValidator) const {
337 return setDefaultValue(addOptionVariable(name, variable, description, typeName, additionalValidator), defaultValue);
338 }
339
340 template <typename ValueTypeT>
341 CLI::Option* SubCommand::addOptionFunction(const std::string& name,
342 const std::function<void(const std::string&)>& callback,
343 const std::string& description,
344 const std::string& typeName,
345 ValueTypeT defaultValue,
346 const CLI::Validator& validator) const {
347 return setDefaultValue(addOptionFunction(name, callback, description, typeName, validator), defaultValue);
348 }
349
350 template <typename ValueTypeT>
351 CLI::Option* SubCommand::addFlag(const std::string& name,
352 const std::string& description,
353 const std::string& typeName,
354 ValueTypeT defaultValue,
355 const CLI::Validator& additionalValidator) const {
356 return setDefaultValue(addFlag(name, description, typeName, additionalValidator), defaultValue);
357 }
358
359 template <typename ValueTypeT>
360 CLI::Option* SubCommand::setDefaultValue(CLI::Option* option, const ValueTypeT& value, bool clear) const {
361 try {
362 option->default_val(value);
363
364 if (clear) {
365 option->clear();
366 }
367 } catch (const CLI::ParseError&) {
368 option = nullptr;
369 }
370
371 return option;
372 }
373
374} // namespace utils
375
376#endif // UTILS_SUBCOMMAND_H
#define VLOG(level)
Definition Logger.h:164
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
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
Definition Logger.cpp:280
const SubCommand * getPtr() const
SubCommand * getPtr()
~AppWithPtr() override
AppWithPtr(const std::string &description, const std::string &name, SubCommand *t)
SubCommand * ptr
Definition SubCommand.h:94
CLI::Option * enforceLogFileOpt
Definition Config.h:87
std::string pidDirectory
Definition Config.h:80
bool parse1(int argc, char *argv[])
Definition Config.cpp:463
CLI::Option * logFileOpt
Definition Config.h:83
CLI::Option * versionOpt
Definition Config.h:91
bool parse2(int argc, char *argv[], bool parse1=false)
Definition Config.cpp:552
CLI::Option * verboseLevelOpt
Definition Config.h:89
CLI::Option * killOpt
Definition Config.h:93
CLI::Option * aliasOpt
Definition Config.h:94
bool bootstrap(int argc, char *argv[])
Definition Config.cpp:501
std::string applicationName
Definition Config.h:79
CLI::Option * userNameOpt
Definition Config.h:85
void * operator new(std::size_t)=delete
CLI::Option * daemonizeOpt
Definition Config.h:82
CLI::Option * monochromLogOpt
Definition Config.h:84
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:361
CLI::Option * writeConfigOpt
Definition Config.h:92
CLI::Option * groupNameOpt
Definition Config.h:86
CLI::Option * quietOpt
Definition Config.h:90
CLI::Option * logLevelOpt
Definition Config.h:88
~ConfigRoot() override
Definition Config.cpp:358
static std::string applicationName
Definition Config.h:122
Config(const Config &)=delete
static const std::string & getApplicationName()
Definition Config.cpp:791
static std::string configDirectory
Definition Config.h:124
static std::string pidDirectory
Definition Config.h:126
~Config()=delete
static void terminate()
Definition Config.cpp:787
static void parse()
Definition Config.cpp:783
static int argc
Definition Config.h:119
static std::string logDirectory
Definition Config.h:125
static bool init(int argc, char *argv[])
Definition Config.cpp:657
static ConfigRoot configRoot
Definition Config.h:116
Config & operator=(const Config &)=delete
Config()=delete
static char ** argv
Definition Config.h:120
static bool bootstrap()
Definition Config.cpp:779
static int getLogLevel()
Definition Config.cpp:795
static int getVerboseLevel()
Definition Config.cpp:799
CLI::Option * addFlagFunction(const std::string &name, const std::function< void()> &callback, const std::string &description, const std::string &typeName, const CLI::Validator &validator) const
std::string version() const
SubCommand * required(bool required=true)
NewSubCommand * newSubCommand(Args &&... args)
Definition SubCommand.h:280
SubCommand * description(const std::string &description)
std::shared_ptr< AppWithPtr > selfAnchoredSubCommandApp
Definition SubCommand.h:258
SubCommand * forceUnrequired(bool unrequired=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:331
SubCommand * allowExtras(bool allow=true)
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) const
bool getRequired() const
SubCommand * footer(const std::string &footer)
RequestedSubCommand * getSubCommand()
Definition SubCommand.h:286
void parse(int argc, char *argv[]) const
static CLI::App * showConfigTriggerApp
Definition SubCommand.h:242
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:351
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:312
SubCommand * required(CLI::Option *option, bool required=true)
bool hasParent() const
SubCommand(SubCommand *parent, std::shared_ptr< utils::AppWithPtr > appWithPtr, const std::string &group, bool final=false)
RequestedSubCommand * getSubCommand() const
Definition SubCommand.h:299
CLI::Option * addLogFileFlag(const std::string &defaultLogFile) const
CLI::Option * setDefaultValue(CLI::Option *option, const ValueTypeT &value, bool clear=true) const
Definition SubCommand.h:360
std::set< SubCommand * > childSubCommands
Definition SubCommand.h:262
CLI::Option * addVersionFlag(const std::string &version) const
CLI::Option * commandlineOpt
Definition SubCommand.h:255
CLI::Option * helpOpt
Definition SubCommand.h:253
SubCommand * finalCallback(const std::function< void()> &finalCallback)
AppWithPtr * subCommandApp
Definition SubCommand.h:248
virtual ~SubCommand()
SubCommand * parent
Definition SubCommand.h:250
static CLI::App * helpTriggerApp
Definition SubCommand.h:241
std::string name
Definition SubCommand.h:249
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 * addOptionFunction(const std::string &name, const 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:341
CLI::Option * showConfigOpt
Definition SubCommand.h:254
static std::map< std::string, std::string > aliases
Definition SubCommand.h:239
std::string help(const CLI::App *helpApp, const CLI::AppFormatMode &mode) const
SubCommand * needs(SubCommand *subCommand, bool needs=true)
std::string configToStr() const
static CLI::App * commandlineTriggerApp
Definition SubCommand.h:243
void addSubCommandApp(std::shared_ptr< utils::AppWithPtr > subCommand)
CLI::Option * addOptionFunction(const std::string &name, const std::function< void(const std::string &)> &callback, const std::string &description, const std::string &typeName, const CLI::Validator &validator) const
SubCommand * getParent() const
static CLI::App * getShowConfigTriggerApp()
SubCommand(SubCommand *parent, ConcretSubCommand *concretSubCommand, const std::string &group, bool final=false)
Definition SubCommand.h:271
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
CLI::Option * addConfigFlag(const std::string &defaultConfigFile) 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:321
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