koutil
Loading...
Searching...
No Matches
command_builder.h
Go to the documentation of this file.
1#ifndef KOUTIL_ARGS_COMMAND_BUILDER_H
2#define KOUTIL_ARGS_COMMAND_BUILDER_H
3
9#include <cassert>
10#include <optional>
11#include <string_view>
12#include <utility>
13
14namespace koutil::args {
15
31template <extends_result Result = result_base_t> class command_builder_t {
32public:
33 using result_t = Result;
37
43 command_builder_t(std::string_view name, std::string_view description)
44 : m_cmd(name, description) { }
45
53 template <void_handle<std::optional<std::string_view>, result_t&> Handle>
54 command_builder_t(std::string_view name, std::string_view description, Handle&& handle)
55 : m_cmd(name, description, std::forward<Handle>(handle)) { }
56
63 assert(m_cmd.add_option(option));
64 return std::move(*this);
65 }
66
73 m_cmd.add_argument(argument);
74 return std::move(*this);
75 }
76
83 assert(m_cmd.add_command(std::move(command)));
84 return std::move(*this);
85 }
86
91 command_t build() && { return std::move(m_cmd); }
92
93private:
95};
96}
97
98#endif
Builder for creating command-line command.
Definition command_builder.h:31
option_t< result_t > option_t
Definition command_builder.h:35
command_t< result_t > command_t
Definition command_builder.h:34
argument_t< result_t > argument_t
Definition command_builder.h:36
command_builder_t(std::string_view name, std::string_view description)
Constructs a builder for a command with name and description.
Definition command_builder.h:43
command_builder_t && add_argument(const argument_t &argument) &&
Adds a positional argument to the command.
Definition command_builder.h:72
command_builder_t && add_command(command_t &&command) &&
Adds a subcommand to the command.
Definition command_builder.h:82
command_builder_t && add_option(const option_t &option) &&
Adds an option to the command.
Definition command_builder.h:62
command_t build() &&
Builds and returns the final command.
Definition command_builder.h:91
command_t m_cmd
Definition command_builder.h:94
Result result_t
Definition command_builder.h:33
command_builder_t(std::string_view name, std::string_view description, Handle &&handle)
Constructs a builder for a command with name, description, and handler.
Definition command_builder.h:54
bool add_option(const option_t &option)
Adds an option to the command.
Definition command_impl.h:41
void add_argument(const argument_t &argument)
Adds a positional argument.
Definition command_impl.h:65
bool add_command(command_t &&command)
Adds a subcommand.
Definition command_impl.h:24
Definition argument.h:9