koutil
Loading...
Searching...
No Matches
option.h
Go to the documentation of this file.
1#ifndef KOUTIL_ARGS_OPTION_H
2#define KOUTIL_ARGS_OPTION_H
3
6#include <cassert>
7#include <functional>
8#include <optional>
9#include <string_view>
10#include <utility>
11
12namespace koutil::args {
13
23 std::optional<std::string_view> long_name;
24 std::optional<char> short_name;
25 std::string_view description;
26 std::string_view value_name = "value";
27 bool has_value = false;
28 bool required = false;
29};
30
41template <extends_result Result = result_base_t> class option_t {
42public:
43 using result_t = Result;
45 using handle_t = std::function<void(std::optional<std::string_view>, result_t&)>;
46
53 template <void_handle<std::optional<std::string_view>, result_t&> Handle>
54 explicit option_t(const option_data_t& data, Handle&& handle)
56 , m_required(data.required)
61 , m_handle(std::forward<Handle>(handle)) { }
62
67 [[nodiscard]] bool required() const { return m_required; }
68
73 [[nodiscard]] bool used() const { return m_used; }
74
79 [[nodiscard]] auto long_name() const { return m_long_name; }
80
85 [[nodiscard]] auto short_name() const { return m_short_name; }
86
91 [[nodiscard]] std::string_view description() const { return m_description; }
92
97 [[nodiscard]] bool has_value() const { return m_has_value; }
98
103 [[nodiscard]] std::string_view value_name() const { return m_value_name; }
104
110 void process(std::optional<std::string_view> value, result_t& result);
111
115 void clear_used() { m_used = false; }
116
117private:
120
121 bool m_used = false;
122
123 std::optional<std::string_view> m_long_name;
124 std::optional<char> m_short_name;
125 std::string_view m_description;
126 std::string_view m_value_name;
127
129};
130
131template <extends_result Result>
132void option_t<Result>::process(std::optional<std::string_view> value, result_t& result) {
133 m_used = true;
134
135 if (m_handle) {
136 m_handle(value, result);
137 }
138}
139
140}
141
142#endif
Represents a command-line option.
Definition option.h:41
handle_t m_handle
Definition option.h:128
std::string_view value_name() const
Gets the display name for the option value.
Definition option.h:103
std::optional< char > m_short_name
Definition option.h:124
bool used() const
Checks if the option has been used.
Definition option.h:73
std::string_view m_value_name
Definition option.h:126
bool m_used
Definition option.h:121
auto short_name() const
Gets the short option name.
Definition option.h:85
bool required() const
Checks if the option is required.
Definition option.h:67
option_t(const option_data_t &data, Handle &&handle)
Constructs an option from data and a handler.
Definition option.h:54
bool m_has_value
Definition option.h:118
std::function< void(std::optional< std::string_view >, result_t &)> handle_t
Type alias for option handler function.
Definition option.h:45
Result result_t
Definition option.h:43
bool m_required
Definition option.h:119
void clear_used()
Clears state.
Definition option.h:115
auto long_name() const
Gets the long option name.
Definition option.h:79
std::string_view description() const
Gets the option description.
Definition option.h:91
void process(std::optional< std::string_view > value, result_t &result)
Processes the option value using the assigned handler.
Definition option.h:132
std::optional< std::string_view > m_long_name
Definition option.h:123
bool has_value() const
Checks if the option expects a value.
Definition option.h:97
std::string_view m_description
Definition option.h:125
Definition argument.h:9
Holds metadata for an option.
Definition option.h:22
std::string_view description
Description of the option.
Definition option.h:25
std::optional< char > short_name
Short form name (without "-").
Definition option.h:24
bool has_value
True if the option expects a value.
Definition option.h:27
std::string_view value_name
Name used to represent the option value.
Definition option.h:26
std::optional< std::string_view > long_name
Long form name (without "--").
Definition option.h:23
bool required
True if the option is mandatory.
Definition option.h:28