22namespace kaycxx::cli {
71 [[nodiscard]]
virtual std::any parse_value(std::string_view text)
const = 0;
81 virtual void append_value(std::any& storage, std::string_view text)
const = 0;
84 bool repeatable_ =
false;
92template <parseable_value T>
133 default_value_ = std::move(
value);
143 return std::format(
"-{}, --{} <{}>", *
alias,
name(), value_name_);
146 return std::format(
" --{} <{}>",
name(), value_name_);
164 if (default_value_) {
166 auto values = std::vector<T>();
167 values.push_back(*default_value_);
168 return std::any(std::move(
values));
170 return std::any(*default_value_);
186 [[
nodiscard]] std::any parse_value(std::string_view
text)
const override {
187 return detail::parse_value<T>(
text);
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>();
203 std::any_cast<std::vector<T>&>(storage).push_back(std::move(value));
207 std::string value_name_;
210 std::optional<T> default_value_;
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.