From a89b5c2b69d7ead95d39b6fd2eab6fcf4ec81538 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 08:29:31 +0100 Subject: [PATCH] Add LastValueWins, RequireEquals, multi-char Short, Sensitive argv masking, and occurrences log --- include/rfl/Short.hpp | 2 +- include/rfl/cli.hpp | 2 + include/rfl/cli/LastValueWins.hpp | 41 ++++++ include/rfl/cli/Opt.hpp | 11 ++ include/rfl/cli/RequireEquals.hpp | 41 ++++++ include/rfl/cli/options.hpp | 29 ++++- include/rfl/cli/parse_argv.hpp | 107 +++++++++++++--- include/rfl/cli/read.hpp | 16 ++- include/rfl/cli/resolve_args.hpp | 8 +- .../test_parsing_modes_and_occurrences.cpp | 120 ++++++++++++++++++ 10 files changed, 347 insertions(+), 30 deletions(-) create mode 100644 include/rfl/cli/LastValueWins.hpp create mode 100644 include/rfl/cli/RequireEquals.hpp create mode 100644 tests/cli/test_parsing_modes_and_occurrences.cpp diff --git a/include/rfl/Short.hpp b/include/rfl/Short.hpp index b4fb59afe..afb609ba5 100644 --- a/include/rfl/Short.hpp +++ b/include/rfl/Short.hpp @@ -19,7 +19,7 @@ struct Short { /// The short name. static constexpr auto short_name_ = _name; - static_assert(_name.length == 1, "Short name must be exactly one character."); + static_assert(_name.length >= 1, "Short name cannot be empty."); /// @brief Default constructor. /// @details Constructs the underlying value using its default constructor. diff --git a/include/rfl/cli.hpp b/include/rfl/cli.hpp index 361a45feb..4a73da6b9 100644 --- a/include/rfl/cli.hpp +++ b/include/rfl/cli.hpp @@ -5,12 +5,14 @@ #include "cli/AllowAbbreviations.hpp" #include "cli/App.hpp" #include "cli/KeepEmptyItems.hpp" +#include "cli/LastValueWins.hpp" #include "cli/ListDelimiter.hpp" #include "cli/Opt.hpp" #include "cli/OptionsFirst.hpp" #include "cli/Parser.hpp" #include "cli/PureFlags.hpp" #include "cli/Reader.hpp" +#include "cli/RequireEquals.hpp" #include "cli/SeparateOptionValues.hpp" #include "cli/options.hpp" #include "cli/parse_argv.hpp" diff --git a/include/rfl/cli/LastValueWins.hpp b/include/rfl/cli/LastValueWins.hpp new file mode 100644 index 000000000..1afad03f2 --- /dev/null +++ b/include/rfl/cli/LastValueWins.hpp @@ -0,0 +1,41 @@ +#ifndef RFL_CLI_LAST_VALUE_WINS_HPP_ +#define RFL_CLI_LAST_VALUE_WINS_HPP_ + +#include + +#include "../Processors.hpp" + +namespace rfl::cli { + +/// A setting that makes repeated non-list options overwrite their previous +/// value instead of failing with a duplicate/conflict error (last occurrence +/// in argv wins, as in GNU/compiler CLIs such as gcc, cppcheck, and aria2). +/// Use `rfl::cli::read_detailed` to also inspect every occurrence in argv order. +struct LastValueWins { + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +namespace internal { + +template +struct last_value_wins : std::false_type {}; + +template <> +struct last_value_wins : std::true_type {}; + +template +struct last_value_wins> + : std::disjunction...> {}; + +template +constexpr bool last_value_wins_v = + last_value_wins>>::value; + +} // namespace internal + +} // namespace rfl::cli + +#endif diff --git a/include/rfl/cli/Opt.hpp b/include/rfl/cli/Opt.hpp index 75e7c2aba..010127f9f 100644 --- a/include/rfl/cli/Opt.hpp +++ b/include/rfl/cli/Opt.hpp @@ -39,6 +39,11 @@ struct Implicit { struct MultiToken {}; +/// Marks an option's value as sensitive so mutable `char* argv[]` overloads +/// overwrite the value in place with `'*'` after parsing (hiding secrets from +/// process listings). +struct Sensitive {}; + template struct Desc { static constexpr auto value = _desc; @@ -71,6 +76,7 @@ struct opt_attrs<> { static constexpr bool has_group = false; static constexpr bool has_value_name = false; static constexpr bool is_multi_token = false; + static constexpr bool is_sensitive = false; static constexpr size_t num_implicit_value = 0; static constexpr int count = 0; static constexpr rfl::internal::StringLiteral<1> short_name = ""; @@ -138,6 +144,11 @@ struct opt_attrs : opt_attrs { static constexpr bool is_multi_token = true; }; +template +struct opt_attrs : opt_attrs { + static constexpr bool is_sensitive = true; +}; + template struct opt_attrs, Rest...> : opt_attrs { using Next = opt_attrs; diff --git a/include/rfl/cli/RequireEquals.hpp b/include/rfl/cli/RequireEquals.hpp new file mode 100644 index 000000000..550499483 --- /dev/null +++ b/include/rfl/cli/RequireEquals.hpp @@ -0,0 +1,41 @@ +#ifndef RFL_CLI_REQUIRE_EQUALS_HPP_ +#define RFL_CLI_REQUIRE_EQUALS_HPP_ + +#include + +#include "../Processors.hpp" + +namespace rfl::cli { + +/// A setting that requires long options taking a value (`OptionKind::value`) +/// to be given with `=` (`--key=value`) when `SeparateOptionValues` is not +/// enabled, failing with a missing-value error if bare `--key` is passed. +/// Flags and `rfl::ImplicitValue` options still work without `=`. +struct RequireEquals { + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +namespace internal { + +template +struct require_equals : std::false_type {}; + +template <> +struct require_equals : std::true_type {}; + +template +struct require_equals> + : std::disjunction...> {}; + +template +constexpr bool require_equals_v = + require_equals>>::value; + +} // namespace internal + +} // namespace rfl::cli + +#endif diff --git a/include/rfl/cli/options.hpp b/include/rfl/cli/options.hpp index d53999e63..06075f221 100644 --- a/include/rfl/cli/options.hpp +++ b/include/rfl/cli/options.hpp @@ -33,12 +33,14 @@ #include "../internal/processed_t.hpp" #include "../parsing/is_string_map.hpp" #include "AllowAbbreviations.hpp" +#include "LastValueWins.hpp" #include "Opt.hpp" #include "OptionsFirst.hpp" #include "Parser.hpp" #include "PureFlags.hpp" -#include "SeparateOptionValues.hpp" #include "Reader.hpp" +#include "RequireEquals.hpp" +#include "SeparateOptionValues.hpp" namespace rfl::cli::internal { @@ -80,6 +82,10 @@ struct Option { /// Whether a list option consumes multiple space-separated tokens after one /// occurrence (rfl::MultiToken). bool multi_token = false; + + /// Whether the option carries a sensitive value (rfl::cli::attr::Sensitive) + /// that should be overwritten with `'*'` in mutable `char* argv[]`. + bool sensitive = false; }; /// The options that a target struct accepts, built once per @@ -167,6 +173,14 @@ struct Options { /// token (rfl::cli::PureFlags). bool pure_flags = false; + /// Whether repeated non-list options overwrite their previous value instead + /// of failing (rfl::cli::LastValueWins). + bool last_value_wins = false; + + /// Whether long value options require `=` when `separate_values` is false + /// (rfl::cli::RequireEquals). + bool require_equals = false; + /// Records one leaf long option along with its optional hidden/group/value_name /// metadata. Non-template to avoid per-field template instantiations. void add_leaf_option(std::string _path, const Option& _opt, @@ -388,6 +402,7 @@ struct cli_annotations { static constexpr bool has_group = false; static constexpr bool has_value_name = false; static constexpr bool is_multi_token = false; + static constexpr bool is_sensitive = false; static constexpr size_t num_implicit_value = 0; static constexpr rfl::internal::StringLiteral<1> short_name = ""; static constexpr int count = 0; @@ -463,6 +478,8 @@ struct cli_annotations> { Info::has_value_name || Inner::has_value_name; static constexpr bool is_multi_token = Info::is_multi_token || Inner::is_multi_token; + static constexpr bool is_sensitive = + Info::is_sensitive || Inner::is_sensitive; static constexpr size_t num_implicit_value = Info::num_implicit_value + Inner::num_implicit_value; static constexpr int count = Info::count + Inner::count; @@ -664,7 +681,8 @@ Option implicit_value_option() { "bool, a std::string or a list, such as " "rfl::ImplicitValue."); return Option{OptionKind::implicit_value, is_option_list_v, - Annotations::implicit_value.string_view()}; + Annotations::implicit_value.string_view(), false, + Annotations::is_sensitive}; } /// The option for a field, or a key of a map or of rfl::ExtraFields, with @@ -686,7 +704,8 @@ Option option_for() { } else { return Option{OptionKind::value, is_option_list_v, {}, - cli_annotations_t::is_multi_token}; + cli_annotations_t::is_multi_token, + cli_annotations_t::is_sensitive}; } } @@ -792,7 +811,7 @@ void collect_options(const std::string& _prefix, Options* _options) { _options->add_leaf_option( std::move(path), Option{OptionKind::value, is_option_list_v, {}, - Ann::is_multi_token}, + Ann::is_multi_token, Ann::is_sensitive}, is_hidden, group_name, value_name); } } @@ -870,6 +889,8 @@ Options make_options() { options.allow_abbreviations = allow_abbreviations_v; options.options_first = options_first_v; options.pure_flags = pure_flags_v; + options.last_value_wins = last_value_wins_v; + options.require_equals = require_equals_v; using U = unwrapped_option_t; if constexpr (_all_paths) { collect_value_options>(&options); diff --git a/include/rfl/cli/parse_argv.hpp b/include/rfl/cli/parse_argv.hpp index 611b9bbc9..08d9a8d24 100644 --- a/include/rfl/cli/parse_argv.hpp +++ b/include/rfl/cli/parse_argv.hpp @@ -1,6 +1,7 @@ #ifndef RFL_CLI_PARSE_ARGV_HPP_ #define RFL_CLI_PARSE_ARGV_HPP_ +#include #include #include #include @@ -85,7 +86,8 @@ namespace internal { /// bare arguments → positional /// -- → everything after goes to positional inline rfl::Result parse_string_views( - std::span _args, const Options& _options) { + std::span _args, const Options& _options, + std::vector* _sensitive_spans = nullptr) { ParsedArgs result; const bool negative_numbers_are_options = _options.negative_numbers_are_options; @@ -106,10 +108,11 @@ inline rfl::Result parse_string_views( }; // Every occurrence of a list option adds to it. Any other option can be - // given only once. + // given only once unless LastValueWins is set. const auto add_long = [&](std::string _key, std::string _val, const Option& _option, std::string_view _raw) -> rfl::Result { + result.occurrences.emplace_back(_key, _val); const auto it = result.named.find(_key); if (it == result.named.end()) { result.named.emplace(std::move(_key), std::move(_val)); @@ -117,6 +120,9 @@ inline rfl::Result parse_string_views( else if (_option.is_list) { result.later_occurrences[it->first].emplace_back(std::move(_val)); } + else if (_options.last_value_wins) { + it->second = std::move(_val); + } else { return error("Duplicate argument: " + std::string(_raw)); } @@ -126,12 +132,16 @@ inline rfl::Result parse_string_views( const auto add_short = [&](std::string _key, std::string _val, const Option& _option, std::string_view _raw) -> rfl::Result { - if (_option.is_list) { - // Under its long name, so that `-r a --report b` keeps argv order. A - // short option is only a list if it is in `_options`. + if (_option.is_list + || (_options.last_value_wins && _option.kind != OptionKind::unknown)) { + // Under its long name, so that `-r a --report b` keeps argv order and + // LastValueWins resolves short/long overrides cleanly. return add_long(*_options.find_short_target(_key), std::move(_val), _option, _raw); } + if (const auto* target = _options.find_short_target(_key)) { + result.occurrences.emplace_back(*target, _val); + } result.short_order.emplace_back(_key); if (!result.short_args.emplace(std::move(_key), std::move(_val)).second) { return error("Duplicate short argument: " + std::string(_raw)); @@ -139,6 +149,13 @@ inline rfl::Result parse_string_views( return true; }; + const auto record_sensitive = [&](const Option& _option, + const std::string_view _span) { + if (_sensitive_spans && _option.sensitive && !_span.empty()) { + _sensitive_spans->push_back(_span); + } + }; + // The value of the option _args[*_i], given without '=', taking the next // token if the option says so. const auto take_value = [&](const Option& _option, std::string_view _raw, @@ -151,6 +168,7 @@ inline rfl::Result parse_string_views( return error( "Missing value for option '" + std::string(_raw) + "'."); } + record_sensitive(_option, _args[*_i + 1]); return std::string(_args[++*_i]); case OptionKind::flag: @@ -172,6 +190,7 @@ inline rfl::Result parse_string_views( // A variant, rfl::Generic or nested struct: `-m fast` takes the next // token unless it looks like an option. if (has_next && !looks_like_option(_args[*_i + 1])) { + record_sensitive(_option, _args[*_i + 1]); return std::string(_args[++*_i]); } return std::string(); @@ -202,8 +221,9 @@ inline rfl::Result parse_string_views( const auto eq = arg.find('='); auto key = std::string( eq == std::string_view::npos ? arg : arg.substr(0, eq)); - auto val = std::string( - eq == std::string_view::npos ? "" : arg.substr(eq + 1)); + const std::string_view eq_val = + eq == std::string_view::npos ? std::string_view{} : arg.substr(eq + 1); + auto val = std::string(eq_val); if (key.empty()) { return error("Empty key in argument: " + std::string(arg_raw)); } @@ -222,9 +242,18 @@ inline rfl::Result parse_string_views( if (option.kind == OptionKind::unknown && _options.reject_unknown) { return error("Unknown option '--" + key + "'."); } + if (eq != std::string_view::npos) { + record_sensitive(option, eq_val); + } bool consume_multi = false; if (eq == std::string_view::npos && option.kind == OptionKind::value - && _options.separate_values) { + && _options.require_equals) { + // rfl::cli::RequireEquals takes precedence over SeparateOptionValues + // so presets (e.g. GnuStyle / GnuLastWins) compose cleanly with it. + return error("Missing value for option '--" + key + "'."); + } + else if (eq == std::string_view::npos && option.kind == OptionKind::value + && _options.separate_values) { // --key value (rfl::cli::SeparateOptionValues). A flag never takes // the next token: `--verbose 0` leaves "0" positional. // The message names the option that `key` abbreviates. @@ -243,6 +272,7 @@ inline rfl::Result parse_string_views( if (!r) { return error(r.error().what()); } while (consume_multi && i + 1 < _args.size() && _args[i + 1] != "--" && !looks_like_option(_args[i + 1])) { + record_sensitive(option, _args[i + 1]); const auto r2 = add_long(key, std::string(_args[++i]), option, arg_raw); if (!r2) { return error(r2.error().what()); } @@ -250,8 +280,8 @@ inline rfl::Result parse_string_views( continue; } - // Short options: -x, -x value, -x=value, and groups of them in one - // token, as in getopt: -qv, -fvalue, -qfvalue, -qf value. + // Short options: -x, -x value, -x=value, multi-character single-dash + // options (-CC, -rp=paths), and single-char clusters (-qv, -fvalue). if (arg_raw.size() >= 2 && arg_raw[0] == '-') { const auto group = arg_raw.substr(1); @@ -265,17 +295,29 @@ inline rfl::Result parse_string_views( continue; } + // Check for an exact multi-character single-dash short option (e.g. -CC, + // -rp, -rp=paths) before splitting character-by-character into clusters. + const auto full_eq = group.find('='); + const auto full_key = + full_eq == std::string_view::npos ? group : group.substr(0, full_eq); + const bool exact_multi_short = + _options.group_short_options && full_key.size() > 1 + && _options.find_short(full_key).kind != OptionKind::unknown; + // Without a table, the short options are not known, so -abc is the one // short option "abc": -abc value, -abc=value or -abc. for (size_t j = 0; j < group.size(); ++j) { - // Short names are single ASCII characters. An unknown character may - // take several bytes in UTF-8, so take them all for the message. + // Short names are single ASCII characters (or exact multi-char short + // names matched above). An unknown character may take several bytes in + // UTF-8, so take them all for the message. const auto c = static_cast(group[j]); auto key = std::string( - _options.group_short_options - ? group.substr( - j, c >= 0xF0 ? 4 : c >= 0xE0 ? 3 : c >= 0xC0 ? 2 : 1) - : group.substr(0, group.find('='))); + exact_multi_short + ? full_key + : (_options.group_short_options + ? group.substr( + j, c >= 0xF0 ? 4 : c >= 0xE0 ? 3 : c >= 0xC0 ? 2 : 1) + : full_key)); const auto option = _options.find_short(key); const auto rest = group.substr(j + key.size()); const auto name = "-" + key; @@ -286,7 +328,9 @@ inline rfl::Result parse_string_views( if (j == 0 && rest.starts_with('=')) { // -x=value, whatever -x is. Unlike getopt, where -f=csv gives // "=csv", the '=' is not part of the value. - val = std::string(rest.substr(1)); + const auto v_span = rest.substr(1); + record_sensitive(option, v_span); + val = std::string(v_span); } else if (option.kind == OptionKind::unknown) { if (!alone) { @@ -315,7 +359,9 @@ inline rfl::Result parse_string_views( else if (!rest.empty()) { // -fvalue, -qfvalue, -hvalue: the rest of the token is the value. // -qf=value is the same as -qfvalue, as -f=value is. - val = std::string(rest.starts_with('=') ? rest.substr(1) : rest); + const auto v_span = rest.starts_with('=') ? rest.substr(1) : rest; + record_sensitive(option, v_span); + val = std::string(v_span); } else { // The last option of the token handles the next one as it would @@ -330,6 +376,7 @@ inline rfl::Result parse_string_views( if (!r) { return error(r.error().what()); } while (consume_multi && i + 1 < _args.size() && _args[i + 1] != "--" && !looks_like_option(_args[i + 1])) { + record_sensitive(option, _args[i + 1]); const auto r2 = add_short(key, std::string(_args[++i]), option, arg_raw); if (!r2) { return error(r2.error().what()); } @@ -387,8 +434,9 @@ bool is_null(const S& _str) noexcept { /// Parses every element of `_args`, rejecting null pointers. /// `_first_index` is the index of the first element in error messages. template -rfl::Result parse_strings(R&& _args, size_t _first_index, - const Options& _options) { +rfl::Result parse_strings( + R&& _args, size_t _first_index, const Options& _options, + std::vector* _sensitive_spans = nullptr) { std::vector views; auto i = _first_index; for (auto&& arg : _args) { @@ -399,7 +447,7 @@ rfl::Result parse_strings(R&& _args, size_t _first_index, views.push_back(view); ++i; } - return parse_string_views(views, _options); + return parse_string_views(views, _options, _sensitive_spans); } /// Converts every element of `_args` to UTF-8 and parses it. @@ -465,6 +513,23 @@ inline rfl::Result parse_argv(int argc, const char* const argv[], [&](auto _args) { return parse_strings(_args, 1, _options); }); } +/// Parses mutable argc/argv with the options of the target struct, skipping +/// argv[0] and overwriting any `Option::sensitive` value spans in place with +/// `'*'`. +inline rfl::Result parse_argv(int argc, char* argv[], + const Options& _options) { + return skip_program_name(argc, static_cast(argv)) + .and_then([&](auto _args) { + std::vector sensitive_spans; + auto res = parse_strings(_args, 1, _options, &sensitive_spans); + for (const auto& span : sensitive_spans) { + auto* p = const_cast(span.data()); + std::fill(p, p + span.size(), '*'); + } + return res; + }); +} + /// Parses wide argc/argv with the options of the target struct, skipping /// argv[0] (the program name). Converts each wide string to UTF-8. inline rfl::Result parse_argv(int argc, const wchar_t* const argv[], diff --git a/include/rfl/cli/read.hpp b/include/rfl/cli/read.hpp index 16fd6d8a7..504e25732 100644 --- a/include/rfl/cli/read.hpp +++ b/include/rfl/cli/read.hpp @@ -29,6 +29,9 @@ template struct ReadResult { T value; std::vector provided; + /// Every recognized option occurrence `(canonical_long_name, raw_value)` in + /// exact argv order. + std::vector> occurrences; /// Returns true if the canonical option or positional name `_name` was /// explicitly supplied on the command line. @@ -68,7 +71,8 @@ rfl::Result> read_parsed_detailed(ParsedArgs _parsed, &_args.later_occurrences}; return Parser::read(r, var).transform( [&](T&& _val) { - return ReadResult{std::move(_val), std::move(provided)}; + return ReadResult{std::move(_val), std::move(provided), + std::move(_args.occurrences)}; }); }); } @@ -185,7 +189,10 @@ rfl::Result read(int argc, const char* const argv[]) { template rfl::Result read(int argc, char* argv[]) { - return read(argc, static_cast(argv)); + return internal::parse_and_read( + [&](const auto& _options) { + return internal::parse_argv(argc, argv, _options); + }); } /// Parses wide-character command-line arguments into a struct using reflection. @@ -231,7 +238,10 @@ rfl::Result> read_detailed(int argc, const char* const argv[]) { template rfl::Result> read_detailed(int argc, char* argv[]) { - return read_detailed(argc, static_cast(argv)); + return internal::parse_and_read_detailed( + [&](const auto& _options) { + return internal::parse_argv(argc, argv, _options); + }); } template diff --git a/include/rfl/cli/resolve_args.hpp b/include/rfl/cli/resolve_args.hpp index 0adc82644..a0ac061d9 100644 --- a/include/rfl/cli/resolve_args.hpp +++ b/include/rfl/cli/resolve_args.hpp @@ -27,6 +27,9 @@ struct ParsedArgs { /// The occurrences after the first of each list option given more than /// once, by long name, in argv order. internal::CliLaterOccurrencesType later_occurrences; + /// Every recognized option occurrence `(canonical_long_name, raw_value)` in + /// exact argv order. + std::vector> occurrences; }; namespace internal { @@ -42,6 +45,8 @@ struct ResolvedArgs { /// dropped, or empty: with rfl::cli::OptionsFirst, the list as the last /// positional field when it gets the rest of the command line. std::string verbatim_list; + /// Every option occurrence `(canonical_long_name, raw_value)` in argv order. + std::vector> occurrences; }; /// Applies what parse_argv could not know without the option table. @@ -160,7 +165,8 @@ inline rfl::Result resolve_args( return ResolvedArgs{std::move(result), std::move(_parsed.later_occurrences), - std::move(verbatim_list)}; + std::move(verbatim_list), + std::move(_parsed.occurrences)}; } /// The error for a long option that abbreviates several options, worded diff --git a/tests/cli/test_parsing_modes_and_occurrences.cpp b/tests/cli/test_parsing_modes_and_occurrences.cpp new file mode 100644 index 000000000..468422fcb --- /dev/null +++ b/tests/cli/test_parsing_modes_and_occurrences.cpp @@ -0,0 +1,120 @@ +#include + +#include +#include +#include +#include +#include +#include + +namespace test_parsing_modes_and_occurrences { + +using namespace rfl::cli::attr; + +struct AnalyzerOpts { + rfl::cli::Opt, Short<"CC">, Desc<"Keep comments">> + keep_comments = false; + rfl::cli::Opt, Short<"rp">, Implicit<"">, + Desc<"Relative paths">> + relative_paths; + rfl::cli::Opt, Short<"f">, Desc<"Force">> force = false; + rfl::cli::Opt, Desc<"Max configs">> max_configs; + rfl::cli::Opt, Short<"j">, Desc<"Jobs">> jobs; + rfl::cli::Opt>, Desc<"Enable checks">> + enable; + rfl::cli::Opt>, Desc<"Disable checks">> + disable; + rfl::cli::Opt, Short<"A">, Sensitive, + Desc<"Secret token">> + secret; + rfl::cli::Opt>, Positional> files; +}; + +using StrictEqCli = + rfl::Processors>; + +TEST(cli, test_multi_char_short_options) { + const auto res = rfl::cli::read( + {"-CC", "-rp=src;inc", "main.cpp"}); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(rfl::unwrap(res->keep_comments)); + EXPECT_EQ(rfl::unwrap(res->relative_paths), + std::optional("src;inc")); + + const auto bare_rp = + rfl::cli::read({"-rp", "main.cpp"}); + ASSERT_TRUE(bare_rp) << bare_rp.error().what(); + EXPECT_EQ(rfl::unwrap(bare_rp->relative_paths), + std::optional("")); +} + +TEST(cli, test_require_equals_rejects_bare_value_option) { + const auto missing = + rfl::cli::read({"--max-configs", "main.cpp"}); + ASSERT_FALSE(missing); + EXPECT_NE(std::string(missing.error().what()).find("Missing value"), + std::string::npos); + + // ImplicitValue and flags still work without '='. + const auto ok = rfl::cli::read( + {"--relative-paths", "--force", "--max-configs=5", "main.cpp"}); + ASSERT_TRUE(ok) << ok.error().what(); + EXPECT_TRUE(rfl::unwrap(ok->force)); + EXPECT_EQ(rfl::unwrap(ok->max_configs), std::optional(5)); + // RequireEquals takes precedence even when combined with SeparateOptionValues. + using CombinedProcs = + rfl::Processors; + const auto combined_missing = + rfl::cli::read({"--max-configs", "5"}); + ASSERT_FALSE(combined_missing); + const auto combined_ok = + rfl::cli::read({"--max-configs=5"}); + ASSERT_TRUE(combined_ok) << combined_ok.error().what(); + EXPECT_EQ(rfl::unwrap(combined_ok->max_configs), std::optional(5)); +} + +TEST(cli, test_last_value_wins_and_occurrences_order) { + const auto res = rfl::cli::read_detailed( + {"-f", "--max-configs=3", "--max-configs=7", "--force", "-j", "2", "-j4", + "--enable=all", "--disable=style", "--enable=warning", "a.cpp"}); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(rfl::unwrap(res->value.force)); + EXPECT_EQ(rfl::unwrap(res->value.max_configs), std::optional(7)); + EXPECT_EQ(rfl::unwrap(res->value.jobs), std::optional("4")); + + // Verify exact argv occurrence order with canonical long names. + using Pair = std::pair; + const std::vector expected = { + {"force", ""}, {"max-configs", "3"}, + {"max-configs", "7"}, {"force", ""}, + {"jobs", "2"}, {"jobs", "4"}, + {"enable", "all"}, {"disable", "style"}, + {"enable", "warning"}, + }; + EXPECT_EQ(res->occurrences, expected); +} + +TEST(cli, test_sensitive_masks_mutable_argv_in_place) { + char prog[] = "tool"; + char opt1[] = "-A"; + char val1[] = "ALPHA"; + char opt2[] = "--secret=TOPSECRET"; + char file[] = "a.cpp"; + char* argv[] = {prog, opt1, val1, opt2, file}; + + const auto res = rfl::cli::read_detailed(5, argv); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(rfl::unwrap(res->value.secret), + std::optional("TOPSECRET")); + // Both sensitive values were overwritten in place with '*'. + EXPECT_STREQ(val1, "*****"); + EXPECT_STREQ(opt2, "--secret=*********"); + // Non-sensitive args remain untouched. + EXPECT_STREQ(file, "a.cpp"); +} + +} // namespace test_parsing_modes_and_occurrences