kaycxx-cli
C++ CLI library
Loading...
Searching...
No Matches
parameters.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 <limits>
15#include <optional>
16#include <span>
17#include <string>
18#include <string_view>
19#include <utility>
20#include <vector>
21
22#include <kaycxx/cli/detail/to_string.hpp>
26
27namespace kaycxx::cli {
28
37template <parseable_value T>
38class parameters : public parameter_base {
39public:
46 explicit parameters(std::string_view name, std::optional<std::string_view> description = std::nullopt)
48 {}
49
55 void min(std::size_t count) noexcept {
56 min_count_ = count;
57 }
58
64 void max(std::size_t count) noexcept {
65 max_count_ = count;
66 }
67
75 void default_values(std::vector<T> values) {
76 auto text = std::string{"["};
77 for (auto index = std::size_t{0}; index < values.size(); ++index) {
78 if (index > 0) {
79 text += ", ";
80 }
81 text += detail::to_string(values[index]);
82 }
83 text += "]";
84 default_value_text(std::move(text));
85 default_values_ = std::move(values);
86 }
87
93 [[nodiscard]] std::string usage() const override {
94 return std::format("<{}>...", name());
95 }
96
102 [[nodiscard]] std::size_t min_count() const noexcept override {
103 return default_values_ ? 0 : min_count_;
104 }
105
111 [[nodiscard]] std::size_t max_count() const noexcept override {
112 return max_count_;
113 }
114
124 [[nodiscard]] std::any parse_values(std::span<std::string_view const> values) const override {
125 if (values.empty() && default_values_) {
126 validate_count(default_values_->size());
127 return *default_values_;
128 }
129
130 validate_count(values.size());
131
132 auto result = std::vector<T>();
133 result.reserve(values.size());
134
135 for (auto value : values) {
136 try {
137 result.push_back(detail::parse_value<T>(value));
138 } catch (parse_error const& error) {
139 throw parse_error(std::format("{} for parameter <{}>", error.what(), name()));
140 }
141 }
142
143 return result;
144 }
145
146private:
154 void validate_count(std::size_t count) const {
155 if (count < min_count_) {
156 throw parse_error(std::format("Missing parameter <{}>", name()));
157 }
158 if (count > max_count_) {
159 throw parse_error(std::format("Too many values for parameter <{}>", name()));
160 }
161 }
162
164 std::size_t min_count_ = 0;
165
167 std::size_t max_count_ = std::numeric_limits<std::size_t>::max();
168
170 std::optional<std::vector<T>> default_values_;
171};
172
173} // 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 positional parameter list definition.
Definition parameters.hpp:38
std::any parse_values(std::span< std::string_view const > values) const override
Parses this positional parameter list.
Definition parameters.hpp:124
void min(std::size_t count) noexcept
Sets the minimum number of values consumed by this parameter list.
Definition parameters.hpp:55
std::string usage() const override
Returns generated help usage for this parameter list.
Definition parameters.hpp:93
std::size_t min_count() const noexcept override
Returns the minimum number of values consumed by this parameter list.
Definition parameters.hpp:102
void default_values(std::vector< T > values)
Sets default values for this parameter list.
Definition parameters.hpp:75
void max(std::size_t count) noexcept
Sets the maximum number of values consumed by this parameter list.
Definition parameters.hpp:64
parameters(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Creates a positional parameter list definition.
Definition parameters.hpp:46
std::size_t max_count() const noexcept override
Returns the maximum number of values consumed by this parameter list.
Definition parameters.hpp:111
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.