|
koutil
|
Represents a command in the command-line interface. More...
#include <command.h>
Classes | |
| struct | string_hash |
Public Types | |
| using | result_t = Result |
| using | option_t = option_t<result_t> |
| using | argument_t = argument_t<result_t> |
| using | handle_t = std::function<void(std::span<const char* const>, result_t&)> |
| Function handle type for command execution. | |
Public Member Functions | |
| template<void_handle< std::span< const char *const >, result_t & > Handle> | |
| command_t (std::string_view name, std::string_view description, Handle &&handle) | |
| Constructs a command with a name, description, and handler. | |
| command_t (std::string_view name, std::string_view description) | |
| Constructs a command with a name and description, without a handler. | |
| std::string_view | name () const |
| Gets the command name. | |
| std::string_view | description () const |
| Gets the command description. | |
| const auto & | arguments () const |
| Gets the list of positional arguments. | |
| const auto & | options () const |
| Gets the list of options. | |
| const auto & | commands () const |
| Gets the list of subcommands. | |
| const std::string & | path () const |
| Returns the full path of parent commands, excluding this command’s name. | |
| bool | add_command (command_t &&command) |
| Adds a subcommand. | |
| bool | add_option (const option_t &option) |
| Adds an option to the command. | |
| void | add_argument (const argument_t &argument) |
| Adds a positional argument. | |
| void | process (const char *const *args, std::uint32_t argc, result_t &result) |
| Processes command-line arguments. | |
| void | process (std::span< const char *const > args, result_t &result) |
| Processes command-line arguments. | |
| void | show_help (std::ostream &out, std::size_t terminal_size=80) const |
| Displays help text for this command. | |
| void | clear_used () |
| Clears state. | |
Private Member Functions | |
| std::uint32_t | process_option (std::string_view name, std::span< const char *const > args, std::uint32_t index, result_t &result) |
| void | check_options (result_t &result) |
Private Attributes | |
| std::string | m_path |
| std::string_view | m_name |
| std::string_view | m_description |
| handle_t | m_handle |
| std::vector< argument_t > | m_arguments |
| std::vector< option_t > | m_options |
| std::vector< command_t > | m_commands |
| std::unordered_map< std::string_view, std::uint32_t, string_hash > | m_cmd_map |
| std::unordered_map< std::string_view, std::uint32_t, string_hash > | m_long_options |
| std::unordered_map< char, std::uint32_t > | m_short_options |
Represents a command in the command-line interface.
A command can contain subcommands, options, and arguments.
The order in which options, arguments, and subcommands are added determines their order in the help text. The order of arguments also defines the order in which they are expected on the command line.
| using koutil::args::command_t< extends_result >::argument_t = argument_t<result_t> |
| using koutil::args::command_t< extends_result >::handle_t = std::function<void(std::span<const char* const>, result_t&)> |
Function handle type for command execution.
The handle is called when the parser encounters this command, and it runs before processing the command’s arguments, options, or subcommands.
| using koutil::args::command_t< extends_result >::option_t = option_t<result_t> |
| using koutil::args::command_t< extends_result >::result_t = Result |
|
inlineexplicit |
Constructs a command with a name, description, and handler.
| Handle | Callable matching void_handle<std::span<const char* const>, result_t&>. |
| name | Command name. |
| description | Description shown in help. |
| handle | Function to execute when the command is invoked. |
|
inlineexplicit |
Constructs a command with a name and description, without a handler.
| name | Command name. |
| description | Command description. |
| void koutil::args::command_t< Result >::add_argument | ( | const argument_t & | argument | ) |
Adds a positional argument.
| argument | Argument to add. |
| bool koutil::args::command_t< Result >::add_command | ( | command_t< extends_result > && | command | ) |
Adds a subcommand.
| command | Command to add. |
| bool koutil::args::command_t< Result >::add_option | ( | const option_t & | option | ) |
Adds an option to the command.
| option | Option to add. |
|
inlinenodiscard |
Gets the list of positional arguments.
|
private |
| void koutil::args::command_t< Result >::clear_used | ( | ) |
Clears state.
|
inlinenodiscard |
Gets the list of subcommands.
|
inlinenodiscard |
Gets the command description.
|
inlinenodiscard |
Gets the command name.
|
inlinenodiscard |
Gets the list of options.
|
inline |
Returns the full path of parent commands, excluding this command’s name.
For example:
"library" under "program build", path() returns "program build".| void koutil::args::command_t< Result >::process | ( | const char *const * | args, |
| std::uint32_t | argc, | ||
| result_t & | result ) |
Processes command-line arguments.
| args | Argument array. |
| argc | Number of arguments. |
| result | Result object to update. |
| void koutil::args::command_t< Result >::process | ( | std::span< const char *const > | args, |
| result_t & | result ) |
Processes command-line arguments.
| args | Argument span. |
| result | Result object to update. |
|
private |
| void koutil::args::command_t< Result >::show_help | ( | std::ostream & | out, |
| std::size_t | terminal_size = 80 ) const |
Displays help text for this command.
| out | Output stream. |
| terminal_size | Terminal width for formatting. |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |