Skip to content

Commit

Permalink
Support multi command in CLI, with commands starting without -- (for …
Browse files Browse the repository at this point in the history
…instance, 'trade' instead of '--trade')
  • Loading branch information
sjanel committed Oct 1, 2023
1 parent 7e5cc4a commit d9b58a7
Show file tree
Hide file tree
Showing 13 changed files with 647 additions and 480 deletions.
50 changes: 25 additions & 25 deletions CONFIG.md

Large diffs are not rendered by default.

244 changes: 134 additions & 110 deletions README.md

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions src/engine/include/coincentercommands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ class CoincenterCommands {
CoincenterCommands() noexcept(std::is_nothrow_default_constructible_v<Commands>) = 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<const CoincenterCmdLineOptions>{&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<const CoincenterCmdLineOptions> cmdLineOptionsSpan);

static vector<CoincenterCmdLineOptions> 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<const CoincenterCommand> commands() const { return _commands; }

Expand Down
239 changes: 112 additions & 127 deletions src/engine/include/coincenteroptions.hpp

Large diffs are not rendered by default.

33 changes: 22 additions & 11 deletions src/engine/include/commandlineoption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <compare>
#include <optional>
#include <string_view>
#include <type_traits>
#include <variant>

#include "cct_cctype.hpp"
Expand All @@ -21,11 +22,11 @@ class CommandLineOption {

template <class StringViewType>
constexpr CommandLineOption(GroupNameAndPrio optionGroupName, const char* fullName, char shortName,
const char* valueDescription, StringViewType description)
const char* valueDescription, StringViewType descriptionStr)
: _optionGroupName(optionGroupName.first),
_fullName(fullName),
_valueDescription(valueDescription),
_description(description),
_description(descriptionStr),
_prio(optionGroupName.second),
_shortName(shortName) {
static_assert(std::is_same_v<StringViewType, std::string_view> || std::is_same_v<StringViewType, const char*>);
Expand All @@ -47,9 +48,11 @@ class CommandLineOption {

constexpr bool hasShortName() const { return _shortName != '\0'; }

constexpr std::strong_ordering operator<=>(const CommandLineOption& o) const;
constexpr std::strong_ordering operator<=>(const CommandLineOption& other) const;

private:
static constexpr std::string_view kLegacyFullNamePrefixOption = "--";

std::string_view _optionGroupName;
std::string_view _fullName;
std::string_view _valueDescription;
Expand Down Expand Up @@ -92,19 +95,27 @@ struct AllowedCommandLineOptionsBase {

constexpr bool CommandLineOption::matches(std::string_view optName) const {
if (optName.size() == 2 && optName.front() == '-' && optName.back() == _shortName) {
return true;
return true; // it is a short hand flag
}
if (optName == _fullName) {
return true; // standard full match
}
if (optName.starts_with(kLegacyFullNamePrefixOption)) {
// backwards compatibility check
optName.remove_prefix(kLegacyFullNamePrefixOption.length());
return optName == _fullName;
}
return optName == _fullName;
return false;
}

constexpr std::strong_ordering CommandLineOption::operator<=>(const CommandLineOption& o) const {
if (_prio != o._prio) {
return _prio <=> o._prio;
constexpr std::strong_ordering CommandLineOption::operator<=>(const CommandLineOption& other) const {
if (_prio != other._prio) {
return _prio <=> other._prio;
}
if (_optionGroupName != o._optionGroupName) {
return _optionGroupName <=> o._optionGroupName;
if (_optionGroupName != other._optionGroupName) {
return _optionGroupName <=> other._optionGroupName;
}
return _fullName <=> o._fullName;
return _fullName <=> other._fullName;
}

} // namespace cct
Loading

0 comments on commit d9b58a7

Please sign in to comment.