Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 24 additions & 38 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,9 @@ parser.add_optional_argument<char>("method", "m").choices('a', 'b', 'c');
<br />

#### Value Actions - Functions that are called after parsing an argument's value.
Actions are represented as functions, which take the argument's value as an argument. The available action types are:
Actions are represented as standard C++ callables. The library automatically deduces the action's intent based on the function's signature. The available value action types are:

- `observe` actions | `void(const value_type&)` - applied to the parsed value. No value is returned - this action type is used to perform some logic on the parsed value without modifying it.
- **observe** actions | `void(const value_type&)` - applied to the parsed value. No value is returned. This is used to validate or log the parsed value without modifying it.

```cpp
void is_valid_user_tag(const std::string& tag) {
Expand All @@ -635,41 +635,29 @@ Actions are represented as functions, which take the argument's value as an argu
}

parser.add_optional_argument<std::string>("user", "u")
.actions<argon::action::observe>(is_valid_user_tag);
.actions(is_valid_user_tag); // The compiler automatically deduces this as an observer
```

- `transform` actions | `value_type(const value_type&)` - applied to the parsed value. The returned value will be used to initialize the argument's value.

```cpp
std::string to_lower(std::string s) {
for (auto& c : s)
c = static_cast<char>(std::tolower(c));
return s;
}

parser.add_optional_argument<std::string>("key", "k")
.actions<argon::action::transform>(to_lower);
```

- `modify` actions | `void(value_type&)` - applied to the initialized value of an argument.
- **modify** actions | `void(value_type&)` - mutates the parsed value of an argument in-place.

```cpp
void capitalize(std::string& s) {
s.at(0) = std::toupper(s.at(0));
}

parser.add_optional_argument<std::string>("name", "n")
.actions<argon::action::modify>(capitalize);
.actions(capitalize); // The compiler automatically deduces this as a modifier
```

> [!TIP]
>
> A single argument can have multiple value actions. Instead of writing complex logic in one action, consider composing several simple, focused actions for better readability and reusability. You can pass multiple actions into a single `.actions<type>(...)` call, e.g.:
> A single argument can have multiple value actions. Instead of writing complex logic in one action, consider composing several simple, focused actions for better readability and reusability. You can pass multiple actions (both observers and modifiers) into a single `.actions(...)` call, e.g.:
> ```cpp
> parser.add_optional_argument("email")
> .actions<argon::action::observe>(
> .actions(
> validate_email_format,
> validate_active_user
> validate_active_user,
> normalize_lowercase // A modifier that runs after validation
> )
> ```

Expand Down Expand Up @@ -839,19 +827,19 @@ parser.add_optional_argument<std::string>("mode")
Prints the parser's help message to the output stream and optionally exits with the given code.

```cpp
typename argon::action::on_flag::type print_help(
argon::action::util::state_callable_type print_help(
const argon::argument_parser& parser,
const std::optional<int> exit_code = std::nullopt,
std::ostream& os = std::cout
) noexcept;
```

- `check_file_exists` | observe (value type: `std::string`)

Throws if the provided file path does not exist.

```cpp
argon::action::util::callable_type<argon::action::observe, std::string> check_file_exists() noexcept;
auto check_file_exists() noexcept;
```

- `gt` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic))
Expand All @@ -860,7 +848,7 @@ parser.add_optional_argument<std::string>("mode")

```cpp
template <argon::traits::c_arithmetic T>
argon::action::util::callable_type<argon::action::observe, T> gt(const T lower_bound) noexcept;
auto gt(const T lower_bound) noexcept;
```

- `geq` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic))
Expand All @@ -869,7 +857,7 @@ parser.add_optional_argument<std::string>("mode")

```cpp
template <argon::traits::c_arithmetic T>
argon::action::util::callable_type<argon::action::observe, T> geq(const T lower_bound) noexcept;
auto geq(const T lower_bound) noexcept;
```

