From 57e335670cc9e9b45637216c480c96b59430ee22 Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Sun, 18 Oct 2026 23:34:25 +0200 Subject: [PATCH 1/8] argument_name alignment + initial argument attr setter --- include/argon/argument.hpp | 15 ++++- include/argon/argument_name.hpp | 110 +++++++++++++++++++------------- 2 files changed, 81 insertions(+), 44 deletions(-) diff --git a/include/argon/argument.hpp b/include/argon/argument.hpp index fcc6b702..9a6c17b5 100644 --- a/include/argon/argument.hpp +++ b/include/argon/argument.hpp @@ -133,6 +133,19 @@ class argument : public typed_argument_base { // attribute setters + /** + * @brief Set the negative prefix for the argument. + * @param prefix The negative prefix string (e.g., "no-", "disable-"). + * @return Reference to the argument instance. + * @note The method is enabled only for optional arguments. + */ + argument& negative(std::string_view prefix) + requires(type == argument_type::optional) + { + this->_name.negative_prefix(prefix); + return *this; + } + /** * @brief Set the help message for the argument. * @param help_msg The help message to set. @@ -768,7 +781,7 @@ class argument : public typed_argument_base { } // attributes - const argon::argument_name _name; ///< The argument's name. + argument_name _name; ///< The argument's name. std::string _help_msg; ///< The argument's help message. nargs::range _nargs_range; ///< The argument's nargs range attribute value. diff --git a/include/argon/argument_name.hpp b/include/argon/argument_name.hpp index 0a814ef7..b3f2a27d 100644 --- a/include/argon/argument_name.hpp +++ b/include/argon/argument_name.hpp @@ -20,9 +20,10 @@ class argument_name { public: /// @brief Specifies the type of argument name match. enum class match_type : std::uint8_t { - m_any, ///< Matches either the primary or the secondary name. + m_any, ///< Matches either the primary, secondary, or negative name. m_primary, ///< Matches only the primary name. - m_secondary ///< Matches only the secondary name. + m_secondary, ///< Matches only the secondary name. + m_negative ///< Matches only the negative primary name. }; using enum match_type; @@ -34,12 +35,10 @@ class argument_name { argument_name& operator=(const argument_name&) = default; argument_name& operator=(argument_name&&) = default; - /** - * @brief Primary and secondary name constructor. - * @param primary The primary name of the argument. - * @param secondary The secondary (short) name of the argument. - * @param flag_char The flag character (used for optional argument names). - */ + /// @brief Primary and secondary name constructor. + /// @param primary The primary name of the argument. + /// @param secondary The secondary (short) name of the argument. + /// @param flag_char The flag character (used for optional argument names). explicit argument_name( std::string primary, std::string secondary = "", char flag_char = flag_char_sentinel ) @@ -52,13 +51,12 @@ class argument_name { /// @brief Class destructor. ~argument_name() = default; - /** - * @brief Equality comparison operator. - * @param other The argument_name instance to compare with. - * @return Equality of argument names. - */ + /// @brief Equality comparison operator. + /// @param other The argument_name instance to compare with. + /// @return Equality of argument names. bool operator==(const argument_name& other) const noexcept { - return this->_primary == other._primary and this->_secondary == other._secondary; + return this->_primary == other._primary and this->_secondary == other._secondary + and this->_negative_prefix == other._negative_prefix; } /// @brief Checks if the argument name instance has a primary name. @@ -71,6 +69,11 @@ class argument_name { return not this->_secondary.empty(); } + /// @brief Checks if the argument name instance has a negative prefix. + [[nodiscard]] bool has_negative() const noexcept { + return not this->_negative_prefix.empty(); + } + /// @return The primary name of the argument. [[nodiscard]] const std::string& primary() const noexcept { return this->_primary; @@ -81,70 +84,93 @@ class argument_name { return this->_secondary; } + /// @return The negative prefix of the argument. + [[nodiscard]] const std::string& negative_prefix() const noexcept { + return this->_negative_prefix; + } + /// @return The flag character (used for optional argument names). [[nodiscard]] char flag_char() const noexcept { return this->_flag_char; } - /** - * @brief Matches the given string to the argument_name instance. - * @param arg_name The name string to match. - * @param m_type The match type used to find the argument. - * @return `true` if the given name matches the primary/secondary name (depending on the match type). - */ + /// @brief Sets the negative prefix for the argument name. + /// @param prefix The negative prefix string. + /// @return Reference to the argument name instance. + /// @throws std::logic_error if the argument does not have a primary name. + argument_name& negative_prefix(std::string_view prefix) { + if (not this->has_primary()) + throw std::logic_error("Cannot add a negative prefix to an argument without a primary " + "name!"); + this->_negative_prefix = prefix; + return *this; + } + + /// @brief Matches the given string to the argument_name instance. + /// @param arg_name The name string to match. + /// @param m_type The match type used to find the argument. + /// @return `true` if the given name matches the primary/secondary/negative name (depending on the match type). [[nodiscard]] bool match(std::string_view arg_name, const match_type m_type = m_any) const noexcept { switch (m_type) { case m_any: return (this->has_primary() and this->_primary == arg_name) + or (this->has_primary() and this->has_negative() + and (this->_negative_prefix + this->_primary) == arg_name) or (this->has_secondary() and this->_secondary == arg_name); case m_primary: return this->has_primary() and this->_primary == arg_name; case m_secondary: return this->has_secondary() and this->_secondary == arg_name; + case m_negative: + return this->has_primary() and this->has_negative() + and (this->_negative_prefix + this->_primary) == arg_name; } return false; } - /** - * @brief Matches the given argument name to the argument_name instance. - * @param arg_name The argument_name instance to match. - * @return True if arg_name's primary or secondary value matches the argument_name instance. - */ + /// @brief Matches the given argument name to the argument_name instance. + /// @param arg_name The argument_name instance to match. + /// @return True if arg_name's primary, secondary, or negative value matches the argument_name instance. [[nodiscard]] bool match(const argument_name& arg_name) const noexcept { if (arg_name.has_primary() and this->match(arg_name._primary)) return true; - if (arg_name.has_secondary()) - return this->match(arg_name._secondary); + if (arg_name.has_secondary() and this->match(arg_name._secondary)) + return true; + + if (arg_name.has_negative() and this->match(arg_name._negative_prefix + arg_name._primary)) + return true; return false; } - /** - * @brief Get a string representation of the argument_name. - */ + /// @brief Get a string representation of the argument_name. [[nodiscard]] std::string str() const noexcept { const std::string fc( static_cast(this->_flag_char != flag_char_sentinel), this->_flag_char ); std::string primary_str = - this->has_primary() ? std::format("{}{}{}", fc, fc, this->_primary) : ""; - std::string separator = this->has_primary() and this->has_secondary() ? ", " : ""; + this->has_primary() + ? (this->has_negative() + ? std::format("{}{}[{}]{}", fc, fc, this->_negative_prefix, this->_primary) + : std::format("{}{}{}", fc, fc, this->_primary)) + : ""; + + std::string separator = (this->has_primary() and this->has_secondary()) ? ", " : ""; + std::string secondary_str = this->has_secondary() ? std::format("{}{}", fc, this->_secondary) : ""; return std::format("{}{}{}", primary_str, separator, secondary_str); } - /** - * @brief Stream insertion operator for argument names. - * @param os The output stream. - * @param arg_name The argument name to be inserted into the stream. - * @return The modified output stream. - */ + /// @brief Stream insertion operator for argument names. + /// @param os The output stream. + /// @param arg_name The argument name to be inserted into the stream. + /// @return The modified output stream. friend std::ostream& operator<<(std::ostream& os, const argument_name& arg_name) noexcept { os << arg_name.str(); return os; @@ -156,14 +182,12 @@ class argument_name { private: std::string _primary; ///< The primary name of the argument. std::string _secondary; ///< The optional (short) name of the argument. + std::string _negative_prefix; ///< The optional negative prefix for the primary name. char _flag_char; ///< The flag character (used for optional argument names). }; -/** - * @brief Argument name member discriminator. - * - * This discriminator type is used in the argument initializing methods of the `argument_parser` class. - */ +/// @brief Argument name member discriminator. +/// This discriminator type is used in the argument initializing methods of the `argument_parser` class. enum class argument_name_discriminator : bool { n_primary, ///< Represents the primary name (used with a long flag prefix --). n_secondary ///< Represents the secondary name (used with a short flag prefix --). From c32889e63364b108e2074dfbd10bf6701096de2c Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Mon, 19 Oct 2026 14:06:13 +0200 Subject: [PATCH 2/8] initial negative flag behaviour impl --- include/argon/action/predefined.hpp | 6 ++ include/argon/action/traits.hpp | 10 +++ include/argon/action/types.hpp | 6 ++ include/argon/argument.hpp | 85 +++++++++++++++++++++---- include/argon/argument_base.hpp | 4 ++ include/argon/argument_name.hpp | 2 +- include/argon/argument_parser.hpp | 38 ++++++++--- include/argon/detail/argument_token.hpp | 7 +- 8 files changed, 133 insertions(+), 25 deletions(-) diff --git a/include/argon/action/predefined.hpp b/include/argon/action/predefined.hpp index d284adb4..27e7da0f 100644 --- a/include/argon/action/predefined.hpp +++ b/include/argon/action/predefined.hpp @@ -19,6 +19,12 @@ std::ostream& operator<<(std::ostream& os, const argument_parser&) noexcept; namespace action { +/// @brief A predefined negative flag action tag that clears only the parsed values. +inline constexpr action_type::reset_parsed_t reset_parsed{}; + +/// @brief A predefined negative flag action tag that clears all parsed, default, and implicit values. +inline constexpr action_type::reset_all_t reset_all{}; + /** * @brief Returns an *on-flag* action which prints the argument parser's help message. * @param parser Argument parser instance the help message of which will be printed. diff --git a/include/argon/action/traits.hpp b/include/argon/action/traits.hpp index bf58197b..2398e441 100644 --- a/include/argon/action/traits.hpp +++ b/include/argon/action/traits.hpp @@ -41,4 +41,14 @@ concept c_flag_action_specifier = argon::traits::c_one_of concept c_action_specifier = c_value_action_specifier or std::same_as; +/// @brief Concept validating if a type is a predefined negative action tag. +template +concept c_negative_action_specifier = + c_one_of; + +/// @brief Concept validating if a type is a valid negative action (a specifier tag or an invocable callable). +template +concept c_negative_action = + c_negative_action_specifier> or std::invocable; + } // namespace argon::traits diff --git a/include/argon/action/types.hpp b/include/argon/action/types.hpp index 71495fc2..70410188 100644 --- a/include/argon/action/types.hpp +++ b/include/argon/action/types.hpp @@ -60,4 +60,10 @@ struct on_flag { using type = std::function; }; +/// @brief Tag type used to reset an argument's parsed values. +struct reset_parsed_t {}; + +/// @brief Tag type used to clear an argument's parsed, default, and implicit values. +struct reset_all_t {}; + } // namespace argon::action_type diff --git a/include/argon/argument.hpp b/include/argon/argument.hpp index 9a6c17b5..4dae1167 100644 --- a/include/argon/argument.hpp +++ b/include/argon/argument.hpp @@ -133,19 +133,6 @@ class argument : public typed_argument_base { // attribute setters - /** - * @brief Set the negative prefix for the argument. - * @param prefix The negative prefix string (e.g., "no-", "disable-"). - * @return Reference to the argument instance. - * @note The method is enabled only for optional arguments. - */ - argument& negative(std::string_view prefix) - requires(type == argument_type::optional) - { - this->_name.negative_prefix(prefix); - return *this; - } - /** * @brief Set the help message for the argument. * @param help_msg The help message to set. @@ -300,6 +287,60 @@ class argument : public typed_argument_base { return *this; } + /** + * @brief Set the negative prefix and action for the argument. + * @param prefix The negative prefix string (e.g., "no-"). + * @param action A predefined tag (like `action::reset_all`) or a generic invocable callable. + * @return Reference to the argument instance. + * @note The method is enabled only for optional arguments. + */ + template + argument& negative(std::string_view prefix, F&& action) + requires(type == argument_type::optional) + { + this->_name.with_negative_prefix(prefix); + + using action_t = std::remove_cvref_t; + if constexpr (traits::c_negative_action_specifier) { + if constexpr (std::same_as) { + this->_negative_action = [this]() { this->_values.clear(); }; + } + else if constexpr (std::same_as) { + this->_negative_action = [this]() { + this->_values.clear(); + if constexpr (not traits::c_is_none) { + this->_default_values.clear(); + this->_implicit_values.clear(); + } + }; + } + } + else { + this->_negative_action = std::forward(action); + } + + return *this; + } + + /** + * @brief Set the negative prefix. Defaults to storing `false` for booleans, and clearing all values (the @ref reset_all action) for others. + * @param prefix The negative prefix string. + * @return Reference to the argument instance. + * @note The method is enabled only for optional arguments. + */ + argument& negative(std::string_view prefix) + requires(type == argument_type::optional) + { + if constexpr (std::same_as) { + this->_name.with_negative_prefix(prefix); + this->_negative_action = [this]() { this->_values.emplace_back(false); }; + return *this; + } + else { + return this->negative(prefix, action::reset_all); + } + } + /** * @brief Add the choices for the argument. * @tparam CR The choices range type. @@ -517,6 +558,9 @@ class argument : public typed_argument_base { /// @brief The argument's flag action type alias. using flag_action_type = typename action_type::on_flag::type; + /// @brief The argument's negative action type alias. + using negative_action_type = std::function; + /// @brief The argument's value-argument-specific type alias. /// @tparam _T The actual type used if the argument's `value_type` is not `none_type`. template @@ -584,6 +628,19 @@ class argument : public typed_argument_base { return this->_accepts_further_values(); } + /// @brief Mark the optional argument's negative flag as used. + /// @return `true` if the argument accepts further values, `false` otherwise. + bool mark_negative_used() override { + if constexpr (type == argument_type::optional) { + ++this->_count; + if (this->_negative_action) + this->_negative_action(); + } + + // Negative flags are terminal and do not consume subsequent command-line values + return false; + } + /** * @brief Set the value for the optional argument. * @param str_value The string value to use. @@ -795,6 +852,8 @@ class argument : public typed_argument_base { _flag_actions; ///< The optional argument's flag actions collection. [[no_unique_address]] value_arg_specific_type> _value_actions; ///< The argument's value actions collection. + [[no_unique_address]] optional_specific_type + _negative_action; ///< The action executed when the negative flag is used. bool _required : 1; ///< The argument's `required` attribute value. bool _suppress_arg_checks : 1 = diff --git a/include/argon/argument_base.hpp b/include/argon/argument_base.hpp index 50c797fe..d352f626 100644 --- a/include/argon/argument_base.hpp +++ b/include/argon/argument_base.hpp @@ -83,6 +83,10 @@ class argument_base { /// @return `true` if the argument accepts further values, `false` otherwise. virtual bool mark_used() = 0; + /// @brief Mark the argument's negative flag as used. + /// @return `true` if the argument accepts further values, `false` otherwise. + virtual bool mark_negative_used() = 0; + /// @brief Set the value for the argument. /// @param value The string representation of the value. /// @return `true` if the argument accepts further values, `false` otherwise. diff --git a/include/argon/argument_name.hpp b/include/argon/argument_name.hpp index b3f2a27d..a32dd8d2 100644 --- a/include/argon/argument_name.hpp +++ b/include/argon/argument_name.hpp @@ -98,7 +98,7 @@ class argument_name { /// @param prefix The negative prefix string. /// @return Reference to the argument name instance. /// @throws std::logic_error if the argument does not have a primary name. - argument_name& negative_prefix(std::string_view prefix) { + argument_name& with_negative_prefix(std::string_view prefix) { if (not this->has_primary()) throw std::logic_error("Cannot add a negative prefix to an argument without a primary " "name!"); diff --git a/include/argon/argument_parser.hpp b/include/argon/argument_parser.hpp index 546b8e40..70d0ea94 100644 --- a/include/argon/argument_parser.hpp +++ b/include/argon/argument_parser.hpp @@ -1391,19 +1391,36 @@ class argument_parser { return false; } - [[nodiscard]] arg_ptr_vec_iter_t _find_opt_arg(const detail::argument_token& flag_tok - ) noexcept { + [[nodiscard]] arg_ptr_vec_iter_t _find_opt_arg(detail::argument_token& flag_tok) noexcept { if (not flag_tok.is_flag_token()) return this->_optional_args.end(); const auto actual_tok_value = this->_strip_flag_prefix(flag_tok); - const auto match_type = - flag_tok.type == detail::argument_token::t_flag_primary - ? argument_name::m_primary - : argument_name::m_secondary; + if (flag_tok.type == detail::argument_token::t_flag_primary) { + auto it = std::ranges::find_if( + this->_optional_args, + this->_name_match_predicate(actual_tok_value, argument_name::m_primary) + ); + if (it != this->_optional_args.end()) + return it; + + it = std::ranges::find_if( + this->_optional_args, + this->_name_match_predicate(actual_tok_value, argument_name::m_negative) + ); + if (it != this->_optional_args.end()) { + flag_tok.type = detail::argument_token::t_flag_negative; + return it; + } + + return this->_optional_args.end(); + } + + // Secondary and compound flags cannot have a negative prefix return std::ranges::find_if( - this->_optional_args, this->_name_match_predicate(actual_tok_value, match_type) + this->_optional_args, + this->_name_match_predicate(actual_tok_value, argument_name::m_secondary) ); } @@ -1445,7 +1462,12 @@ class argument_parser { } for (const auto& arg : tok.args) { - if (arg->mark_used()) + const bool accepts_values = + (tok.type == detail::argument_token::t_flag_negative) + ? arg->mark_negative_used() + : arg->mark_used(); + + if (accepts_values) state.curr_arg = arg; else state.curr_arg.reset(); diff --git a/include/argon/detail/argument_token.hpp b/include/argon/detail/argument_token.hpp index 5894b450..5611626c 100644 --- a/include/argon/detail/argument_token.hpp +++ b/include/argon/detail/argument_token.hpp @@ -25,7 +25,8 @@ struct argument_token { t_value, ///< Represents a value argument. t_flag_primary, ///< Represents a primary (--) flag argument. t_flag_secondary, ///< Represents a secondary (-) flag argument. - t_flag_compound ///< Represents a compound flag argument (secondary flag matching multiple arguments). + t_flag_compound, ///< Represents a compound flag argument (secondary flag matching multiple arguments). + t_flag_negative ///< Represents a primary flag argument matching a negative prefix. }; using enum token_type; @@ -45,14 +46,14 @@ struct argument_token { * @note - t_flag_primary * @note - t_flag_secondary * @note - t_flag_compound + * @note - t_flag_negative */ [[nodiscard]] bool is_flag_token() const noexcept { switch (this->type) { case t_flag_primary: - [[fallthrough]]; case t_flag_secondary: - [[fallthrough]]; case t_flag_compound: + case t_flag_negative: return true; default: return false; From 96844ec30744c2314bdd800c08810f1250913d70 Mon Sep 17 00:00:00 2001 From: SpectraL519 Date: Mon, 19 Oct 2026 23:28:00 +0200 Subject: [PATCH 3/8] wip: refactor --- include/argon/action/predefined.hpp | 23 ++- include/argon/action/traits.hpp | 16 ++- include/argon/action/types.hpp | 14 +- include/argon/argument.hpp | 167 +++++++++++++++------- include/argon/argument_parser.hpp | 48 ++++--- include/argon/traits.hpp | 27 ++++ tests/include/argument_test_fixture.hpp | 2 + tests/source/test_optional_argument.cpp | 8 +- tests/source/test_positional_argument.cpp | 6 +- 9 files changed, 219 insertions(+), 92 deletions(-) diff --git a/include/argon/action/predefined.hpp b/include/argon/action/predefined.hpp index 27e7da0f..b87d4c1b 100644 --- a/include/argon/action/predefined.hpp +++ b/include/argon/action/predefined.hpp @@ -25,6 +25,16 @@ inline constexpr action_type::reset_parsed_t reset_parsed{}; /// @brief A predefined negative flag action tag that clears all parsed, default, and implicit values. inline constexpr action_type::reset_all_t reset_all{}; +/** + * @brief A factory function returning a negative action tag that injects a specific value. + * @param val The value to store when the negative flag is used. + * @todo Make variadic + */ +template +inline action_type::store_t> store(V&& val) noexcept { + return {std::forward(val)}; +} + /** * @brief Returns an *on-flag* action which prints the argument parser's help message. * @param parser Argument parser instance the help message of which will be printed. @@ -44,7 +54,7 @@ inline typename argon::action_type::on_flag::type print_help( } /// @brief Returns an *observe* action which checks whether lower_bound file with the given name exists. -inline util::callable_type check_file_exists() noexcept { +inline util::callable_type check_file_exists() noexcept { return [](const std::string& file_path) { if (not std::filesystem::exists(file_path)) throw std::filesystem::filesystem_error( @@ -61,7 +71,7 @@ inline util::callable_type check_file_ * @param lower_bound The exclusive lower bound to validate against. */ template -util::callable_type gt(const T lower_bound) noexcept { +util::callable_type gt(const T lower_bound) noexcept { return [lower_bound](const T& value) { if (not (value > lower_bound)) throw std::out_of_range( @@ -76,7 +86,7 @@ util::callable_type gt(const T lower_bound) noex * @param lower_bound The inclusive lower bound to validate against. */ template -util::callable_type geq(const T lower_bound) noexcept { +util::callable_type geq(const T lower_bound) noexcept { return [lower_bound](const T& value) { if (! (value >= lower_bound)) throw std::out_of_range( @@ -91,7 +101,7 @@ util::callable_type geq(const T lower_bound) noe * @param lower_bound The exclusive upper bound to validate against. */ template -util::callable_type lt(const T upper_bound) noexcept { +util::callable_type lt(const T upper_bound) noexcept { return [upper_bound](const T& value) { if (! (value < upper_bound)) throw std::out_of_range( @@ -106,7 +116,7 @@ util::callable_type lt(const T upper_bound) noex * @param lower_bound The inclusive upper bound to validate against. */ template -util::callable_type leq(const T upper_bound) noexcept { +util::callable_type leq(const T upper_bound) noexcept { return [upper_bound](const T& value) { if (! (value <= upper_bound)) throw std::out_of_range( @@ -125,8 +135,9 @@ util::callable_type leq(const T upper_bound) noe * @tparam RightInclusive Whether the upper bound is inclusive (`]`) or exclusive (`)`) - default: true. * @param lower_bound The lower bound of the interval. * @param upper_bound The upper bound of the interval. + * @todo Add explicit inclusive/exclusive constants (perhaps in argon::traits) */ -template +template util::callable_type within( const T lower_bound, const T upper_bound ) noexcept { diff --git a/include/argon/action/traits.hpp b/include/argon/action/traits.hpp index 2398e441..e3c5aac6 100644 --- a/include/argon/action/traits.hpp +++ b/include/argon/action/traits.hpp @@ -41,14 +41,18 @@ concept c_flag_action_specifier = argon::traits::c_one_of concept c_action_specifier = c_value_action_specifier or std::same_as; -/// @brief Concept validating if a type is a predefined negative action tag. +/** + * @brief Concept validating if a type is a predefined state action tag. + */ template -concept c_negative_action_specifier = - c_one_of; +concept c_state_action_specifier = + c_one_of + or c_instantiation_of; -/// @brief Concept validating if a type is a valid negative action (a specifier tag or an invocable callable). +/** + * @brief Concept validating if a type is a valid state action (a specifier tag or an invocable callable). + */ template -concept c_negative_action = - c_negative_action_specifier> or std::invocable; +concept c_state_action = c_state_action_specifier> or std::invocable; } // namespace argon::traits diff --git a/include/argon/action/types.hpp b/include/argon/action/types.hpp index 70410188..618c5a4c 100644 --- a/include/argon/action/types.hpp +++ b/include/argon/action/types.hpp @@ -13,6 +13,7 @@ #include +// ??? rename to argon::action ??? namespace argon::action_type { /** @@ -22,7 +23,7 @@ namespace argon::action_type { * performs some logic on it without modifying it. */ struct observe { - template + template using type = std::function; }; @@ -33,7 +34,7 @@ struct observe { * returns a new value with which the argument will be initialized. */ struct transform { - template + template using type = std::function; }; @@ -44,7 +45,7 @@ struct transform { * already initialized argument. */ struct modify { - template + template using type = std::function; }; @@ -66,4 +67,11 @@ struct reset_parsed_t {}; /// @brief Tag type used to clear an argument's parsed, default, and implicit values. struct reset_all_t {}; +/// @brief A type used to store/insert values for an argument by an argument action. +/// @todo Allow multiple values. +template +struct store_t { + V value; +}; + } // namespace argon::action_type diff --git a/include/argon/argument.hpp b/include/argon/argument.hpp index 4dae1167..ef1bb208 100644 --- a/include/argon/argument.hpp +++ b/include/argon/argument.hpp @@ -253,6 +253,7 @@ class argument : public typed_argument_base { } /** + * @todo align doc * @brief Set the *value* action for the argument. * @tparam AS The action specifier type (see @ref argon/action/type.hpp). * @tparam F The type of the action function. @@ -262,16 +263,19 @@ class argument : public typed_argument_base { * @note - `value_type` is not `none_type`. * @note - `AS` is a valid value action specifier: `action_type::observe`, `action_type::transform`, `action_type::modify`. */ - template - argument& action(F&& action) noexcept + template + argument& actions(Fs&&... value_actions) noexcept requires(not traits::c_is_none) { using callable_type = action::util::callable_type; - this->_value_actions.emplace_back(std::forward(action)); + (this->_value_actions.emplace_back(static_cast(std::forward(value_actions + ))), + ...); return *this; } /** + * @todo align doc * @brief Set the *on-flag* action for the argument. * @tparam AS The action specifier type (see @ref argon/action/types.hpp). * @tparam F The type of the action function. @@ -279,68 +283,54 @@ class argument : public typed_argument_base { * @return Reference to the argument instance. * @note The method is enabled only for optional arguments and if `AS` is `action_type::on_flag`. */ - template - argument& action(F&& action) noexcept + template + argument& actions(Fs&&... state_actions) requires(type == argument_type::optional) { - this->_flag_actions.emplace_back(std::forward(action)); + (this->_flag_actions.emplace_back(this->_compile_state_action(std::forward(state_actions + ))), + ...); return *this; } /** * @brief Set the negative prefix and action for the argument. * @param prefix The negative prefix string (e.g., "no-"). - * @param action A predefined tag (like `action::reset_all`) or a generic invocable callable. - * @return Reference to the argument instance. + * @param actions A variadic list of predefined tags (like `action::reset_parsed`) or generic invocable callables. + * If left empty, sensible defaults are applied. + * @note Defaults to storing `false` for booleans, and clearing all values (the @ref reset_all action) for others. * @note The method is enabled only for optional arguments. + * @return Reference to the argument instance. */ - template - argument& negative(std::string_view prefix, F&& action) + template + argument& negative(std::string_view prefix, Actions&&... actions_) requires(type == argument_type::optional) { this->_name.with_negative_prefix(prefix); - using action_t = std::remove_cvref_t; - if constexpr (traits::c_negative_action_specifier) { - if constexpr (std::same_as) { - this->_negative_action = [this]() { this->_values.clear(); }; + if constexpr (sizeof...(Actions) == 0) { + if constexpr (std::same_as) { + this->_negative_actions.emplace_back( + this->_compile_state_action(action::reset_parsed) + ); + this->_negative_actions.emplace_back(this->_compile_state_action(action::store(false + ))); } - else if constexpr (std::same_as) { - this->_negative_action = [this]() { - this->_values.clear(); - if constexpr (not traits::c_is_none) { - this->_default_values.clear(); - this->_implicit_values.clear(); - } - }; + else { + this->_negative_actions.emplace_back(this->_compile_state_action(action::reset_all) + ); } } else { - this->_negative_action = std::forward(action); + (this->_negative_actions.emplace_back( + this->_compile_state_action(std::forward(actions_)) + ), + ...); } return *this; } - /** - * @brief Set the negative prefix. Defaults to storing `false` for booleans, and clearing all values (the @ref reset_all action) for others. - * @param prefix The negative prefix string. - * @return Reference to the argument instance. - * @note The method is enabled only for optional arguments. - */ - argument& negative(std::string_view prefix) - requires(type == argument_type::optional) - { - if constexpr (std::same_as) { - this->_name.with_negative_prefix(prefix); - this->_negative_action = [this]() { this->_values.emplace_back(false); }; - return *this; - } - else { - return this->negative(prefix, action::reset_all); - } - } - /** * @brief Add the choices for the argument. * @tparam CR The choices range type. @@ -558,8 +548,8 @@ class argument : public typed_argument_base { /// @brief The argument's flag action type alias. using flag_action_type = typename action_type::on_flag::type; - /// @brief The argument's negative action type alias. - using negative_action_type = std::function; + /// @brief The argument's state action type alias. + using state_action_type = std::function; /// @brief The argument's value-argument-specific type alias. /// @tparam _T The actual type used if the argument's `value_type` is not `none_type`. @@ -616,6 +606,8 @@ class argument : public typed_argument_base { return bld; } + // --- parsing --- + /// @brief Mark the optional argument as used. /// @return `true` if the argument accepts further values, `false` otherwise. bool mark_used() override { @@ -633,12 +625,11 @@ class argument : public typed_argument_base { bool mark_negative_used() override { if constexpr (type == argument_type::optional) { ++this->_count; - if (this->_negative_action) - this->_negative_action(); + for (const auto& action : this->_negative_actions) + action(); } - // Negative flags are terminal and do not consume subsequent command-line values - return false; + return false; // Negative flags are terminal } /** @@ -653,10 +644,10 @@ class argument : public typed_argument_base { /// @return The ordering relationship of the argument's values and its nargs range attribute. [[nodiscard]] std::weak_ordering nvalues_ordering() const noexcept override { - if (this->_values.empty() and this->_has_predefined_values_impl()) + if (this->_parsed_nvalues == 0ull and this->_has_predefined_values_impl()) return std::weak_ordering::equivalent; - return this->_values.size() <=> this->_nargs_range; + return this->_parsed_nvalues <=> this->_nargs_range; } /// @return Reference to the vector of parsed values for the argument. @@ -834,9 +825,79 @@ class argument : public typed_argument_base { std::visit(apply_visitor, action); this->_values.emplace_back(std::move(value)); + ++this->_parsed_nvalues; // Mark an explicitly parsed value + return this->_accepts_further_values(); } + // --- configuration --- + + template + state_action_type _compile_state_action(F&& action) { + using action_t = std::remove_cvref_t; + + if constexpr (traits::c_state_action_specifier) { + if constexpr (std::same_as) { + return [this]() { + this->_values.clear(); + this->_parsed_nvalues = 0ull; + }; + } + else if constexpr (std::same_as) { + return [this]() { + this->_values.clear(); + this->_parsed_nvalues = 0ull; + if constexpr (not traits::c_is_none) { + this->_default_values.clear(); + this->_implicit_values.clear(); + } + }; + } + else if constexpr (traits::c_instantiation_of) { + return [this, val = action.value]() { + if constexpr (not traits::c_is_none) { + this->_values.emplace_back(val); + } + }; + } + } + else { + return std::forward(action); // User provided a raw callable + } + } + + template + void _add_negative_action(F&& action) { + using action_t = std::remove_cvref_t; + + if constexpr (traits::c_state_action_specifier) { + if constexpr (std::same_as) { + this->_negative_actions.emplace_back([this]() { + this->_values.clear(); + this->_parsed_nvalues = 0ull; + }); + } + else if constexpr (std::same_as) { + this->_negative_actions.emplace_back([this]() { + this->_values.clear(); + this->_parsed_nvalues = 0ull; + if constexpr (not traits::c_is_none) { + this->_default_values.clear(); + this->_implicit_values.clear(); + } + }); + } + else if constexpr (traits::c_instantiation_of) { + this->_negative_actions.emplace_back([this, val = action.value]() { + this->_values.emplace_back(val); + }); + } + } + else { + this->_negative_actions.emplace_back(std::forward(action)); + } + } + // attributes argument_name _name; ///< The argument's name. std::string _help_msg; ///< The argument's help message. @@ -852,8 +913,8 @@ class argument : public typed_argument_base { _flag_actions; ///< The optional argument's flag actions collection. [[no_unique_address]] value_arg_specific_type> _value_actions; ///< The argument's value actions collection. - [[no_unique_address]] optional_specific_type - _negative_action; ///< The action executed when the negative flag is used. + [[no_unique_address]] optional_specific_type> + _negative_actions; ///< The action executed when the negative flag is used. bool _required : 1; ///< The argument's `required` attribute value. bool _suppress_arg_checks : 1 = @@ -866,6 +927,8 @@ class argument : public typed_argument_base { // parsing result [[no_unique_address]] optional_specific_type _count; ///< The argument's value count. + [[no_unique_address]] std::size_t _parsed_nvalues = + 0ull; ///< Tracks ONLY the values consumed from the command line. std::vector _values; ///< The argument's parsed values. // default attribute values diff --git a/include/argon/argument_parser.hpp b/include/argon/argument_parser.hpp index 70d0ea94..2a75e46e 100644 --- a/include/argon/argument_parser.hpp +++ b/include/argon/argument_parser.hpp @@ -39,7 +39,7 @@ enum class default_argument : std::uint8_t { * Equivalent to: * @code{.cpp} * parser.add_positional_argument("input") - * .action(argon::action::check_file_exists()) + * .actiions(argon::action::check_file_exists()) * .help("Input file path"); * @endcode */ @@ -60,7 +60,7 @@ enum class default_argument : std::uint8_t { * Equivalent to: * @code{.cpp} * parser.add_optional_argument("help") - * .action(argon::action::print_help(parser, EXIT_SUCCESS)) + * .actiions(argon::action::print_help(parser, EXIT_SUCCESS)) * .help("Display the help message"); * @endcode */ @@ -71,7 +71,7 @@ enum class default_argument : std::uint8_t { * Equivalent to: * @code{.cpp} * arg_parser.add_optional_argument("version", "v") - * .action([&arg_parser]() { + * .actiions([&arg_parser]() { * arg_parser.print_version(); * std::exit(EXIT_SUCCESS); * }) @@ -86,7 +86,7 @@ enum class default_argument : std::uint8_t { * @code{.cpp} * parser.add_positional_argument("input", "i") * .nargs(1ull) - * .action(argon::action::check_file_exists()) + * .actiions(argon::action::check_file_exists()) * .help("Input file path"); * @endcode */ @@ -109,7 +109,7 @@ enum class default_argument : std::uint8_t { * @code{.cpp} * parser.add_positional_argument("input", "i") * .nargs(argon::nargs::at_least(1ull)) - * .action(argon::action::check_file_exists()) + * .actiions(argon::action::check_file_exists()) * .help("Input file path"); * @endcode */ @@ -513,10 +513,13 @@ class argument_parser { optional_argument& add_flag( const std::string_view base_name, const argument_name_discriminator name_discr = n_primary ) { + // TODO: use public add_flag(group) overload, use .actions, allow .actions to accept the reset/store actions as well return this->add_optional_argument(base_name, name_discr) .default_values(not StoreImplicitly) - .implicit_values(StoreImplicitly) - .nargs(0ull); + .nargs(0ull) + .template actions( + action::reset_parsed, action::store(StoreImplicitly) + ); } /** @@ -531,10 +534,13 @@ class argument_parser { optional_argument& add_flag( const std::string_view base_primary_name, const std::string_view base_secondary_name ) { + // TODO: use public add_flag(group) overload, use .actions, allow .actions to accept the reset/store actions as well return this->add_optional_argument(base_primary_name, base_secondary_name) .default_values(not StoreImplicitly) - .implicit_values(StoreImplicitly) - .nargs(0ull); + .nargs(0ull) + .template actions( + action::reset_parsed, action::store(StoreImplicitly) + ); } /** @@ -552,10 +558,13 @@ class argument_parser { const std::string_view base_name, const argument_name_discriminator name_discr = n_primary ) { + // TODO: allow .actions to accept the reset/store actions as well return this->add_optional_argument(group, base_name, name_discr) .default_values(not StoreImplicitly) - .implicit_values(StoreImplicitly) - .nargs(0ull); + .nargs(0ull) + .template actions( + action::reset_parsed, action::store(StoreImplicitly) + ); } /** @@ -573,10 +582,13 @@ class argument_parser { const std::string_view base_primary_name, const std::string_view base_secondary_name ) { + // TODO: allow .actions to accept the reset/store actions as well return this->add_optional_argument(group, base_primary_name, base_secondary_name) .default_values(not StoreImplicitly) - .implicit_values(StoreImplicitly) - .nargs(0ull); + .nargs(0ull) + .template actions( + action::reset_parsed, action::store(StoreImplicitly) + ); } /** @@ -1754,7 +1766,7 @@ inline void add_default_argument( switch (arg_discriminator) { case default_argument::p_input: arg_parser.add_positional_argument("input") - .action(action::check_file_exists()) + .actions(action::check_file_exists()) .help("Input file path"); break; @@ -1764,13 +1776,13 @@ inline void add_default_argument( case default_argument::o_help: arg_parser.add_optional_argument("help", "h") - .action(action::print_help(arg_parser, EXIT_SUCCESS)) + .actions(action::print_help(arg_parser, EXIT_SUCCESS)) .help("Display the help message"); break; case default_argument::o_version: arg_parser.add_optional_argument("version", "v") - .action([&arg_parser]() { + .actions([&arg_parser]() { arg_parser.print_version(); std::exit(EXIT_SUCCESS); }) @@ -1780,7 +1792,7 @@ inline void add_default_argument( case default_argument::o_input: arg_parser.add_optional_argument("input", "i") .nargs(1ull) - .action(action::check_file_exists()) + .actions(action::check_file_exists()) .help("Input file path"); break; @@ -1791,7 +1803,7 @@ inline void add_default_argument( case default_argument::o_multi_input: arg_parser.add_optional_argument("input", "i") .nargs(argon::nargs::at_least(1ull)) - .action(action::check_file_exists()) + .actions(action::check_file_exists()) .help("Input files paths"); break; diff --git a/include/argon/traits.hpp b/include/argon/traits.hpp index 9d14e11f..25d509dd 100644 --- a/include/argon/traits.hpp +++ b/include/argon/traits.hpp @@ -86,6 +86,33 @@ using argument_result_type = template concept c_one_of = std::disjunction_v...>; +/// @ingroup traits +/// @brief Type trait to check if a type is an instantiation of a specific class template. +/// @tparam T The type to check. +/// @tparam Template The class template to match against. +template typename Template> +struct is_instantiation_of : std::false_type {}; + +/// @ingroup traits +/// @brief Specialization of the @ref gl::traits::is_instantiation_of "is_instantiation_of" trait +/// for when the type is an instantiation of the provided class template. +template