From 044b52bca96de931887f701ff9834b5b34fd6f0b Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 27 Nov 2026 08:21:04 +0100 Subject: [PATCH] Add rfl::Hidden wrapper to omit CLI options from usage and help --- include/rfl.hpp | 1 + include/rfl/Hidden.hpp | 48 +++++++++++++++++++++++++++ include/rfl/cli/options.hpp | 15 +++++++++ include/rfl/cli/usage.hpp | 3 ++ tests/cli/test_hidden.cpp | 66 +++++++++++++++++++++++++++++++++++++ 5 files changed, 133 insertions(+) create mode 100644 include/rfl/Hidden.hpp create mode 100644 tests/cli/test_hidden.cpp diff --git a/include/rfl.hpp b/include/rfl.hpp index dc5ace36a..b56864c05 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -30,6 +30,7 @@ #include "rfl/Flatten.hpp" #include "rfl/Generic.hpp" #include "rfl/Hex.hpp" +#include "rfl/Hidden.hpp" #include "rfl/ImplicitValue.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" diff --git a/include/rfl/Hidden.hpp b/include/rfl/Hidden.hpp new file mode 100644 index 000000000..8fad4f40b --- /dev/null +++ b/include/rfl/Hidden.hpp @@ -0,0 +1,48 @@ +#ifndef RFL_HIDDEN_HPP_ +#define RFL_HIDDEN_HPP_ + +#include "internal/TransparentWrapper.hpp" + +namespace rfl { + +/// Marks a CLI option or positional argument as hidden from generated usage +/// and help text (`rfl::cli::usage` and `rfl::cli::read_app`), while still +/// parsing it normally. +/// For non-CLI formats (JSON, YAML, etc.), this is transparent. +template +struct Hidden : internal::TransparentWrapper, T> { + using Base = internal::TransparentWrapper, T>; + using Base::Base; + using Base::operator=; + + Hidden() + requires std::is_default_constructible_v + = default; + Hidden(const Hidden&) = default; + Hidden(Hidden&&) = default; + Hidden& operator=(const Hidden&) = default; + Hidden& operator=(Hidden&&) = default; + + template + Hidden(const Hidden& _field) : Base(_field.get()) {} + + template + Hidden(Hidden&& _field) noexcept(std::is_nothrow_constructible_v) + : Base(std::move(_field.value_)) {} + + template + auto& operator=(const Hidden& _field) { + this->value_ = _field.get(); + return *this; + } + + template + auto& operator=(Hidden&& _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 51c293df8..405e23f1b 100644 --- a/include/rfl/cli/options.hpp +++ b/include/rfl/cli/options.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,7 @@ #include "../DefaultVal.hpp" #include "../Deprecated.hpp" #include "../Description.hpp" +#include "../Hidden.hpp" #include "../ImplicitValue.hpp" #include "../Positional.hpp" #include "../Rename.hpp" @@ -106,6 +108,10 @@ struct Options { /// `--servers.eu.port` is looked up as `port`. std::map> open_prefix_options; + /// Paths of fields wrapped in rfl::Hidden, which are parsed normally but + /// omitted from usage() and --help. + std::set hidden_options; + /// Whether options that match no field are an error (rfl::NoExtraFields). bool reject_unknown = false; @@ -265,6 +271,7 @@ template struct cli_annotations { static constexpr bool is_short = false; static constexpr bool is_positional = false; + static constexpr bool is_hidden = false; static constexpr size_t num_implicit_value = 0; static constexpr rfl::internal::StringLiteral<1> short_name = ""; static constexpr int count = 0; @@ -313,6 +320,11 @@ struct cli_annotations> : cli_annotations_t { static constexpr auto implicit_value = _implicit; }; +template +struct cli_annotations> : cli_annotations_t { + static constexpr bool is_hidden = true; +}; + /// 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 @@ -545,6 +557,9 @@ void collect_value_options(Options* _options) { template void add_option(const std::string& _path, Options* _options) { using U = unwrapped_option_t; + if constexpr (cli_annotations_t::is_hidden) { + _options->hidden_options.insert(_path); + } 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 2c04b69b6..25352df9b 100644 --- a/include/rfl/cli/usage.hpp +++ b/include/rfl/cli/usage.hpp @@ -435,6 +435,9 @@ class UsageCollector { for (const auto& [name, type] : _object.types_) { auto arg = UsageArg{}; arg.name = is_root ? name : _parent.name + path_separator + name; + if (options_->hidden_options.contains(arg.name)) { + continue; + } // Children of an optional object are only needed if it is given. arg.required = _parent.required; if (is_root) { diff --git a/tests/cli/test_hidden.cpp b/tests/cli/test_hidden.cpp new file mode 100644 index 000000000..0f0f4961e --- /dev/null +++ b/tests/cli/test_hidden.cpp @@ -0,0 +1,66 @@ +#include + +#include +#include +#include +#include +#include + +namespace test_hidden { + +struct SortConfig { + rfl::Short<"v", rfl::Description<"Verbose output", rfl::DefaultVal>> + verbose = false; + rfl::Hidden>> strategy; + rfl::Positional>>> + input_filenames; +}; + +struct HiddenFlattened { + rfl::Short<"d", rfl::Hidden>> debug_level = 0; +}; + +struct WrapperOrderConfig { + rfl::Description<"Visible", std::optional> visible; + rfl::Flatten extra; +}; + +TEST(cli, test_hidden_parsed_but_omitted_from_usage) { + const std::vector args = {"-v", "-s", "multipass", "a.osm", + "b.osm"}; + const auto res = + rfl::cli::read>(args); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_TRUE(res->verbose.value().value().get()); + EXPECT_EQ(res->strategy.value().value(), "multipass"); + ASSERT_TRUE(res->input_filenames.value().value().has_value()); + EXPECT_EQ(*res->input_filenames.value().value(), + (std::vector{"a.osm", "b.osm"})); + + const auto short_usage = rfl::cli::usage("osmium sort", false); + ASSERT_TRUE(short_usage) << short_usage.error().what(); + EXPECT_EQ(*short_usage, "Usage: osmium sort [-v|--verbose[=]]\n"); + + const auto detailed = rfl::cli::usage("osmium sort", true); + ASSERT_TRUE(detailed) << detailed.error().what(); + EXPECT_EQ(detailed->find("strategy"), std::string::npos); + EXPECT_EQ(detailed->find("input-filenames"), std::string::npos); + EXPECT_NE(detailed->find("--verbose"), std::string::npos); +} + +TEST(cli, test_hidden_inside_short_and_flatten) { + const std::vector args = {"--visible=hi", "-d", "3"}; + const auto res = rfl::cli::read(args); + ASSERT_TRUE(res) << res.error().what(); + EXPECT_EQ(res->visible.value(), "hi"); + EXPECT_EQ(res->extra().debug_level.value().value().get(), 3); + + const auto detailed = rfl::cli::usage("app", true); + ASSERT_TRUE(detailed) << detailed.error().what(); + EXPECT_NE(detailed->find("--visible"), std::string::npos); + EXPECT_EQ(detailed->find("debug-level"), std::string::npos); +} + +} // namespace test_hidden