- `lt` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic))
Expand All @@ -878,7 +866,7 @@ parser.add_optional_argument<std::string>("mode")

```cpp
template <argon::traits::c_arithmetic T>
argon::action::util::callable_type<argon::action::observe, T> lt(const T upper_bound) noexcept;
auto lt(const T upper_bound) noexcept;
```

- `leq` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic))
Expand All @@ -887,18 +875,16 @@ parser.add_optional_argument<std::string>("mode")

```cpp
template <argon::traits::c_arithmetic T>
argon::action::util::callable_type<argon::action::observe, T> leq(const T upper_bound) noexcept;
auto leq(const T upper_bound) noexcept;
```

- `within` | observe (value type: [arithmetic](https://www.google.com/search?q=https%3A%2F%2Fen.cppreference.com%2Fw%2Fcpp%2Ftypes%2Fis_arithmetic))

Checks if the value is within the given interval. Bound inclusivity is customizable using template parameters.

```cpp
template <argon::traits::c_arithmetic T, bool LeftInclusive = true, bool RightInclusive = true>
argon::action::util::callable_type<argon::action::observe, T> within(
const T lower_bound, const T upper_bound
) noexcept;
template <traits::c_arithmetic T, bool LeftInclusive = true, bool RightInclusive = true>
auto within(const T lower_bound, const T upper_bound) noexcept;
```

<br/>
Expand All @@ -924,7 +910,7 @@ parser.default_arguments(<args>);
```cpp
// equivalent to:
parser.add_positional_argument<std::string>("input")
.actions<argon::action::observe>(argon::action::check_file_exists())
.actions(argon::action::check_file_exists())
.help("Input file path");
```

Expand Down Expand Up @@ -959,18 +945,18 @@ parser.default_arguments(<args>);
- `o_input` and `o_multi_input`:

```cpp
// input - equivalent to:
// o_input - equivalent to:
parser.add_optional_argument("input", "i")
.required()
.nargs(1)
.actions<argon::action::observe>(argon::action::check_file_exists())
.actions(argon::action::check_file_exists())
.help("Input file path");

// multi_input - equivalent to:
// o_multi_input - equivalent to:
parser.add_optional_argument("input", "i")
.required()
.nargs(argon::nargs::at_least(1))
.actions<argon::action::observe>(argon::action::check_file_exists())
.actions(argon::action::check_file_exists())
.help("Input files paths");
```

Expand Down
47 changes: 22 additions & 25 deletions include/argon/action/predefined.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ std::ostream& operator<<(std::ostream& os, const argument_parser&) noexcept;

namespace action {

/// @brief A predefined negative flag action tag that discards the parsed values.
/// @brief A predefined state action tag that discards the parsed values.
inline constexpr action::discard_parsed_t discard_parsed{};

/// @brief A predefined negative flag action tag that discards all parsed, default, and implicit values.
/// @brief A predefined state action tag that discards all parsed, default, and implicit values.
inline constexpr action::discard_all_t discard_all{};

/// @brief A factory function returning a negative action tag that injects specific values.
/// @brief A factory function returning a state action tag that injects specific values.
/// @param val The primary value to store.
/// @param vals Additional variadic values to store.
template <typename V, typename... Vs>
Expand All @@ -41,7 +41,7 @@ inline action::store_t<std::remove_cvref_t<V>> store(V&& val, Vs&&... vals) noex
// clang-format on
}

/// @brief A factory function returning a negative action tag that injects a range of values.
/// @brief A factory function returning a state action tag that injects a range of values.
/// @param range A range of values to store.
template <std::ranges::range R>
requires(not traits::c_argument_value_type<std::remove_cvref_t<R>>)
Expand All @@ -64,7 +64,7 @@ inline action::store_t<std::remove_cvref_t<std::ranges::range_value_t<R>>> store
/// @param parser Argument parser instance the help message of which will be printed.
/// @param exit_code The exit code with which `std::exit` will be called (if not `std::nullopt`).
/// @param os The output stream to which the help message will be printed.
inline typename argon::action::on_flag::type print_help(
inline util::state_callable_type print_help(
const argument_parser& parser,
const std::optional<int> exit_code = std::nullopt,
std::ostream& os = std::cout
Expand All @@ -76,8 +76,8 @@ inline typename argon::action::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<action::observe, std::string> check_file_exists() noexcept {
/// @brief Returns a value action callable which checks whether a file with the given path exists.
inline auto check_file_exists() noexcept {
return [](const std::string& file_path) {
if (not std::filesystem::exists(file_path))
throw std::filesystem::filesystem_error(
Expand All @@ -88,11 +88,11 @@ inline util::callable_type<action::observe, std::string> check_file_exists() noe
};
}

/// @brief Returns an *observe* action which checks if a parsed value is greater than the given bound.
/// @brief Returns a value action callable which checks if a parsed value is greater than the given bound.
/// @tparam T The *arithmetic* value type.
/// @param lower_bound The exclusive lower bound to validate against.
template <argon::traits::c_arithmetic T>
util::callable_type<action::observe, T> gt(const T lower_bound) noexcept {
auto gt(const T lower_bound) noexcept {
return [lower_bound](const T& value) {
if (not (value > lower_bound))
throw std::out_of_range(
Expand All @@ -101,46 +101,46 @@ util::callable_type<action::observe, T> gt(const T lower_bound) noexcept {
};
}

/// @brief Returns an *observe* action which checks if a parsed value is greater than or equal to the given bound.
/// @brief Returns a value action callable which checks if a parsed value is greater than or equal to the given bound.
/// @tparam T The *arithmetic* value type.
/// @param lower_bound The inclusive lower bound to validate against.
template <argon::traits::c_arithmetic T>
util::callable_type<action::observe, T> geq(const T lower_bound) noexcept {
auto geq(const T lower_bound) noexcept {
return [lower_bound](const T& value) {
if (! (value >= lower_bound))
if (not (value >= lower_bound))
throw std::out_of_range(
std::format("Value `{}` must be greater than or equal to `{}`!", value, lower_bound)
);
};
}

/// @brief Returns an *observe* action which checks if a parsed value is less than the given bound.
/// @brief Returns a value action callable which checks if a parsed value is less than the given bound.
/// @tparam T The *arithmetic* value type.
/// @param lower_bound The exclusive upper bound to validate against.
/// @param upper_bound The exclusive upper bound to validate against.
template <argon::traits::c_arithmetic T>
util::callable_type<action::observe, T> lt(const T upper_bound) noexcept {
auto lt(const T upper_bound) noexcept {
return [upper_bound](const T& value) {
if (! (value < upper_bound))
if (not (value < upper_bound))
throw std::out_of_range(
std::format("Value `{}` must be less than `{}`!", value, upper_bound)
);
};
}

/// @brief Returns an *observe* action which checks if a parsed value is less than or equal to the given bound.
/// @brief Returns a value action callable which checks if a parsed value is less than or equal to the given bound.
/// @tparam T The *arithmetic* value type.
/// @param lower_bound The inclusive upper bound to validate against.
/// @param upper_bound The inclusive upper bound to validate against.
template <argon::traits::c_arithmetic T>
util::callable_type<action::observe, T> leq(const T upper_bound) noexcept {
auto leq(const T upper_bound) noexcept {
return [upper_bound](const T& value) {
if (! (value <= upper_bound))
if (not (value <= upper_bound))
throw std::out_of_range(
std::format("Value `{}` must be less than or equal to `{}`!", value, upper_bound)
);
};
}

/// @brief Returns an *observe* action which checks if a parsed value falls within the specified interval.
/// @brief Returns a value action callable which checks if a parsed value falls within the specified interval.
///
/// The interval is defined by the given lower and upper bounds, with inclusivity controlled by the template parameters.
///
Expand All @@ -149,11 +149,8 @@ util::callable_type<action::observe, T> leq(const T upper_bound) noexcept {
/// @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 <traits::c_arithmetic T, bool LeftInclusive = true, bool RightInclusive = true>
util::callable_type<argon::action::observe, T> within(
const T lower_bound, const T upper_bound
) noexcept {
auto within(const T lower_bound, const T upper_bound) noexcept {
return [lower_bound, upper_bound](const T& value) {
constexpr char left_brace = LeftInclusive ? '[' : '(';
constexpr char right_brace = RightInclusive ? ']' : ')';
Expand Down
59 changes: 35 additions & 24 deletions include/argon/action/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,49 @@

#include "argon/action/types.hpp"

#include <utility>
#include <variant>
#include <concepts>
#include <type_traits>

namespace argon::traits {

/// @brief The concept is satisfied when `AS` is a valid *value* action action specifier.
/// @tparam AS The action specifier type.
/// @ingroup traits
template <typename AS>
concept c_value_action_specifier =
argon::traits::c_one_of<AS, action::observe, action::transform, action::modify>;

/// @brief The concept is satisfied when `AS` is a valid *on-flag* action action specifier.
/// @tparam AS The action specifier type.
/// @ingroup traits
template <typename AS>
concept c_flag_action_specifier = argon::traits::c_one_of<AS, action::on_flag>;

/// @brief The concept is satisfied when `AS` is a valid action action specifier.
/// @tparam AS The action specifier type.
/// @ingroup traits
template <typename AS>
concept c_action_specifier = c_value_action_specifier<AS> or std::same_as<AS, action::on_flag>;
/// @brief Validates that `AC` is a valid action category tag (`action::value` or `action::on_flag`).
template <typename AC>
concept c_action_category = c_one_of<AC, action::value, action::on_flag>;

// --- Value Action Concepts ---

/// @brief Validates that `F` is a valid *observe* callable for value type `T` (`void(const T&)`).
/// @note Safely rejects callables taking non-const `T&`.
template <typename F, typename T>
concept c_observe_callable =
c_argument_value_type<T> and std::invocable<std::remove_cvref_t<F>&, const T&>
and std::same_as<std::invoke_result_t<std::remove_cvref_t<F>&, const T&>, void>;

/// @brief Validates that `F` is a valid *modify* callable for value type `T` (`void(T&)`).
/// @note Rejects callables taking `const T&` or `T` by value to prevent accidental no-op modifications.
template <typename F, typename T>
concept c_modify_callable =
c_argument_value_type<T> and std::invocable<std::remove_cvref_t<F>&, T&>
and (not std::invocable<std::remove_cvref_t<F>&, const T&>)
and std::same_as<std::invoke_result_t<std::remove_cvref_t<F>&, T&>, void>;

/// @brief Validates that `F` is a valid value action callable (either observer or modifier) for value type `T`.
template <typename F, typename T>
concept c_value_action = c_observe_callable<F, T> or c_modify_callable<F, T>;

// --- State Action Concepts ---

/// @brief Concept validating if a type is a predefined state action tag.
template <typename T>
concept c_state_action_specifier =
c_one_of<T, action::discard_parsed_t, action::discard_all_t>
or c_instantiation_of<T, action::store_t>;
c_one_of<std::remove_cvref_t<T>, action::discard_parsed_t, action::discard_all_t>
or c_instantiation_of<std::remove_cvref_t<T>, action::store_t>;

/// @brief Concept validating if a type is a valid state action (a specifier tag or an invocable callable).
/// @brief Concept validating if a type is a valid state action (a specifier tag or a `void()` invocable callable).
template <typename F>
concept c_state_action = c_state_action_specifier<std::remove_cvref_t<F>> or std::invocable<F>;
concept c_state_action =
c_state_action_specifier<F>
or (std::invocable<std::remove_cvref_t<F>&>
and std::same_as<std::invoke_result_t<std::remove_cvref_t<F>&>, void>);

} // namespace argon::traits
Loading
Loading