1#ifndef KOUTIL_ARGPARSER_SUBCOMMAND_H
2#define KOUTIL_ARGPARSER_SUBCOMMAND_H
52 std::is_base_of_v<SubcommandBase, T>;
53 requires std::same_as<std::decay_t<
decltype(t.name)>, std::string_view>;
62concept are_commands = std::is_base_of_v<CommandsBase, std::decay_t<T>>;
84 [[nodiscard]]
constexpr virtual std::size_t
size()
const = 0;
94 static constexpr std::size_t
count =
sizeof...(Cmd);
99 constexpr Commands(
const Cmd&... cmd)
101 :
m_cmds(std::tuple<Cmd...>(cmd...)) {
110 [[nodiscard]]
constexpr std::size_t
size()
const override {
return count; }
118 [[nodiscard]]
constexpr const SubcommandBase*
find(std::string_view name)
const override {
120 if constexpr (
count == 1) {
122 }
else if constexpr (
count == 0) {
138 if constexpr (
count <= 1) {
150 template <std::
size_t I, std::
size_t J>
153 const auto& cmd = std::get<I>(
m_cmds);
154 const auto& other = std::get<J>(
m_cmds);
156 if (cmd.name == other.name) {
160 if constexpr (J + 1 >=
count) {
167 template <std::
size_t I, std::
size_t J>
180 template <std::
size_t I> [[nodiscard]]
constexpr const SubcommandBase*
find_impl(std::string_view name)
const {
181 if constexpr (I >=
count) {
185 const auto& cmd = std::get<I>(
m_cmds);
187 if (cmd.name == name) {
207 if (m_cmd.name == name) {
214 [[nodiscard]]
constexpr std::size_t
size()
const override {
return 1; }
279template <is_arg... Args, is_subcommand... Cmds>
281 std::string_view name,
283 const Commands<Cmds...>& cmds = Commands<> {}
285 return Subcommand<type::types<Args...>, type::types<Cmds...>>(name, args, cmds);
Base class for argument collections.
Definition arg.h:135
Base class for commands.
Definition subcommand.h:67
virtual constexpr const SubcommandBase * find(std::string_view name) const =0
Finds a subcommand by name.
virtual constexpr std::size_t size() const =0
Gets the number of subcommands.
virtual constexpr ~CommandsBase()=default
Cmd m_cmd
Definition subcommand.h:217
constexpr Commands(const Cmd &cmd)
Definition subcommand.h:203
constexpr std::size_t size() const override
Gets the number of subcommands.
Definition subcommand.h:214
constexpr const SubcommandBase * find(std::string_view name) const override
Finds a subcommand by name.
Definition subcommand.h:206
Collection of subcommands.
Definition subcommand.h:92
constexpr const SubcommandBase * find_impl(std::string_view name) const
Finds a subcommand by name recursively.
Definition subcommand.h:179
std::conditional_t< count !=1, std::tuple< Cmd... >, type::types_get_t< type::types< std::decay_t< Cmd >... >, 0 > > storage_t
Definition subcommand.h:95
constexpr bool contains_duplicate_impl() const
Checks for duplicate subcommand names recursively.*.
Definition subcommand.h:151
constexpr bool contains_duplicate() const
Checks for duplicate subcommand names.
Definition subcommand.h:136
constexpr std::size_t size() const override
Gets the number of subcommands.
Definition subcommand.h:109
static constexpr std::size_t count
Definition subcommand.h:94
storage_t m_cmds
Definition subcommand.h:129
constexpr const SubcommandBase * find(std::string_view name) const override
Finds a subcommand by name.
Definition subcommand.h:117
constexpr Commands(const Cmd &... cmd)
Definition subcommand.h:98
Concept to check if a type satisfies the requirements of a collection of commands.
Definition subcommand.h:62
Checks if a type is Arg.
Definition arg.h:24
Concept to check if a type satisfies the requirements of a subcommand.
Definition subcommand.h:51
constexpr auto make_subcommand(std::string_view name, const Arguments< Args... > &args=Arguments<> {}, const Commands< Cmds... > &cmds=Commands<> {})
Helper function to create a subcommand.
Definition subcommand.h:280
detail::types_get_impl< Types, I >::type types_get_t
Alias for getting a type by index in a types list.
Definition types.h:381
Base class for subcommands.
Definition subcommand.h:20
virtual constexpr const ArgumentsBase & get_args() const =0
Gets the arguments of the subcommand.
constexpr SubcommandBase(std::string_view cmd_name)
Definition subcommand.h:23
virtual constexpr ~SubcommandBase()=default
virtual constexpr const CommandsBase & get_cmds() const =0
Gets the commands of the subcommand.
std::string_view name
Definition subcommand.h:21
constexpr bool operator==(std::string_view n) const
Definition subcommand.h:42
constexpr const CommandsBase & get_cmds() const override
Gets the commands of the subcommand.
Definition subcommand.h:266
constexpr Subcommand(std::string_view cmd_name, const Arguments< Args... > &arguments, const Commands< Cmd... > &commands)
Constructs a Subcommand object with the given name, arguments, and subcommands.
Definition subcommand.h:247
constexpr const ArgumentsBase & get_args() const override
Gets the arguments of the subcommand.
Definition subcommand.h:259
Commands< Cmd... > cmds
Definition subcommand.h:238
Arguments< Args... > args
Definition subcommand.h:237
Subcommand template struct.
Definition subcommand.h:226
A structure representing a variadic list of types.
Definition types.h:20