From 4f0ce95250f051e193a45f42d5b947370c3e3d27 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Sat, 14 Nov 2026 11:26:08 +0100 Subject: [PATCH] Find rfl::Short and rfl::Positional through other wrappers in rfl::cli rfl::cli only recognized rfl::Short and rfl::Positional as the outermost wrapper of a field, so `rfl::Description<"...", rfl::Short<"o", T>>` had no `-o` and `std::optional>` was not positional. The option table now finds them through DefaultVal, std::optional, Description, Rename and Deprecated, in any order. Every Short and Positional counts wherever it sits: positional fields take bare arguments in declaration order, two fields with the same short name are an error, and a field with more than one Short or Positional does not compile. Co-Authored-By: Claude Opus 5.5 --- docs/content/supported_formats/cli.md | 19 ++- include/rfl/cli/options.hpp | 84 +++++++---- tests/cli/test_wrapper_order.cpp | 191 ++++++++++++++++++++++++++ 3 files changed, 265 insertions(+), 29 deletions(-) create mode 100644 tests/cli/test_wrapper_order.cpp diff --git a/docs/content/supported_formats/cli.md b/docs/content/supported_formats/cli.md index dcb3ed14..fa94eede 100644 --- a/docs/content/supported_formats/cli.md +++ b/docs/content/supported_formats/cli.md @@ -133,10 +133,21 @@ looks like an option (`-` followed by anything except a digit or `.`): `Missing value for option '-o'.` Use `=` to pass an empty value or one that starts with `-`: `-o=`, `-o=-x`. -## Combining Positional and Short - -`Positional` and `Short` can be used together in the same struct, but -**cannot be nested** (`Positional>` is a compile-time error): +## Combining wrappers + +`Short` and `Positional` can be nested with `DefaultVal`, `std::optional`, +`Description`, `Rename` and `Deprecated` in any order: +`rfl::Description<"...", rfl::Short<"o", T>>` is `-o`, and +`rfl::DefaultVal>` is positional. Put `rfl::Rename` +outermost, as in `rfl::Rename<"out", rfl::Short<"o", T>>`; inside other +wrappers it is ignored. + +Bare arguments go to the `Positional` fields in declaration order. Two fields +with the same `Short` name make every read fail with +`Duplicate short name '-o' in struct definition.` A field with more than one +`Short` or `Positional`, such as `rfl::Positional>` or +`rfl::Short<"o", rfl::Description<"...", rfl::Positional>>`, is a +compile-time error: ```cpp struct Config { diff --git a/include/rfl/cli/options.hpp b/include/rfl/cli/options.hpp index 7dc338b8..21149f65 100644 --- a/include/rfl/cli/options.hpp +++ b/include/rfl/cli/options.hpp @@ -10,8 +10,12 @@ #include #include +#include "../DefaultVal.hpp" +#include "../Deprecated.hpp" +#include "../Description.hpp" #include "../Generic.hpp" #include "../Positional.hpp" +#include "../Rename.hpp" #include "../Short.hpp" #include "../Tuple.hpp" #include "../internal/StringLiteral.hpp" @@ -62,8 +66,9 @@ struct Options { /// of their field. std::map short_options; - /// The paths of the rfl::Positional fields, in declaration order. Each - /// takes one positional argument. + /// The paths of the rfl::Positional fields, in declaration order, wherever + /// rfl::Positional sits among a field's wrappers. Each takes one positional + /// argument. std::vector positionals; /// Whether tokens such as `-5` and `-.5` are short options rather than @@ -99,20 +104,52 @@ inline bool is_bool_literal(std::string_view _token) noexcept { || _token == "0"; } -/// Whether T is rfl::Short directly around rfl::Positional, or the other way -/// round, which is a compile-time error. +/// What rfl::Short and rfl::Positional say about a field of type T. They are +/// found through rfl::DefaultVal, std::optional, rfl::Description, +/// rfl::Deprecated and rfl::Rename, in any order. `count` is the number of +/// rfl::Short and rfl::Positional wrappers; a field may have at most one. template -consteval bool is_short_around_positional() { - if constexpr (rfl::internal::is_short_v) { - return rfl::internal::is_positional_v; - } - else if constexpr (rfl::internal::is_positional_v) { - return rfl::internal::is_short_v; - } - else { - return false; - } -} +struct cli_annotations { + static constexpr bool is_short = false; + static constexpr bool is_positional = false; + static constexpr rfl::internal::StringLiteral<1> short_name = ""; + static constexpr int count = 0; +}; + +template +using cli_annotations_t = cli_annotations>; + +template +struct cli_annotations> : cli_annotations_t {}; + +template +struct cli_annotations> : cli_annotations_t {}; + +template +struct cli_annotations> : cli_annotations_t {}; + +template +struct cli_annotations> + : cli_annotations_t {}; + +template +struct cli_annotations> : cli_annotations_t {}; + +template +struct cli_annotations> : cli_annotations_t { + static constexpr bool is_short = false; + static constexpr bool is_positional = true; + static constexpr int count = cli_annotations_t::count + 1; +}; + +template +struct cli_annotations> : cli_annotations_t { + static constexpr bool is_short = true; + static constexpr bool is_positional = false; + static constexpr auto short_name = _name; + static constexpr int count = cli_annotations_t::count + 1; +}; /// The CLI metadata of the _i-th field of a NamedTuple. InnerType is the /// declared type of the field, whichever wrappers it consists of. @@ -121,17 +158,14 @@ struct field_info { using FieldType = rfl::tuple_element_t<_i, typename NamedTupleType::Fields>; using InnerType = typename FieldType::Type; using OuterType = std::remove_pointer_t>; + using Annotations = cli_annotations_t; - /// Whether rfl::Positional is the outermost wrapper of the field. - static constexpr bool is_positional = - rfl::internal::is_positional_v; - /// Whether rfl::Short is the outermost wrapper of the field. - static constexpr bool is_short = rfl::internal::is_short_v; + static constexpr bool is_positional = Annotations::is_positional; + static constexpr bool is_short = Annotations::is_short; - static_assert( - !is_short_around_positional(), - "Nested wrappers (Positional> or Short<..., Positional<...>>) " - "are not allowed."); + static_assert(Annotations::count <= 1, + "A field can have only one rfl::Short or rfl::Positional, " + "wherever it sits among the field's other wrappers."); static constexpr std::string_view name() { return FieldType::name(); } }; @@ -141,7 +175,7 @@ template constexpr auto get_short_name() { using Info = field_info; if constexpr (Info::is_short) { - return Info::OuterType::short_name_; + return Info::Annotations::short_name; } else { return rfl::internal::StringLiteral<1>(""); diff --git a/tests/cli/test_wrapper_order.cpp b/tests/cli/test_wrapper_order.cpp new file mode 100644 index 00000000..794da374 --- /dev/null +++ b/tests/cli/test_wrapper_order.cpp @@ -0,0 +1,191 @@ +#include + +#include +#include +#include +#include +#include + +// rfl::Short and rfl::Positional work through rfl::DefaultVal, std::optional, +// rfl::Description, rfl::Rename and rfl::Deprecated, in either order. +namespace test_wrapper_order { + +template +rfl::Result read(std::vector _args) { + _args.insert(_args.begin(), "program"); + return rfl::cli::read(static_cast(_args.size()), + const_cast(_args.data())); +} + +struct Shorts { + rfl::DefaultVal> output = std::string("o"); + rfl::Short<"O", std::optional> other_output; + std::optional> format; + rfl::Short<"F", std::optional> other_format; + rfl::Description<"Log file", rfl::Short<"l", std::optional>> + log; + rfl::Short<"L", rfl::Description<"Level", std::optional>> level; + rfl::Rename<"outDir", rfl::Short<"d", std::optional>> out_dir; + rfl::Deprecated<"Use -j.", "Threads", rfl::Short<"t", std::optional>> + threads; + rfl::Short<"j", rfl::Deprecated<"", "Jobs", std::optional>> jobs; + rfl::Description<"Title", std::optional>> + title; +}; + +TEST(cli, test_short_through_wrappers_given) { + const auto result = read( + {"-o", "a", "-O", "b", "-f", "c", "-F", "d", "-l", "e", "-L", "4", "-d", + "f", "-t", "5", "-j", "6", "-T", "g"}); + ASSERT_TRUE(result) << result.error().what(); + const auto& s = result.value(); + EXPECT_EQ(s.output().value(), "a"); + EXPECT_EQ(s.other_output().value(), "b"); + EXPECT_EQ(s.format.value().value(), "c"); + EXPECT_EQ(s.other_format().value(), "d"); + EXPECT_EQ(s.log().value().value(), "e"); + EXPECT_EQ(s.level().value().value(), 4); + EXPECT_EQ(s.out_dir().value().value(), "f"); + EXPECT_EQ(s.threads().value().value(), 5); + EXPECT_EQ(s.jobs().value().value(), 6); + EXPECT_EQ(s.title().value().value(), "g"); +} + +TEST(cli, test_short_through_wrappers_absent) { + const auto result = read({}); + ASSERT_TRUE(result) << result.error().what(); + const auto& s = result.value(); + EXPECT_EQ(s.output().value(), "o"); + EXPECT_FALSE(s.other_output().has_value()); + EXPECT_FALSE(s.format.has_value()); + EXPECT_FALSE(s.other_format().has_value()); + EXPECT_FALSE(s.log().value().has_value()); + EXPECT_FALSE(s.level().value().has_value()); + EXPECT_FALSE(s.out_dir().value().has_value()); + EXPECT_FALSE(s.threads().value().has_value()); + EXPECT_FALSE(s.jobs().value().has_value()); + EXPECT_FALSE(s.title().has_value()); +} + +TEST(cli, test_short_through_wrappers_conflict_with_long) { + const auto result = read({"-d", "a", "--outDir=b"}); + EXPECT_FALSE(result); +} + +// Positional fields take bare arguments in declaration order, wherever +// rfl::Positional sits among their wrappers. +struct Positionals { + rfl::DefaultVal> first = std::string("1"); + rfl::Positional> second; + rfl::Description<"Third", rfl::Positional>> third; + rfl::Positional>> + fourth; + std::optional> fifth; + rfl::Rename<"sixth", rfl::Positional>> sixth_; + rfl::Deprecated<"", "Seventh", rfl::Positional>> + seventh; + std::optional verbose; +}; + +TEST(cli, test_positional_through_wrappers_given) { + const auto result = read( + {"a", "b", "c", "--verbose", "d", "e", "f", "g"}); + ASSERT_TRUE(result) << result.error().what(); + const auto& p = result.value(); + EXPECT_EQ(p.first().value(), "a"); + EXPECT_EQ(p.second().value(), "b"); + EXPECT_EQ(p.third().value().value(), "c"); + EXPECT_EQ(p.fourth().value().value(), "d"); + EXPECT_EQ(p.fifth.value().value(), "e"); + EXPECT_EQ(p.sixth_().value().value(), "f"); + EXPECT_EQ(p.seventh().value().value(), "g"); + EXPECT_EQ(p.verbose, true); +} + +TEST(cli, test_positional_through_wrappers_absent) { + const auto result = read({"a"}); + ASSERT_TRUE(result) << result.error().what(); + const auto& p = result.value(); + EXPECT_EQ(p.first().value(), "a"); + EXPECT_FALSE(p.second().has_value()); + EXPECT_FALSE(p.third().value().has_value()); + EXPECT_FALSE(p.fifth.has_value()); + EXPECT_FALSE(p.seventh().value().has_value()); + + const auto none = read({}); + ASSERT_TRUE(none) << none.error().what(); + EXPECT_EQ(none.value().first().value(), "1"); + EXPECT_FALSE(none.value().second().has_value()); +} + +TEST(cli, test_positional_through_wrappers_by_name) { + const auto result = read({"--sixth=f", "a"}); + ASSERT_TRUE(result) << result.error().what(); + EXPECT_EQ(result.value().first().value(), "a"); + EXPECT_EQ(result.value().sixth_().value().value(), "f"); +} + +struct ShortFlags { + rfl::Short<"q", rfl::DefaultVal> quiet = false; + rfl::Short<"v", std::optional> verbose; + rfl::Description<"Dry run", rfl::Short<"n", bool>> dry_run; + rfl::DefaultVal> force = false; + rfl::Positional> input; + rfl::Positional> output; +}; + +TEST(cli, test_short_flags_through_wrappers_do_not_consume_token) { + for (const char* flag : {"-q", "-v", "-n", "-f"}) { + std::vector args = {flag, "in.rep"}; + if (std::string(flag) != "-n") { + args.push_back("--dry-run=false"); + } + if (std::string(flag) != "-q") { + args.push_back("--quiet=false"); + } + const auto result = read(args); + ASSERT_TRUE(result) << flag << ": " << result.error().what(); + EXPECT_EQ(result.value().input().value(), "in.rep") << flag; + } + + const auto result = + read({"-q", "-v", "in.rep", "-n", "out.csv", "-f"}); + ASSERT_TRUE(result) << result.error().what(); + const auto& f = result.value(); + EXPECT_TRUE(f.quiet().value()); + EXPECT_EQ(f.verbose(), true); + EXPECT_TRUE(f.dry_run().value()); + EXPECT_TRUE(f.force().value()); + EXPECT_EQ(f.input().value(), "in.rep"); + EXPECT_EQ(f.output().value(), "out.csv"); +} + +TEST(cli, test_short_flags_through_wrappers_take_bool_literal) { + const auto result = + read({"-q", "true", "-v", "0", "-n", "false", "-f=1", "x"}); + ASSERT_TRUE(result) << result.error().what(); + const auto& f = result.value(); + EXPECT_TRUE(f.quiet().value()); + EXPECT_EQ(f.verbose(), false); + EXPECT_FALSE(f.dry_run().value()); + EXPECT_TRUE(f.force().value()); + EXPECT_EQ(f.input().value(), "x"); +} + +// Two fields with the same short name fail every read, wherever rfl::Short +// sits among their wrappers. A field with two rfl::Short or rfl::Positional +// wrappers, such as rfl::Short<"o", rfl::Description<"...", +// rfl::Positional>>, does not compile. +struct WrappedAndDirectShort { + rfl::Description<"Name", rfl::Short<"o", std::string>> name; + rfl::Short<"o", int> count; +}; + +TEST(cli, test_wrapped_duplicate_short_name) { + const auto result = read({"--count=5", "--name=x"}); + ASSERT_FALSE(result); + EXPECT_EQ(std::string(result.error().what()), + "Duplicate short name '-o' in struct definition."); +} + +} // namespace test_wrapper_order