kaycxx-cli
C++ CLI library
Loading...
Searching...
No Matches
option.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Klaus Reimer <k@ailis.de>
2// SPDX-License-Identifier: MIT
3
9#pragma once
10
11#include <any>
12#include <format>
13#include <optional>
14#include <string>
15#include <string_view>
16#include <utility>
17#include <vector>
18
21
22namespace kaycxx::cli {
23
27class option_base : public switch_base {
28public:
30
36 [[nodiscard]] virtual std::string const& value_name() const noexcept = 0;
37
44
51 return repeatable_;
52 }
53
54private:
55 friend class command;
56
58 void mark_as_repeatable() noexcept {
59 repeatable_ = true;
60 }
61
71 [[nodiscard]] virtual std::any parse_value(std::string_view text) const = 0;
72
81 virtual void append_value(std::any& storage, std::string_view text) const = 0;
82
84 bool repeatable_ = false;
85};
86
92template <parseable_value T>
93class option : public option_base {
94public:
102 explicit option(std::string_view name, std::string_view value_name, std::optional<std::string_view> description = std::nullopt)
104 value_name_(value_name) {}
105
114 explicit option(std::string_view name, char alias, std::string_view value_name, std::optional<std::string_view> description = std::nullopt)
116 value_name_(value_name) {}
117
123 [[nodiscard]] std::string const& value_name() const noexcept override {
124 return value_name_;
125 }
126
133 default_value_ = std::move(value);
134 }
135
141 [[nodiscard]] std::string usage() const override {
142 if (auto alias = this->alias()) {
143 return std::format("-{}, --{} <{}>", *alias, name(), value_name_);
144 }
145
146 return std::format(" --{} <{}>", name(), value_name_);
147 }
148
154 [[nodiscard]] bool expects_value() const noexcept override {
155 return true;
156 }
157
163 [[nodiscard]] std::optional<std::any> default_value() const override {
164 if (default_value_) {
165 if (is_repeatable()) {
166 auto values = std::vector<T>();
167 values.push_back(*default_value_);
168 return std::any(std::move(values));
169 }
170 return std::any(*default_value_);
171 }
172
173 return std::nullopt;
174 }
175
176private:
186 [[nodiscard]] std::any parse_value(std::string_view text) const override {
187 return detail::parse_value<T>(text);
188 }
189
198 void append_value(std::any& storage, std::string_view text) const override {
199 auto value = detail::parse_value<T>(text);
200 if (!storage.has_value()) {
201 storage = std::vector<T>();
202 }
203 std::any_cast<std::vector<T>&>(storage).push_back(std::move(value));
204 }
205
207 std::string value_name_;
208
210 std::optional<T> default_value_;
211};
212
213} // namespace kaycxx::cli
Defines and parses a command line interface.
Definition command.hpp:68
Common base class for typed options.
Definition option.hpp:27
virtual std::string const & value_name() const noexcept=0
Returns the value placeholder name.
bool is_repeatable() const noexcept
Checks whether this option accepts multiple occurrences.
Definition option.hpp:50
virtual std::optional< std::any > default_value() const =0
Returns the configured default value.
Handle for a registered option.
Definition option_handle.hpp:25
Typed command line option definition.
Definition option.hpp:93
std::string const & value_name() const noexcept override
Returns the value placeholder name.
Definition option.hpp:123
bool expects_value() const noexcept override
Checks whether this option expects a value.
Definition option.hpp:154
std::string usage() const override
Returns generated help usage for this option.
Definition option.hpp:141
std::optional< std::any > default_value() const override
Returns the configured default value.
Definition option.hpp:163
option(std::string_view name, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Creates an option without a short alias.
Definition option.hpp:102
void default_value(T value)
Sets the default option value.
Definition option.hpp:132
option(std::string_view name, char alias, std::string_view value_name, std::optional< std::string_view > description=std::nullopt)
Creates an option with a short alias.
Definition option.hpp:114
Common base class for command line switches.
Definition switch_base.hpp:22
std::optional< std::string > const & description() const noexcept
Returns the switch description.
std::optional< char > alias() const noexcept
Returns the short switch alias.
switch_base(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Creates a switch without a short alias.
std::string const & name() const noexcept
Returns the long switch name.
Defines value parsing support used by typed options and positional parameters.
Defines the common base class for flags and options.