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
25
26namespace kaycxx::cli {
27
36template <parseable_value T>
37class parameters : public parameter_base {
38public:
45 explicit parameters(std::string_view name, std::optional<std::string_view> description = std::nullopt)
47 {}
48
54 void min(std::size_t count) noexcept {
55 min_count_ = count;
56 }
57
63 void max(std::size_t count) noexcept {
64 max_count_ = count;
65 }
66
72 void default_values(std::vector<T> values) {
73 default_values_ = std::move(values);
74 }
75
81 [[nodiscard]] std::string usage() const override {
82 return std::format("<{}>...", name());
83 }
84
90 [[nodiscard]] std::size_t min_count() const noexcept override {
91 return default_values_ ? 0 : min_count_;
92 }
93
99 [[nodiscard]] std::size_t max_count() const noexcept override {
100 return max_count_;
101 }
102
112 [[nodiscard]] std::any parse_values(std::span<std::string_view const> values) const override {
113 if (values.empty() && default_values_) {
114 validate_count(default_values_->size());
115 return *default_values_;
116 }
117
118 validate_count(values.size());
119
120 auto result = std::vector<T>();
121 result.reserve(values.size());
122
123 for (auto value : values) {
124 try {
125 result.push_back(detail::parse_value<T>(value));
126 } catch (parse_error const& error) {
127 throw parse_error(std::format("{} for parameter <{}>", error.what(), name()));
128 }
129 }
130
131 return result;
132 }
133
134private:
142 void validate_count(std::size_t count) const {
143 if (count < min_count_) {
144 throw parse_error(std::format("Missing parameter <{}>", name()));
145 }
146 if (count > max_count_) {
147 throw parse_error(std::format("Too many values for parameter <{}>", name()));
148 }
149 }
150
152 std::size_t min_count_ = 0;
153
155 std::size_t max_count_ = std::numeric_limits<std::size_t>::max();
156
158 std::optional<std::vector<T>> default_values_;
159};
160
161} // 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::string const & name() const noexcept
Returns the parameter name.
Typed positional parameter list definition.
Definition parameters.hpp:37
std::any parse_values(std::span< std::string_view const > values) const override
Parses this positional parameter list.
Definition parameters.hpp:112
void min(std::size_t count) noexcept
Sets the minimum number of values consumed by this parameter list.
Definition parameters.hpp:54
std::string usage() const override
Returns generated help usage for this parameter list.
Definition parameters.hpp:81
std::size_t min_count() const noexcept override
Returns the minimum number of values consumed by this parameter list.
Definition parameters.hpp:90
void default_values(std::vector< T > values)
Sets default values for this parameter list.
Definition parameters.hpp:72
void max(std::size_t count) noexcept
Sets the maximum number of values consumed by this parameter list.
Definition parameters.hpp:63
parameters(std::string_view name, std::optional< std::string_view > description=std::nullopt)
Creates a positional parameter list definition.
Definition parameters.hpp:45
std::size_t max_count() const noexcept override
Returns the maximum number of values consumed by this parameter list.
Definition parameters.hpp:99
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.