1#ifndef KOUTIL_ARGS_COMMAND_H
2#define KOUTIL_ARGS_COMMAND_H
15#include <unordered_map>
30template <extends_result Result = result_base_t>
class command_t {
50 template <
void_handle<std::span<const
char* const>, result_t&> Handle>
54 ,
m_handle(std::forward<Handle>(handle)) { }
70 [[nodiscard]] std::string_view
name()
const {
return m_name; }
135 void process(
const char*
const* args, std::uint32_t argc,
result_t& result);
149 void show_help(std::ostream& out, std::size_t terminal_size = 80)
const;
165 [[nodiscard]] std::size_t
operator()(std::string_view s)
const {
return std::hash<std::string_view>()(s); }
167 [[nodiscard]] std::size_t
operator()(
const std::string& s)
const {
return std::hash<std::string>()(s); }
179 std::unordered_map<std::string_view, std::uint32_t, string_hash>
m_cmd_map;
Represents a positional command-line argument.
Definition argument.h:18
Represents a command in the command-line interface.
Definition help.h:15
void check_options(result_t &result)
Definition command_impl.h:232
std::unordered_map< std::string_view, std::uint32_t, string_hash > m_cmd_map
Definition command.h:179
std::function< void(std::span< const char *const >, result_t &)> handle_t
Function handle type for command execution.
Definition command.h:41
void clear_used()
Clears state.
Definition command_impl.h:246
option_t< result_t > option_t
Definition command.h:33
Result result_t
Definition command.h:32
bool add_option(const option_t &option)
Adds an option to the command.
Definition command_impl.h:41
std::vector< argument_t > m_arguments
Definition command.h:176
std::vector< command_t > m_commands
Definition command.h:178
const std::string & path() const
Returns the full path of parent commands, excluding this command’s name.
Definition command.h:103
command_t(std::string_view name, std::string_view description, Handle &&handle)
Constructs a command with a name, description, and handler.
Definition command.h:51
std::string_view description() const
Gets the command description.
Definition command.h:76
void add_argument(const argument_t &argument)
Adds a positional argument.
Definition command_impl.h:65
argument_t< result_t > argument_t
Definition command.h:34
std::vector< option_t > m_options
Definition command.h:177
bool add_command(command_t &&command)
Adds a subcommand.
Definition command_impl.h:24
void process(const char *const *args, std::uint32_t argc, result_t &result)
Processes command-line arguments.
Definition command_impl.h:70
std::unordered_map< std::string_view, std::uint32_t, string_hash > m_long_options
Definition command.h:181
const auto & commands() const
Gets the list of subcommands.
Definition command.h:94
std::string m_path
Definition command.h:170
std::string_view name() const
Gets the command name.
Definition command.h:70
std::string_view m_description
Definition command.h:173
std::unordered_map< char, std::uint32_t > m_short_options
Definition command.h:182
void show_help(std::ostream &out, std::size_t terminal_size=80) const
Displays help text for this command.
Definition command_impl.h:240
command_t(std::string_view name, std::string_view description)
Constructs a command with a name and description, without a handler.
Definition command.h:61
const auto & options() const
Gets the list of options.
Definition command.h:88
std::uint32_t process_option(std::string_view name, std::span< const char *const > args, std::uint32_t index, result_t &result)
Definition command_impl.h:147
handle_t m_handle
Definition command.h:174
const auto & arguments() const
Gets the list of positional arguments.
Definition command.h:82
std::string_view m_name
Definition command.h:172
Represents a command-line option.
Definition option.h:41
std::size_t operator()(std::string_view s) const
Definition command.h:165
std::size_t operator()(const std::string &s) const
Definition command.h:167
void is_transparent
Definition command.h:163