koutil
Loading...
Searching...
No Matches
command.h
Go to the documentation of this file.
1#ifndef KOUTIL_ARGS_COMMAND_H
2#define KOUTIL_ARGS_COMMAND_H
3
8#include <cstddef>
9#include <cstdint>
10#include <functional>
11#include <ostream>
12#include <span>
13#include <string>
14#include <string_view>
15#include <unordered_map>
16#include <utility>
17#include <vector>
18
19namespace koutil::args {
20
30template <extends_result Result = result_base_t> class command_t {
31public:
32 using result_t = Result;
41 using handle_t = std::function<void(std::span<const char* const>, result_t&)>;
42
50 template <void_handle<std::span<const char* const>, result_t&> Handle>
51 explicit command_t(std::string_view name, std::string_view description, Handle&& handle)
52 : m_name(name)
54 , m_handle(std::forward<Handle>(handle)) { }
55
61 explicit command_t(std::string_view name, std::string_view description)
62 : m_name(name)
64 , m_handle(nullptr) { }
65
70 [[nodiscard]] std::string_view name() const { return m_name; }
71
76 [[nodiscard]] std::string_view description() const { return m_description; }
77
82 [[nodiscard]] const auto& arguments() const { return m_arguments; }
83
88 [[nodiscard]] const auto& options() const { return m_options; }
89
94 [[nodiscard]] const auto& commands() const { return m_commands; }
95
103 const std::string& path() const { return m_path; }
104
111 bool add_command(command_t&& command);
112
119 bool add_option(const option_t& option);
120
127 void add_argument(const argument_t& argument);
128
135 void process(const char* const* args, std::uint32_t argc, result_t& result);
136
142 void process(std::span<const char* const> args, result_t& result);
143
149 void show_help(std::ostream& out, std::size_t terminal_size = 80) const;
150
154 void clear_used();
155
156private:
157 std::uint32_t
158 process_option(std::string_view name, std::span<const char* const> args, std::uint32_t index, result_t& result);
159
160 void check_options(result_t& result);
161
162 struct string_hash {
163 using is_transparent = void;
164
165 [[nodiscard]] std::size_t operator()(std::string_view s) const { return std::hash<std::string_view>()(s); }
166
167 [[nodiscard]] std::size_t operator()(const std::string& s) const { return std::hash<std::string>()(s); }
168 };
169
170 std::string m_path;
171
172 std::string_view m_name;
173 std::string_view m_description;
175
176 std::vector<argument_t> m_arguments;
177 std::vector<option_t> m_options;
178 std::vector<command_t> m_commands;
179 std::unordered_map<std::string_view, std::uint32_t, string_hash> m_cmd_map;
180
181 std::unordered_map<std::string_view, std::uint32_t, string_hash> m_long_options;
182 std::unordered_map<char, std::uint32_t> m_short_options;
183};
184
185}
186
187#endif
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
Definition argument.h:9
Definition command.h:162
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