20#include <kaycxx/cli/detail/to_string.hpp>
24namespace kaycxx::cli {
26template <parseable_value T>
29template <parseable_value T>
30class repeatable_option_handle;
59 return default_value_text_;
88 throw std::invalid_argument{std::format(
"Option --{} cannot be both required and defaulted",
name())};
90 default_value_text_ = std::move(
text);
96 template <parseable_value T>
99 template <parseable_value T>
107 void mark_as_required() {
108 if (default_value_text_) {
109 throw std::invalid_argument{std::format(
"Option --{} cannot be both required and defaulted",
name())};
115 void mark_as_repeatable() noexcept {
128 [[nodiscard]]
virtual std::any parse_value(std::string_view text)
const = 0;
138 virtual void append_value(std::any& storage, std::string_view text)
const = 0;
141 bool repeatable_ =
false;
144 bool required_ =
false;
147 std::optional<std::string> default_value_text_;
155template <parseable_value T>
201 default_value_ = std::move(
value);
211 return std::format(
"-{}, --{} <{}>", *
alias,
name(), value_name_);
214 return std::format(
" --{} <{}>",
name(), value_name_);
232 if (default_value_) {
234 auto values = std::vector<T>();
235 values.push_back(*default_value_);
236 return std::any(std::move(
values));
238 return std::any(*default_value_);
254 [[
nodiscard]] std::any parse_value(std::string_view
text)
const override {
255 return detail::parse_value<T>(
text);
266 void append_value(std::any& storage, std::string_view text)
const override {
267 auto value = detail::parse_value<T>(text);
268 if (!storage.has_value()) {
269 storage = std::vector<T>();
271 std::any_cast<std::vector<T>&>(storage).push_back(std::move(value));
275 std::string value_name_;
278 std::optional<T> default_value_;
Common base class for an application and its commands.
Definition command.hpp:45
Common base class for typed options.
Definition option.hpp:35
virtual std::string const & value_name() const noexcept=0
Returns the value placeholder name.
bool is_required() const noexcept
Checks whether this option must be specified explicitly.
Definition option.hpp:67
void mark_as_defaulted(std::string text)
Marks this option as having a default value.
Definition option.hpp:86
bool is_repeatable() const noexcept
Checks whether this option accepts multiple occurrences.
Definition option.hpp:76
std::optional< std::string > const & default_value_text() const noexcept
Returns the configured default value rendered for help output.
Definition option.hpp:58
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:156
std::string const & value_name() const noexcept override
Returns the value placeholder name.
Definition option.hpp:186
bool expects_value() const noexcept override
Checks whether this option expects a value.
Definition option.hpp:222
std::string usage() const override
Returns generated help usage for this option.
Definition option.hpp:209
std::optional< std::any > default_value() const override
Returns the configured default value.
Definition option.hpp:231
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:165
void default_value(T value)
Sets the default option value.
Definition option.hpp:199
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:177
Handle for a registered repeatable option.
Definition repeatable_option_handle.hpp:25
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.