17struct positional_argument_test_fixture;
22namespace ap::argument {
28template <detail::c_argument_value_type T = std::
string>
49 return this->_name == other._name;
58 this->_help_msg = help_msg;
70 template <detail::c_range_of<value_type, detail::type_val
idator::convertible> CR>
72 requires(std::equality_comparable<value_type>)
74 for (
const auto& choice :
choices)
75 this->_choices.emplace_back(choice);
86 requires(std::equality_comparable<value_type>)
88 return this->choices<>(
choices);
98 template <action::detail::c_value_action_specifier AS, std::invocable<value_type&> F>
101 this->_value_actions.emplace_back(std::forward<callable_type>(
action));
106 friend class ::ap::argument_parser;
112 friend struct ::ap_testing::positional_argument_test_fixture;
116 using value_action_type =
125 const bool verbose, [[maybe_unused]]
const char flag_char
126 )
const noexcept override {
133 if (not this->_choices.empty())
141 [[nodiscard]]
bool is_required() const noexcept
override {
142 return this->_required;
146 [[nodiscard]]
bool bypass_required_enabled() const noexcept
override {
147 return this->_bypass_required;
154 bool mark_used()
override {
159 [[nodiscard]]
bool is_used() const noexcept
override {
160 return this->_value.has_value();
164 [[nodiscard]] std::size_t count() const noexcept
override {
165 return static_cast<std::size_t
>(this->_value.has_value());
174 bool set_value(
const std::string& str_value)
override {
175 if (this->_value.has_value())
176 throw parsing_failure::value_already_set(this->_name);
179 if (not (std::istringstream(str_value) >> value))
180 throw parsing_failure::invalid_value(this->_name, str_value);
182 if (not detail::is_valid_choice(value, this->_choices))
183 throw parsing_failure::invalid_choice(this->_name, str_value);
185 const auto apply_visitor = action::detail::apply_visitor<value_type>{value};
186 for (
const auto& action : this->_value_actions)
187 std::visit(apply_visitor,
action);
189 this->_value = value;
194 [[nodiscard]]
bool has_value() const noexcept
override {
195 return this->_value.has_value();
199 [[nodiscard]]
bool has_parsed_values() const noexcept
override {
200 return this->has_value();
204 [[nodiscard]] std::weak_ordering nvalues_ordering() const noexcept
override {
205 return this->_value.has_value() ? std::weak_ordering::equivalent : std::weak_ordering::less;
212 [[nodiscard]]
const std::any& value()
const override {
213 if (not this->_value.has_value())
214 throw std::logic_error(
215 std::format(
"No value parsed for the `{}` positional argument.", this->_name.
str())
224 [[nodiscard]]
const std::vector<std::any>& values()
const override {
225 throw std::logic_error(
226 std::format(
"Positional argument `{}` has only 1 value.", this->_name.
primary)
230 static constexpr bool _required =
true;
231 static constexpr bool _bypass_required =
false;
232 std::vector<value_type> _choices;
233 std::vector<value_action_type> _value_actions;
Defines the base argument class and common utility.
The positional argument class.
bool operator==(const positional &other) const noexcept
Equality operator for positional argument.
positional & action(F &&action) noexcept
Set the action for the positional argument.
positional & choices(const CR &choices) noexcept
Set the choices for the positional argument.
positional & help(std::string_view help_msg) noexcept
Set the help message for the positional argument.
T value_type
The argument's value type.
positional & choices(std::initializer_list< value_type > choices) noexcept
Set the choices for the positional argument.
positional(const detail::argument_name &name)
Constructor for positional argument with the name identifier.
Argument class interface.
const ap::detail::argument_name & name() const noexcept
A structure used to represent an argument's description.
void add_range_param(const std::string &name, const R &range, const std::string_view delimiter=default_delimiter)
Adds a range parameter descriptor with the given value.
The concept is satisfied when T overloads the std::ostream operator <<.
Provides the general concept definitions.
Structure holding the argument's name.
std::string str(const std::optional< char > flag_char=std::nullopt) const noexcept
Get a string representation of the argument_name.
const std::string primary
The primary name of the argument.
Defines general action-related utility.
typename AS::template type< T > callable_type
Template argument action callable type alias.
std::variant< callable_type< action_type::observe, T >, callable_type< action_type::transform, T >, callable_type< action_type::modify, T > > value_action_variant_type
Template argument action callabla variant type alias.