From 5a61f5b6ce7e13084bbf40f6413440efb7e786ab Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 08:22:33 +0100 Subject: [PATCH] Add rfl::Group wrapper for named help sections in rfl::cli::usage --- include/rfl.hpp | 1 + include/rfl/Group.hpp | 52 ++++++++++++++++++++++++++++++ include/rfl/cli/options.hpp | 16 ++++++++++ include/rfl/cli/usage.hpp | 52 ++++++++++++++++++++++++++---- tests/cli/test_group.cpp | 63 +++++++++++++++++++++++++++++++++++++ 5 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 include/rfl/Group.hpp create mode 100644 tests/cli/test_group.cpp diff --git a/include/rfl.hpp b/include/rfl.hpp index b56864c05..825544c50 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -29,6 +29,7 @@ #include "rfl/Field.hpp" #include "rfl/Flatten.hpp" #include "rfl/Generic.hpp" +#include "rfl/Group.hpp" #include "rfl/Hex.hpp" #include "rfl/Hidden.hpp" #include "rfl/ImplicitValue.hpp" diff --git a/include/rfl/Group.hpp b/include/rfl/Group.hpp new file mode 100644 index 000000000..045737733 --- /dev/null +++ b/include/rfl/Group.hpp @@ -0,0 +1,52 @@ +#ifndef RFL_GROUP_HPP_ +#define RFL_GROUP_HPP_ + +#include "internal/StringLiteral.hpp" +#include "internal/TransparentWrapper.hpp" + +namespace rfl { + +/// Assigns a CLI option to a named help section (e.g. "OUTPUT OPTIONS") in +/// `rfl::cli::usage` and `rfl::cli::read_app`. +/// For non-CLI formats (JSON, YAML, etc.), this is transparent. +template +struct Group : internal::TransparentWrapper, T> { + using Base = internal::TransparentWrapper, T>; + using Base::Base; + using Base::operator=; + + static constexpr auto group_name_ = _group; + static_assert(_group.length > 0, "Group name must not be empty."); + + Group() + requires std::is_default_constructible_v + = default; + Group(const Group&) = default; + Group(Group&&) = default; + Group& operator=(const Group&) = default; + Group& operator=(Group&&) = default; + + template + Group(const Group<_group, U>& _field) : Base(_field.get()) {} + + template + Group(Group<_group, U>&& _field) noexcept( + std::is_nothrow_constructible_v) + : Base(std::move(_field.value_)) {} + + template + auto& operator=(const Group<_group, U>& _field) { + this->value_ = _field.get(); + return *this; + } + + template + auto& operator=(Group<_group, U>&& _field) { + this->value_ = std::move(_field.value_); + return *this; + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/cli/options.hpp b/include/rfl/cli/options.hpp index 405e23f1b..4d7a4d0eb 100644 --- a/include/rfl/cli/options.hpp +++ b/include/rfl/cli/options.hpp @@ -15,6 +15,7 @@ #include "../DefaultVal.hpp" #include "../Deprecated.hpp" #include "../Description.hpp" +#include "../Group.hpp" #include "../Hidden.hpp" #include "../ImplicitValue.hpp" #include "../Positional.hpp" @@ -112,6 +113,9 @@ struct Options { /// omitted from usage() and --help. std::set hidden_options; + /// Section header name for fields wrapped in rfl::Group, by path. + std::map option_groups; + /// Whether options that match no field are an error (rfl::NoExtraFields). bool reject_unknown = false; @@ -272,10 +276,12 @@ struct cli_annotations { static constexpr bool is_short = false; static constexpr bool is_positional = false; static constexpr bool is_hidden = false; + static constexpr bool has_group = false; static constexpr size_t num_implicit_value = 0; static constexpr rfl::internal::StringLiteral<1> short_name = ""; static constexpr int count = 0; static constexpr rfl::internal::StringLiteral<1> implicit_value = ""; + static constexpr rfl::internal::StringLiteral<1> group_name = ""; }; template @@ -325,6 +331,12 @@ struct cli_annotations> : cli_annotations_t { static constexpr bool is_hidden = true; }; +template +struct cli_annotations> : cli_annotations_t { + static constexpr bool has_group = true; + static constexpr auto group_name = _group; +}; + /// The CLI metadata of the _i-th field of a NamedTuple. InnerType is the /// declared type of the field, whichever wrappers it consists of. template @@ -560,6 +572,10 @@ void add_option(const std::string& _path, Options* _options) { if constexpr (cli_annotations_t::is_hidden) { _options->hidden_options.insert(_path); } + if constexpr (cli_annotations_t::has_group) { + _options->option_groups[_path] = + std::string(cli_annotations_t::group_name.string_view()); + } if constexpr (cli_annotations_t::num_implicit_value > 0) { _options->long_options[_path] = implicit_value_option(); diff --git a/include/rfl/cli/usage.hpp b/include/rfl/cli/usage.hpp index 25352df9b..9c5c2d1ca 100644 --- a/include/rfl/cli/usage.hpp +++ b/include/rfl/cli/usage.hpp @@ -84,6 +84,8 @@ struct UsageArg { /// Rendered constraints, such as "Range: 1..65535.". std::vector constraints; std::optional default_value; + /// Optional help section header (from rfl::Group). + std::string group; }; template @@ -499,6 +501,10 @@ class UsageCollector { _arg.default_value = std::move(def); } } + if (const auto it = options_->option_groups.find(_arg.name); + it != options_->option_groups.end()) { + _arg.group = it->second; + } args_.emplace_back(std::move(_arg)); } @@ -678,12 +684,12 @@ inline std::string format_usage(const std::string& _program_name, constexpr size_t indent = 2; constexpr size_t gap = 2; const auto continuation = '\n' + std::string(indent + width + gap, ' '); - out += "\nArguments:\n"; - for (const auto& [left, right] : rows) { - out += std::string(indent, ' ') + left; - if (!right.empty()) { - out += std::string(width - left.size() + gap, ' '); - for (const char c : right) { + const auto emit_row = [&](const std::string& _left, + const std::string& _right) { + out += std::string(indent, ' ') + _left; + if (!_right.empty()) { + out += std::string(width - _left.size() + gap, ' '); + for (const char c : _right) { if (c == '\n') { out += continuation; } else { @@ -692,6 +698,40 @@ inline std::string format_usage(const std::string& _program_name, } } out += '\n'; + }; + + const bool has_groups = std::any_of( + _args.begin(), _args.end(), + [](const UsageArg& _arg) { return !_arg.group.empty(); }); + + if (!has_groups) { + out += "\nArguments:\n"; + for (const auto& [left, right] : rows) { + emit_row(left, right); + } + return out; + } + + // Emit sections in order of first appearance; ungrouped args (if any) go + // under "Arguments". + std::vector section_order; + for (const auto& arg : _args) { + const std::string section = + arg.group.empty() ? std::string("Arguments") : arg.group; + if (std::find(section_order.begin(), section_order.end(), section) + == section_order.end()) { + section_order.push_back(section); + } + } + for (const auto& section : section_order) { + out += "\n" + section + ":\n"; + for (size_t i = 0; i < _args.size(); ++i) { + const std::string arg_section = + _args[i].group.empty() ? std::string("Arguments") : _args[i].group; + if (arg_section == section) { + emit_row(rows[i].first, rows[i].second); + } + } } return out; } diff --git a/tests/cli/test_group.cpp b/tests/cli/test_group.cpp new file mode 100644 index 000000000..52f6a7c67 --- /dev/null +++ b/tests/cli/test_group.cpp @@ -0,0 +1,63 @@ +#include + +#include +#include +#include +#include +#include + +namespace test_group { + +struct InputMixin { + rfl::Group<"INPUT OPTIONS", + rfl::Short<"F", rfl::Description<"Format of input file", + std::optional>>> + input_format; +}; + +struct OutputMixin { + rfl::Group<"OUTPUT OPTIONS", + rfl::Short<"o", rfl::Description<"Output file", + std::optional>>> + output; + rfl::Short<"O", rfl::Group<"OUTPUT OPTIONS", + rfl::Description<"Allow overwrite", + rfl::DefaultVal>>> + overwrite = false; +}; + +struct CatConfig { + rfl::Group<"COMMAND OPTIONS", + rfl::Description<"Buffer data in memory", rfl::DefaultVal>> + buffer_data = false; + rfl::Flatten input; + rfl::Flatten output; +}; + +TEST(cli, test_group_sections_and_flatten) { + const std::vector args = {"--buffer-data", "-F", "osm", "-o", + "out.pbf", "-O"}; + const auto res = + rfl::cli::read(args); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(res->buffer_data.value().value().get()); + EXPECT_EQ(res->input().input_format.value().value().value(), "osm"); + EXPECT_EQ(res->output().output.value().value().value(), "out.pbf"); + EXPECT_TRUE(res->output().overwrite.value().value().value().get()); + + const auto detailed = rfl::cli::usage("osmium cat", true); + ASSERT_TRUE(detailed) << detailed.error().what(); + + const auto cmd_pos = detailed->find("\nCOMMAND OPTIONS:\n"); + const auto in_pos = detailed->find("\nINPUT OPTIONS:\n"); + const auto out_pos = detailed->find("\nOUTPUT OPTIONS:\n"); + ASSERT_NE(cmd_pos, std::string::npos) << *detailed; + ASSERT_NE(in_pos, std::string::npos) << *detailed; + ASSERT_NE(out_pos, std::string::npos) << *detailed; + EXPECT_LT(cmd_pos, in_pos); + EXPECT_LT(in_pos, out_pos); + EXPECT_EQ(detailed->find("\nArguments:\n"), std::string::npos); +} + +} // namespace test_group