From c54cfc2f22369a137f572714366357897e6af005 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Sat, 14 Nov 2026 06:05:07 +0100 Subject: [PATCH] Accept abbreviated long options in rfl::cli With the new rfl::cli::AllowAbbreviations processor, a long option that matches no field stands for the one long option it is a prefix of, so `--rep` is `--report`. Only the last part of a dotted name is shortened (`--database.h`, not `--data`). An exact match wins, and a prefix of several options fails with "Option '--for' is ambiguous; possibilities: '--force-export', '--format'." Short options, map keys and rfl::ExtraFields keys are never abbreviated. This is opt-in, like rfl::AllowRawPtrs. Without AllowAbbreviations, rfl::cli::read behaves as before: a long option that is a prefix of another is ignored, or rejected with rfl::NoExtraFields. Co-Authored-By: Claude Opus 5.5 --- docs/content/supported_formats/cli.md | 44 ++++ include/rfl/cli.hpp | 1 + include/rfl/cli/AllowAbbreviations.hpp | 51 +++++ include/rfl/cli/options.hpp | 44 +++- include/rfl/cli/parse_argv.hpp | 20 +- include/rfl/cli/read.hpp | 6 +- include/rfl/cli/resolve_args.hpp | 50 ++++- tests/cli/test_abbreviations.cpp | 276 +++++++++++++++++++++++++ 8 files changed, 482 insertions(+), 10 deletions(-) create mode 100644 include/rfl/cli/AllowAbbreviations.hpp create mode 100644 tests/cli/test_abbreviations.cpp diff --git a/docs/content/supported_formats/cli.md b/docs/content/supported_formats/cli.md index b5e89b68..2417be40 100644 --- a/docs/content/supported_formats/cli.md +++ b/docs/content/supported_formats/cli.md @@ -117,6 +117,49 @@ const auto result = rfl::cli::read(argc, argv); Unknown short options are always an error. +## Abbreviated long options + +Pass `rfl::cli::AllowAbbreviations` to accept abbreviated long options, like +argparse's `allow_abbrev`. Other formats ignore it: + +```cpp +rfl::cli::read(argc, argv); +``` + +A long option can then be shortened to a prefix that matches only one option: +with fields `report` and `format`, `--rep=a` and `--form` stand for +`--report=a` and `--format`. This works for flags, +positional fields given by name and nested fields, where only the last part +of the dotted name is shortened: `--database.h` stands for +`--database.host`, but `--data` and `--database.` stand for nothing. + +- An exact name always wins: `--format` is `--format` even when + `--format-version` exists. +- A prefix of several options is an error, even without `rfl::NoExtraFields`: + `Option '--for' is ambiguous; possibilities: '--force-export', '--format'.` + A field has only one long name, so the matches are always different + fields. +- A prefix of no option is an unknown option, as above. +- Short options, map keys and `rfl::ExtraFields` keys are never + abbreviated. Under `rfl::ExtraFields`, an unknown name is an extra key. +- The two-step API expands abbreviations in `resolve_args` when `Ps` + contains `rfl::cli::AllowAbbreviations`. `parse_argv` does not know the + struct, so there the value of an abbreviated option must follow `=`. + +With `rfl::cli::AllowAbbreviations`, an option that matches no field is +not always unknown: `--rep` sets `--report` if that is its only match. With +a field `verbose_logging` and no `verbose`, `--verbose` sets +`verbose_logging`. `rfl::NoExtraFields` does not stop this: with a field +`port_range` and no `port`, `--port=8080` fills `port_range` instead of +failing. Adding an option can make an abbreviation ambiguous, so scripts +should spell options out. Without `rfl::cli::AllowAbbreviations`, only exact +names match: + +```cpp +rfl::cli::read(argc, argv); +// ./app --rep=a -> --rep is an unknown option +``` + ## Positional arguments Wrap a field with `rfl::Positional` to accept it as a bare (non-flag) argument: @@ -394,6 +437,7 @@ Parsing proceeds in three stages: - `include/rfl/cli/Parser.hpp` — Parser type alias - `include/rfl/cli/parse_argv.hpp` — `argv` -> `ParsedArgs` - `include/rfl/cli/options.hpp` — the internal option table: the long, short and positional options of a struct +- `include/rfl/cli/AllowAbbreviations.hpp` — `AllowAbbreviations` processor - `include/rfl/cli/resolve_args.hpp` — `ParsedArgs` -> `map` - `include/rfl/cli/usage.hpp` — `usage()` help text, built on `rfl::parsing::schema` and the internal option table - `include/rfl/cli.hpp` — aggregator header diff --git a/include/rfl/cli.hpp b/include/rfl/cli.hpp index 72c208d5..8cedad7f 100644 --- a/include/rfl/cli.hpp +++ b/include/rfl/cli.hpp @@ -2,6 +2,7 @@ #define RFL_CLI_HPP_ #include "../rfl.hpp" +#include "cli/AllowAbbreviations.hpp" #include "cli/Parser.hpp" #include "cli/Reader.hpp" #include "cli/SeparateOptionValues.hpp" diff --git a/include/rfl/cli/AllowAbbreviations.hpp b/include/rfl/cli/AllowAbbreviations.hpp new file mode 100644 index 00000000..9ed37271 --- /dev/null +++ b/include/rfl/cli/AllowAbbreviations.hpp @@ -0,0 +1,51 @@ +#ifndef RFL_CLI_ALLOWABBREVIATIONS_HPP_ +#define RFL_CLI_ALLOWABBREVIATIONS_HPP_ + +#include + +#include "../Processors.hpp" + +namespace rfl::cli { + +/// A processor that turns on abbreviated long options in rfl::cli::read, +/// like argparse's allow_abbrev. With AllowAbbreviations, a long option may +/// be shortened to any prefix that matches exactly one option, so `--rep` +/// stands for `--report`. By default, a long option must be spelled out, and +/// `--rep` is an unknown option. +/// This is a marker type (doesn't modify data) that only rfl::cli::read looks +/// at. Other formats ignore it. +/// Usage: rfl::cli::read(argc, argv) +struct AllowAbbreviations { + public: + /// Identity process function - returns the named tuple unchanged. + /// @tparam StructType The struct type being processed + /// @param _named_tuple The named tuple representation of the struct + /// @return The same named tuple (unchanged) + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +namespace internal { + +template +struct allow_abbreviations : std::false_type {}; + +template <> +struct allow_abbreviations : std::true_type {}; + +template +struct allow_abbreviations> + : std::bool_constant<(allow_abbreviations::value || ...)> {}; + +/// Whether the processors contain rfl::cli::AllowAbbreviations. +template +constexpr bool allow_abbreviations_v = + allow_abbreviations>>::value; + +} // namespace internal + +} // namespace rfl::cli + +#endif diff --git a/include/rfl/cli/options.hpp b/include/rfl/cli/options.hpp index 91468e34..7f5b6d34 100644 --- a/include/rfl/cli/options.hpp +++ b/include/rfl/cli/options.hpp @@ -29,6 +29,7 @@ #include "../internal/no_extra_fields_v.hpp" #include "../internal/processed_t.hpp" #include "../parsing/is_string_map.hpp" +#include "AllowAbbreviations.hpp" #include "Parser.hpp" #include "SeparateOptionValues.hpp" #include "Reader.hpp" @@ -90,6 +91,10 @@ struct Options { /// Whether options that match no field are an error (rfl::NoExtraFields). bool reject_unknown = false; + /// Whether a long option that matches no field may be an abbreviation of + /// one (rfl::cli::AllowAbbreviations). + bool allow_abbreviations = false; + /// Whether short options can be grouped (`-qv`, `-fcsv`). make_options /// sets it. Without a table (the table-less parse_argv), `-abc` is the /// one short option `abc`. @@ -110,6 +115,31 @@ struct Options { /// struct fails. std::string duplicate_short_name; + /// Returns the long options that `--_prefix` could abbreviate: every + /// long option that starts with `_prefix`, in alphabetical order. Only the + /// last segment of a dotted name is abbreviated, so `_prefix` must spell + /// out the others and end inside the last one: `database.h` abbreviates + /// `database.host`, but `data` and `database.` do not. Keys of maps and of + /// rfl::ExtraFields are not known in advance, so they are never among + /// them. Only meaningful if find_long(_prefix) is unknown: an exact match + /// always wins. + std::vector find_abbreviations( + const std::string& _prefix + ) const { + std::vector names; + if (_prefix.empty() || _prefix.back() == path_separator) { + return names; + } + const auto last_separator = _prefix.rfind(path_separator); + for (auto it = long_options.lower_bound(_prefix); + it != long_options.end() && it->first.starts_with(_prefix); ++it) { + if (it->first.rfind(path_separator) == last_separator) { + names.push_back(it->first); + } + } + return names; + } + /// Returns the long option `--_name`. Option find_long(const std::string& _name) const { const auto it = long_options.find(_name); @@ -515,20 +545,24 @@ void collect_top_level_options(Options* _options) { /// @tparam ProcessorsType The processors applied to the struct /// @tparam _all_paths Whether to list every long option through nested /// structs, maps and rfl::ExtraFields, rather than only the top-level fields. -/// Only rfl::NoExtraFields, to reject unknown options, and +/// Only rfl::NoExtraFields, to reject unknown options, /// rfl::cli::SeparateOptionValues, to tell whether a nested option takes the -/// next token, need them all. +/// next token, and rfl::cli::AllowAbbreviations, to abbreviate nested +/// options, need them all. /// @return The options, with reject_unknown set if ProcessorsType contains -/// rfl::NoExtraFields, and separate_values if it contains -/// rfl::cli::SeparateOptionValues +/// rfl::NoExtraFields, separate_values if it contains +/// rfl::cli::SeparateOptionValues, and allow_abbreviations if it contains +/// rfl::cli::AllowAbbreviations template - || separate_option_values_v> + || separate_option_values_v + || allow_abbreviations_v> Options make_options() { Options options; options.group_short_options = true; options.reject_unknown = rfl::internal::no_extra_fields_v; options.separate_values = separate_option_values_v; + options.allow_abbreviations = allow_abbreviations_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 59cc4949..7607e810 100644 --- a/include/rfl/cli/parse_argv.hpp +++ b/include/rfl/cli/parse_argv.hpp @@ -60,6 +60,10 @@ namespace internal { /// takes the rest of the token (`-qfcsv`) or, if nothing is left, the next /// token (`-qf csv`). Without `_options` from make_options, short options /// are not grouped: `-abc` is the short option `abc`. +/// If `_options.allow_abbreviations` is set (rfl::cli::AllowAbbreviations), +/// a long option that matches no field stands for the one long option it is +/// a prefix of (`--rep` for `--report`), and a prefix of several is an +/// error. /// Every element of `_args` is parsed; nothing is skipped as the program name. /// @param _args The command-line arguments to parse /// @param _options The options of the target struct (see make_options) @@ -144,7 +148,18 @@ inline rfl::Result parse_string_views( if (key.empty()) { return error("Empty key in argument: " + std::string(arg_raw)); } - const auto option = _options.find_long(key); + auto option = _options.find_long(key); + if (option.kind == OptionKind::unknown && _options.allow_abbreviations) { + // --rep for --report: a prefix of exactly one long option. + const auto names = _options.find_abbreviations(key); + if (names.size() > 1) { + return error(internal::ambiguous_option_message(key, names)); + } + if (names.size() == 1) { + key = names.front(); + option = _options.find_long(key); + } + } if (option.kind == OptionKind::unknown && _options.reject_unknown) { return error("Unknown option '--" + key + "'."); } @@ -152,7 +167,8 @@ inline rfl::Result parse_string_views( && _options.separate_values) { // --key value (rfl::cli::SeparateOptionValues). A flag never takes // the next token: `--verbose 0` leaves "0" positional. - auto v = take_value(option, arg_raw, &i); + // The message names the option that `key` abbreviates. + auto v = take_value(option, "--" + key, &i); if (!v) { return error(v.error().what()); } val = std::move(*v); } diff --git a/include/rfl/cli/read.hpp b/include/rfl/cli/read.hpp index abcbc4ce..d59ae01f 100644 --- a/include/rfl/cli/read.hpp +++ b/include/rfl/cli/read.hpp @@ -63,7 +63,8 @@ rfl::Result parse_and_read(const ParseFn& _parse) { /// yourself: `std::span(argv, argc)` would parse the program name as an /// argument. /// @tparam T The struct type to parse into -/// @tparam Ps Optional processors to apply during parsing +/// @tparam Ps Optional processors to apply during parsing, including +/// settings such as rfl::NoExtraFields and rfl::cli::AllowAbbreviations /// @param _args The command-line arguments to parse /// @return A Result containing the parsed struct or an error template read(R&& _args) { /// argv[0] is skipped as the program name. See the sequence overload above /// for the supported syntax. /// @tparam T The struct type to parse into -/// @tparam Ps Optional processors to apply during parsing +/// @tparam Ps Optional processors to apply during parsing, including +/// settings such as rfl::NoExtraFields and rfl::cli::AllowAbbreviations /// @param argc Number of command-line arguments /// @param argv Array of command-line argument strings /// @return A Result containing the parsed struct or an error diff --git a/include/rfl/cli/resolve_args.hpp b/include/rfl/cli/resolve_args.hpp index 1bd79d9e..0e272485 100644 --- a/include/rfl/cli/resolve_args.hpp +++ b/include/rfl/cli/resolve_args.hpp @@ -123,13 +123,53 @@ inline rfl::Result> resolve_args( return std::move(result); } +/// The error for a long option that abbreviates several options, worded +/// like getopt_long's: "Option '--for' is ambiguous; possibilities: +/// '--force-export', '--format'." +inline std::string ambiguous_option_message( + const std::string& _key, const std::vector& _names +) { + auto msg = "Option '--" + _key + "' is ambiguous; possibilities: "; + for (size_t i = 0; i < _names.size(); ++i) { + msg += (i == 0 ? "'--" : ", '--") + _names[i] + "'"; + } + return msg + "."; +} + +/// Replaces the long options in `_named` that match no field by the one long +/// option they abbreviate (rfl::cli::AllowAbbreviations), as +/// parse_string_views does while parsing. +inline rfl::Result> expand_abbreviations( + std::map _named, const Options& _options) { + std::map expanded; + for (auto& [key, value] : _named) { + auto name = key; + if (_options.find_long(key).kind == OptionKind::unknown) { + const auto names = _options.find_abbreviations(key); + if (names.size() > 1) { + return rfl::error(ambiguous_option_message(key, names)); + } + if (names.size() == 1) { + name = names.front(); + } + } + if (!expanded.emplace(name, std::move(value)).second) { + return rfl::error("Duplicate argument: --" + name); + } + } + return expanded; +} + } // namespace internal /// Resolves ParsedArgs into a flat key-value map using compile-time /// metadata from the target struct T. Like the overload taking the options /// of T, but for arguments parsed by parse_argv without them: values that /// boolean short options took from the next token are returned to the -/// positional arguments. +/// positional arguments. With rfl::cli::AllowAbbreviations, long options +/// that match no field are expanded here (`--rep=a` for `--report=a`), but +/// parse_argv, which does not know the struct, has already read +/// `--rep value` as `--rep` and a positional argument. /// @tparam T The target struct type /// @tparam ProcessorsType The processors applied to the struct /// @param _parsed The parsed command-line arguments @@ -143,6 +183,14 @@ rfl::Result> resolve_args( if (!short_names) { return rfl::error(short_names.error().what()); } + if (options.allow_abbreviations) { + auto named = + internal::expand_abbreviations(std::move(_parsed.named), options); + if (!named) { + return rfl::error(named.error().what()); + } + _parsed.named = std::move(*named); + } internal::apply_option_kinds(options, &_parsed); return internal::resolve_args(std::move(_parsed), options); } diff --git a/tests/cli/test_abbreviations.cpp b/tests/cli/test_abbreviations.cpp new file mode 100644 index 00000000..cc47f199 --- /dev/null +++ b/tests/cli/test_abbreviations.cpp @@ -0,0 +1,276 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace test_abbreviations { + +template +rfl::Result read_exact(std::vector _args) { + _args.insert(_args.begin(), "program"); + return rfl::cli::read(static_cast(_args.size()), + const_cast(_args.data())); +} + +/// Reads with rfl::cli::AllowAbbreviations. +template +rfl::Result read(std::vector _args) { + return read_exact(std::move(_args)); +} + +template +std::string error_of(const rfl::Result& _result) { + return _result ? std::string("(no error)") + : std::string(_result.error().what()); +} + +struct Database { + std::optional host; + std::optional port; +}; + +struct Export { + std::optional format; + std::optional format_version; + std::optional force_export; + std::optional> report; + rfl::Short<"o", std::optional> output; + rfl::Positional> input_file; + std::optional database; + std::optional> labels; +}; + +TEST(cli, test_abbreviation_value) { + const auto result = read({"--rep=a,b"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().report, + (std::vector{"a", "b"})); + + // With rfl::cli::SeparateOptionValues, also from the next argument. + const auto separate = read( + {"--rep", "a", "in.txt"}); + ASSERT_TRUE(separate) << separate.error().what(); + EXPECT_EQ(separate.value().report, (std::vector{"a"})); + EXPECT_EQ(separate.value().input_file(), "in.txt"); +} + +TEST(cli, test_abbreviation_flag) { + const auto result = read({"--forc", "in.txt"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().force_export, true); + EXPECT_EQ(result.value().input_file(), "in.txt"); + + // Like `--force-export`, an abbreviated long flag never takes the next + // token, even if it is a bool literal. + const auto with_value = read({"--force-e", "false"}); + ASSERT_TRUE(with_value) << with_value.error().what(); + EXPECT_EQ(with_value.value().force_export, true); + EXPECT_EQ(with_value.value().input_file(), "false"); + + const auto with_equals = read({"--forc=false"}); + ASSERT_TRUE(with_equals) << with_equals.error().what(); + EXPECT_EQ(with_equals.value().force_export, false); +} + +TEST(cli, test_abbreviation_exact_match_wins) { + const auto result = read({"--format=csv", "--format-v=2"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().format, "csv"); + EXPECT_EQ(result.value().format_version, "2"); +} + +TEST(cli, test_abbreviation_ambiguous) { + for (const auto& args : std::vector>{ + {"--form", "csv"}, {"--form=csv"}, {"in.txt", "--form"}}) { + const auto result = read(args); + EXPECT_EQ(error_of(result), + "Option '--form' is ambiguous; possibilities: '--format', " + "'--format-version'."); + } + // Every candidate is listed. + EXPECT_EQ(error_of(read({"--for=x"})), + "Option '--for' is ambiguous; possibilities: '--force-export', " + "'--format', '--format-version'."); + // Past the end of `--format`, only `--format-version` is left. + const auto unique = read({"--format-=1"}); + ASSERT_TRUE(unique) << unique.error().what(); + EXPECT_EQ(unique.value().format_version, "1"); +} + +TEST(cli, test_abbreviation_nested) { + const auto result = + read({"--database.h=db", "--database.p=5432"}); + ASSERT_TRUE(result) << result.error().what(); + ASSERT_TRUE(result.value().database); + EXPECT_EQ(result.value().database->host, "db"); + EXPECT_EQ(result.value().database->port, 5432); + + // Only the last part of a dotted name is shortened, so the struct's name + // cannot be, and an abbreviation cannot end at or inside it. + const auto inner = read({"--datab.h=db"}); + EXPECT_EQ(error_of(inner), "Unknown option '--datab.h'."); + const auto parent = read({"--data=x"}); + EXPECT_EQ(error_of(parent), "Unknown option '--data'."); + const auto dot = read({"--database.=x"}); + EXPECT_EQ(error_of(dot), "Unknown option '--database.'."); +} + +TEST(cli, test_abbreviation_positional_and_short_fields) { + const auto result = read({"--inp=in.txt", "--out=o.csv"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().input_file(), "in.txt"); + EXPECT_EQ(result.value().output(), "o.csv"); + + EXPECT_EQ(error_of(read({"--inp=a", "b"})), + "Conflict: positional argument and '--input-file' both provided."); + EXPECT_EQ(error_of(read({"-o", "a", "--outp=b"})), + "Conflict: '-o' and '--output' both provided."); +} + +TEST(cli, test_abbreviation_duplicate) { + const auto result = read({"--format-v=a", "--format-version=b"}); + EXPECT_EQ(error_of(result), "Duplicate argument: --format-version=b"); + const auto both_short = + read({"--format-v=a", "--format-ver", "b"}); + EXPECT_EQ(error_of(both_short), "Duplicate argument: --format-ver"); +} + +TEST(cli, test_abbreviation_missing_value_names_the_option) { + const auto result = + read({"in.txt", "--rep"}); + EXPECT_EQ(error_of(result), "Missing value for option '--report'."); +} + +TEST(cli, test_abbreviation_never_into_map_keys) { + // Map keys are not known in advance: `--labels.e` is the key "e". + const auto key = read({"--labels.e=prod"}); + ASSERT_TRUE(key) << key.error().what(); + EXPECT_EQ(key.value().labels, + (std::map{{"e", "prod"}})); + + // The map's own name is not abbreviated either. + const auto map_name = read({"--lab.env=prod"}); + EXPECT_EQ(error_of(map_name), "Unknown option '--lab.env'."); +} + +TEST(cli, test_abbreviation_no_match) { + // Without rfl::NoExtraFields an unknown option is ignored. + const auto lenient = read({"--nope", "in.txt"}); + ASSERT_TRUE(lenient) << lenient.error().what(); + EXPECT_EQ(lenient.value().input_file(), "in.txt"); + EXPECT_FALSE(lenient.value().report); + + const auto strict = read({"--reps=a"}); + EXPECT_EQ(error_of(strict), "Unknown option '--reps'."); +} + +TEST(cli, test_abbreviation_no_extra_fields) { + const auto result = + read({"--rep=a", "--forc"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().report, (std::vector{"a"})); + EXPECT_EQ(result.value().force_export, true); +} + +TEST(cli, test_abbreviation_only_for_long_options) { + const auto separated = read({"--", "--rep"}); + ASSERT_TRUE(separated) << separated.error().what(); + EXPECT_FALSE(separated.value().report); + EXPECT_EQ(separated.value().input_file(), "--rep"); + + // A single dash starts short options, never an abbreviated long option. + const auto result = read({"-out.csv"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().output(), "ut.csv"); + const auto unknown = read({"-rep"}); + EXPECT_EQ(error_of(unknown), "Unknown short argument: -r (in '-rep')."); +} + +TEST(cli, test_no_abbreviations_by_default) { + const auto ignored = read_exact({"--rep=a", "--for", "in.txt"}); + ASSERT_TRUE(ignored) << ignored.error().what(); + EXPECT_FALSE(ignored.value().report); + EXPECT_FALSE(ignored.value().force_export); + EXPECT_EQ(ignored.value().input_file(), "in.txt"); + + const auto exact = read_exact({"--report=a", "--format=x"}); + ASSERT_TRUE(exact) << exact.error().what(); + EXPECT_EQ(exact.value().report, (std::vector{"a"})); + EXPECT_EQ(exact.value().format, "x"); + + for (const auto& args : std::vector>{ + {"--rep=a"}, {"--rep", "a"}}) { + const auto result = read_exact(args); + EXPECT_EQ(error_of(result), "Unknown option '--rep'."); + } + EXPECT_EQ(error_of(read_exact({"--for"})), + "Unknown option '--for'."); + // The processors can come in any order. + const auto other_order = + read_exact( + {"--rep=a"}); + ASSERT_TRUE(other_order) << other_order.error().what(); + EXPECT_EQ(other_order.value().report, (std::vector{"a"})); +} + +TEST(cli, test_abbreviation_resolve_args) { + // The two-step API expands abbreviations, too, but parse_argv does not + // know the struct, so the value must follow '='. + const char* args[] = {"program", "--rep=a", "--forc", "--database.h=db"}; + const auto result = + rfl::cli::parse_argv(4, const_cast(args)) + .and_then(rfl::cli::resolve_args< + Export, rfl::Processors>); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().at("report"), "a"); + EXPECT_EQ(result.value().at("force-export"), ""); + EXPECT_EQ(result.value().at("database.host"), "db"); + + const char* ambiguous[] = {"program", "--for=x"}; + const auto error = + rfl::cli::parse_argv(2, const_cast(ambiguous)) + .and_then(rfl::cli::resolve_args< + Export, rfl::Processors>); + EXPECT_EQ(error_of(error), + "Option '--for' is ambiguous; possibilities: '--force-export', " + "'--format', '--format-version'."); +} + +struct Renamed { + rfl::Rename<"outDir", std::optional> out_dir; + std::optional dry_run; +}; + +TEST(cli, test_abbreviation_renamed_and_kebab_case) { + // A field has one long name: rfl::Rename replaces it. + const auto result = read({"--out=x", "--dry"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().out_dir(), "x"); + EXPECT_EQ(result.value().dry_run, true); + + const auto old_name = read({"--out-d=x"}); + EXPECT_EQ(error_of(old_name), "Unknown option '--out-d'."); +} + +struct WithExtra { + std::optional report; + rfl::ExtraFields extra; +}; + +TEST(cli, test_abbreviation_not_with_extra_fields) { + // Under rfl::ExtraFields every name is a key of its own, so nothing is + // abbreviated. + const auto result = read({"--rep=a", "--report=b"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().report, "b"); + EXPECT_EQ(result.value().extra.at("rep"), "a"); +} + +} // namespace test_abbreviations