From 1b8ebaa128a0522a39ad0d6cb9451331fa207d06 Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Sat, 30 Sep 2023 18:22:46 +0200 Subject: [PATCH] Support multi command in CLI, with commands starting without -- (for instance, 'trade' instead of '--trade') --- src/engine/include/coincentercommands.hpp | 10 +- src/engine/include/coincenteroptions.hpp | 173 +++++++++--------- src/engine/include/commandlineoption.hpp | 14 +- .../include/commandlineoptionsparser.hpp | 146 +++++++++------ src/engine/src/coincentercommands.cpp | 47 +++-- .../src/commandlineoptionsparseriterator.hpp | 60 ++++++ .../test/commandlineoptionsparser_test.cpp | 76 ++++---- src/main/src/main.cpp | 9 +- src/main/src/processcommandsfromcli.cpp | 6 +- src/main/src/processcommandsfromcli.hpp | 2 +- 10 files changed, 326 insertions(+), 217 deletions(-) create mode 100644 src/engine/src/commandlineoptionsparseriterator.hpp diff --git a/src/engine/include/coincentercommands.hpp b/src/engine/include/coincentercommands.hpp index a504a9ea..73cff34b 100644 --- a/src/engine/include/coincentercommands.hpp +++ b/src/engine/include/coincentercommands.hpp @@ -17,13 +17,17 @@ class CoincenterCommands { CoincenterCommands() noexcept(std::is_nothrow_default_constructible_v) = default; // Builds a CoincenterCommands and add commands from given command line options. - explicit CoincenterCommands(const CoincenterCmdLineOptions &cmdLineOptions); + explicit CoincenterCommands(const CoincenterCmdLineOptions &cmdLineOptions) + : CoincenterCommands(std::span{&cmdLineOptions, 1U}) {} - static CoincenterCmdLineOptions ParseOptions(int argc, const char *argv[]); + // Builds a CoincenterCommands and add commands from given command line options span. + explicit CoincenterCommands(std::span cmdLineOptionsSpan); + + static vector ParseOptions(int argc, const char *argv[]); /// @brief Set this CoincenterCommands from given command line options. /// @return false if only help or version is asked, true otherwise - bool setFromOptions(const CoincenterCmdLineOptions &cmdLineOptions); + bool addOption(const CoincenterCmdLineOptions &cmdLineOptions); std::span commands() const { return _commands; } diff --git a/src/engine/include/coincenteroptions.hpp b/src/engine/include/coincenteroptions.hpp index 118bd8d9..6467a36a 100644 --- a/src/engine/include/coincenteroptions.hpp +++ b/src/engine/include/coincenteroptions.hpp @@ -223,9 +223,9 @@ struct CoincenterAllowedOptions { using CommandLineOptionWithValue = typename AllowedCommandLineOptionsBase::CommandLineOptionWithValue; static constexpr CommandLineOptionWithValue value[] = { - {{{"General", 1}, "--help", 'h', "", "Display this information"}, &OptValueType::help}, - {{{"General", 2}, "--data", "", CoincenterCmdLineOptions::kData}, &OptValueType::dataDir}, - {{{"General", 3}, + {{{"General", 10}, "help", 'h', "", "Display this information"}, &OptValueType::help}, + {{{"General", 20}, "--data", "", CoincenterCmdLineOptions::kData}, &OptValueType::dataDir}, + {{{"General", 30}, "--log", 'v', "", @@ -233,21 +233,21 @@ struct CoincenterAllowedOptions { "Possible values are: (off|critical|error|warning|info|debug|trace) or " "(0-6) (overrides .log.console in general config file)"}, &OptValueType::logConsole}, - {{{"General", 4}, "--log-console", "", "Synonym of --log"}, &OptValueType::logConsole}, - {{{"General", 4}, + {{{"General", 40}, "--log-console", "", "Synonym of --log"}, &OptValueType::logConsole}, + {{{"General", 40}, "--log-file", "", "Sets the log level in files during all execution (overrides .log.file in general config file). " "Number of rotating files to keep and their size is configurable in the general config file"}, &OptValueType::logFile}, - {{{"General", 5}, "--output", 'o', "", CoincenterCmdLineOptions::kOutput}, &OptValueType::apiOutputType}, - {{{"General", 7}, + {{{"General", 50}, "--output", 'o', "", CoincenterCmdLineOptions::kOutput}, &OptValueType::apiOutputType}, + {{{"General", 70}, "--no-secrets", "<[exch1,...]>", "Do not load secrets for specified exchanges.\n" "If no exchange is specified, no key will be loaded at all"}, &OptValueType::noSecrets}, - {{{"General", 8}, + {{{"General", 80}, "--repeat", 'r', "<[n]>", @@ -255,151 +255,144 @@ struct CoincenterAllowedOptions { "Modifying requests such as trades and withdraws are not impacted by this option. " "This is useful for monitoring for instance. 'n' is optional, if not given, will repeat endlessly"}, &OptValueType::repeats}, - {{{"General", 9}, "--repeat-time", "