kaycxx-cli
C++ CLI library
Loading...
Searching...
No Matches
parameter.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 <cstddef>
13#include <format>
14#include <optional>
15#include <span>
16#include <string>
17#include <string_view>
18#include <utility>
19
20#include <kaycxx/cli/detail/to_string.hpp>
24
25namespace kaycxx::cli {
26
32template <parseable_value T>
33class parameter : public parameter_base {
34public:
41 explicit parameter(std::string_view name, std::optional<std::string_view> description = std::nullopt)
43 {}
44
53 default_value_text(detail::to_string(value));
54 default_value_ = std::move(value);
55 }
56
62 [[nodiscard]] std::string usage() const override {
63 return std::format("<{}>", name());
64 }
65
71 [[nodiscard]] std::size_t min_count() const noexcept override {
72 return default_value_ ? 0 : 1;
73 }
74
80 [[nodiscard]] std::size_t max_count() const noexcept override {
81 return 1;
82 }
83
93 [[nodiscard]] std::any parse_values(std::span<std::string_view const> values) const override {
94 if (values.empty()) {
95 if (default_value_) {
96 return *default_value_;
97 }
98
99 throw parse_error(std::format("Missing parameter <{}>", name()));
100 }
101
102 try {
103 return detail::parse_value<T>(values.front());
104 } catch (parse_error const& error) {
105 throw parse_error(std::format("{} for parameter <{}>", error.what(), name()));
106 }
107 }
108
109private:
111 std::optional<T> default_value_;
112};
113
114} // namespace kaycxx::cli
Handle for a registered option.
Definition option_handle.hpp:25
Common base class for positional parameter definitions.
Definition parameter_base.hpp:23
std::optional< std::string > const & description() const noexcept
Returns the parameter description.
std::optional< std::string > const & default_value_text() const noexcept
Returns the configured default value rendered for help output.
std::string const & name() const noexcept
Returns the parameter name.
Typed single positional parameter definition.
Definition parameter.hpp:33
void default_value(T value)
Sets the default parameter value.
Definition parameter.hpp:52
std::string usage() const override
Returns generated help usage for this parameter.
Definition parameter.hpp:62
std::size_t max_count() const noexcept override
Returns the maximum number of values consumed by this parameter.
Definition parameter.hpp:80
std::any parse_values(std::span< std::string_view const > values) const override
Parses this positional parameter.
Definition parameter.hpp:93
parameter(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Creates a positional parameter definition.
Definition parameter.hpp:41
std::size_t min_count() const noexcept override
Returns the minimum number of values consumed by this parameter.
Definition parameter.hpp:71
Error thrown when command line arguments cannot be parsed.
Definition parse_error.hpp:19
Defines the common base class for positional parameters.
Defines the command line parse error type.
Defines value parsing support used by typed options and positional parameters.