From 20f3b2bb01a842a997ece5786e25c8f7cabd4ac6 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 08:24:11 +0100 Subject: [PATCH] Flatten nested rfl::Processors bundles in cli and generic entry points --- include/rfl/Processors.hpp | 26 +++++++++ include/rfl/cli/read.hpp | 3 +- include/rfl/cli/read_app.hpp | 6 ++- include/rfl/cli/resolve_args.hpp | 3 +- include/rfl/cli/usage.hpp | 6 ++- include/rfl/generic/read.hpp | 2 +- include/rfl/generic/write.hpp | 3 +- tests/cli/test_processor_bundles.cpp | 79 ++++++++++++++++++++++++++++ 8 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 tests/cli/test_processor_bundles.cpp diff --git a/include/rfl/Processors.hpp b/include/rfl/Processors.hpp index c693dba8d..fc53ac7ec 100644 --- a/include/rfl/Processors.hpp +++ b/include/rfl/Processors.hpp @@ -42,6 +42,32 @@ struct Processors { } }; +namespace internal { + +template +struct flatten_processors; + +template +struct flatten_processors> { + using type = Processors; +}; + +template +struct flatten_processors, Processors, Rest...> + : flatten_processors, Inner..., Rest...> {}; + +template +struct flatten_processors, Head, Rest...> + : flatten_processors, Rest...> {}; + +/// Flattens arbitrarily nested rfl::Processors<...> bundles into a single +/// flat rfl::Processors<...> list, preserving left-to-right order. +template +using flatten_processors_t = + typename flatten_processors, Ps...>::type; + +} // namespace internal + } // namespace rfl #endif diff --git a/include/rfl/cli/read.hpp b/include/rfl/cli/read.hpp index 6cb7ed252..a22fde487 100644 --- a/include/rfl/cli/read.hpp +++ b/include/rfl/cli/read.hpp @@ -47,7 +47,8 @@ rfl::Result read_parsed(ParsedArgs _parsed, const Options& _options) { /// (see make_options) and returns ParsedArgs, and turns them into a struct. template rfl::Result parse_and_read(const ParseFn& _parse) { - using ProcessorsType = Processors; + using ProcessorsType = + rfl::internal::flatten_processors_t; static_assert(!internal::has_app_v, "rfl::cli::App turns on --help and --version, which " "rfl::cli::read cannot report. Call rfl::cli::read_app " diff --git a/include/rfl/cli/read_app.hpp b/include/rfl/cli/read_app.hpp index 4ab5cb336..9dc4c56ef 100644 --- a/include/rfl/cli/read_app.hpp +++ b/include/rfl/cli/read_app.hpp @@ -52,7 +52,8 @@ template rfl::Result> parse_and_read_app( const std::string& _program_name, const ParseFn& _parse, const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) { - using ProcessorsType = Processors; + using ProcessorsType = + rfl::internal::flatten_processors_t; using ResultType = std::variant; auto options = make_options(); const auto builtin = add_builtin_options(&options); @@ -202,7 +203,8 @@ template > read_app( R&& _args, const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) { - using ProcessorsType = Processors; + using ProcessorsType = + rfl::internal::flatten_processors_t; return read_app(internal::app_settings_t::name, std::forward(_args), _usage_opts); } diff --git a/include/rfl/cli/resolve_args.hpp b/include/rfl/cli/resolve_args.hpp index f43ba2046..abc6ad90f 100644 --- a/include/rfl/cli/resolve_args.hpp +++ b/include/rfl/cli/resolve_args.hpp @@ -227,7 +227,8 @@ rfl::Result> resolve_args( return rfl::error("Duplicate argument: --" + _parsed.later_occurrences.begin()->first); } - const auto options = internal::make_options(); + using FlatProcessors = rfl::internal::flatten_processors_t; + const auto options = internal::make_options(); const auto short_names = internal::check_short_names(options); if (!short_names) { return rfl::error(short_names.error().what()); diff --git a/include/rfl/cli/usage.hpp b/include/rfl/cli/usage.hpp index 2a80e10b9..252c80dee 100644 --- a/include/rfl/cli/usage.hpp +++ b/include/rfl/cli/usage.hpp @@ -804,7 +804,8 @@ inline std::string format_usage(const std::string& _program_name, /// default-constructible, which read requires, too. template std::optional usage_defaults() { - using ProcessorsType = Processors; + using ProcessorsType = + rfl::internal::flatten_processors_t; if constexpr (rfl::internal::default_if_missing_v && std::is_default_constructible_v) { try { @@ -844,7 +845,8 @@ std::optional usage_defaults() { template rfl::Result usage(const std::string& _program_name, const UsageOptions& _opts) { - using ProcessorsType = Processors; + using ProcessorsType = + rfl::internal::flatten_processors_t; using NamedTupleType = rfl::internal::processed_t; constexpr auto delimiter = internal::list_delimiter_v; // The table that read() parses with, so the help shows what the parser diff --git a/include/rfl/generic/read.hpp b/include/rfl/generic/read.hpp index 6d1a4c36f..aa301875e 100644 --- a/include/rfl/generic/read.hpp +++ b/include/rfl/generic/read.hpp @@ -19,7 +19,7 @@ namespace generic { template auto read(const Generic& _g) { const auto r = Reader(); - return Parser>::read(r, _g); + return Parser>::read(r, _g); } } // namespace generic diff --git a/include/rfl/generic/write.hpp b/include/rfl/generic/write.hpp index f6668ce8a..b8824493f 100644 --- a/include/rfl/generic/write.hpp +++ b/include/rfl/generic/write.hpp @@ -19,7 +19,8 @@ Generic write(const auto& _t) { using T = std::remove_cvref_t; using ParentType = parsing::Parent; auto w = Writer(); - Parser>::write(w, _t, typename ParentType::Root{}); + Parser>::write( + w, _t, typename ParentType::Root{}); return w.root(); } diff --git a/tests/cli/test_processor_bundles.cpp b/tests/cli/test_processor_bundles.cpp new file mode 100644 index 000000000..0a2aeed7e --- /dev/null +++ b/tests/cli/test_processor_bundles.cpp @@ -0,0 +1,79 @@ +#include + +#include +#include +#include +#include +#include +#include + +namespace test_processor_bundles { + +using BaseCli = rfl::Processors>; +using FullCli = + rfl::Processors>; +using AppBundle = + rfl::Processors, FullCli>; + +static_assert( + std::is_same_v< + rfl::internal::flatten_processors_t, + rfl::Processors, rfl::cli::PureFlags>>); + +struct Config { + rfl::Short<"v", rfl::DefaultVal> verbose = false; + std::optional output; + std::optional> items; + rfl::Positional>> ids; +}; + +TEST(cli, test_processor_bundle_read) { + const std::vector args = { + "--output", "out.txt", "--items", "a,b", "-v", "1", "2"}; + const auto res = rfl::cli::read(args); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(rfl::unwrap(res->verbose)); + EXPECT_EQ(res->output, "out.txt"); + EXPECT_EQ(*res->items, (std::vector{"a,b"})); + EXPECT_EQ(rfl::unwrap(res->ids), (std::vector{"1", "2"})); + + const std::vector bad_args = {"--unknown"}; + const auto err = rfl::cli::read(bad_args); + EXPECT_FALSE(err); +} + +TEST(cli, test_processor_bundle_read_app_and_usage) { + const std::vector help_args = {"--help"}; + const auto res = rfl::cli::read_app(help_args); + ASSERT_TRUE(res) << res.error().what(); + const auto* info = std::get_if(&*res); + ASSERT_NE(info, nullptr); + EXPECT_EQ(info->kind, rfl::cli::HelpOrVersion::Kind::help); + EXPECT_NE(info->text.find("demo 2.0"), std::string::npos); + // PureFlags inside the nested bundle takes effect in usage(). + EXPECT_EQ(info->text.find("[=]"), std::string::npos) << info->text; + EXPECT_EQ(info->text.find("Default: false."), std::string::npos) + << info->text; + + const auto direct = rfl::cli::usage("demo", true); + ASSERT_TRUE(direct) << direct.error().what(); + EXPECT_EQ(*direct, info->text); +} + +TEST(cli, test_processor_bundle_default_if_missing) { + struct Simple { + int count = 7; + bool flag = false; + }; + using Bundle = rfl::Processors; + const auto text = rfl::cli::usage("simple", true); + ASSERT_TRUE(text) << text.error().what(); + EXPECT_NE(text->find("Default: 7."), std::string::npos) << *text; + EXPECT_EQ(text->find("Default: false."), std::string::npos) << *text; +} + +} // namespace test_processor_bundles