koutil
Loading...
Searching...
No Matches
argument.h
Go to the documentation of this file.
1#ifndef KOUTIL_ARGS_ARGUMENT_H
2#define KOUTIL_ARGS_ARGUMENT_H
3
6#include <functional>
7#include <string_view>
8
9namespace koutil::args {
10
18template <extends_result Result = result_base_t> class argument_t {
19public:
20 using result_t = Result;
21
23 using handle_t = std::function<void(std::string_view, result_t&)>;
24
30 explicit argument_t(std::string_view name, std::string_view description)
31 : m_name(name)
33 , m_handle(nullptr) { }
34
42 template <void_handle<std::string_view, result_t&> Handle>
43 explicit argument_t(std::string_view name, std::string_view description, Handle&& handle)
44 : m_name(name)
46 , m_handle(std::forward<Handle>(handle)) { }
47
52 [[nodiscard]] std::string_view name() const { return m_name; }
53
58 [[nodiscard]] std::string_view description() const { return m_description; }
59
65 void process(std::string_view value, result_t& result) const;
66
67private:
68 std::string_view m_name;
69 std::string_view m_description;
70
72};
73
74template <extends_result Result> void argument_t<Result>::process(std::string_view value, result_t& result) const {
75 if (m_handle) {
76 m_handle(value, result);
77 }
78}
79
80}
81
82#endif
Represents a positional command-line argument.
Definition argument.h:18
std::string_view m_name
Definition argument.h:68
argument_t(std::string_view name, std::string_view description)
Constructs an argument with a name and description.
Definition argument.h:30
std::string_view m_description
Definition argument.h:69
Result result_t
Definition argument.h:20
std::function< void(std::string_view, result_t &)> handle_t
Type alias for argument handler function.
Definition argument.h:23
std::string_view name() const
Gets the argument name.
Definition argument.h:52
std::string_view description() const
Gets the argument description.
Definition argument.h:58
argument_t(std::string_view name, std::string_view description, Handle &&handle)
Constructs an argument with a name, description, and handler.
Definition argument.h:43
void process(std::string_view value, result_t &result) const
Processes the argument value using the assigned handler.
Definition argument.h:74
handle_t m_handle
Definition argument.h:71
Definition argument.h:9