Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/rfl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 48 additions & 0 deletions include/rfl/Hidden.hpp
Original file line number Diff line number Diff line change
@@ -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 <class T>
struct Hidden : internal::TransparentWrapper<Hidden<T>, T> {
using Base = internal::TransparentWrapper<Hidden<T>, T>;
using Base::Base;
using Base::operator=;

Hidden()
requires std::is_default_constructible_v<T>
= default;
Hidden(const Hidden&) = default;
Hidden(Hidden&&) = default;
Hidden& operator=(const Hidden&) = default;
Hidden& operator=(Hidden&&) = default;

template <class U>
Hidden(const Hidden<U>& _field) : Base(_field.get()) {}

template <class U>
Hidden(Hidden<U>&& _field) noexcept(std::is_nothrow_constructible_v<T, U&&>)
: Base(std::move(_field.value_)) {}

template <class U>
auto& operator=(const Hidden<U>& _field) {
this->value_ = _field.get();
return *this;
}

template <class U>
auto& operator=(Hidden<U>&& _field) {
this->value_ = std::move(_field.value_);
return *this;
}
};

} // namespace rfl

#endif
15 changes: 15 additions & 0 deletions include/rfl/cli/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstddef>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <type_traits>
Expand All @@ -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"
Expand Down Expand Up @@ -106,6 +108,10 @@ struct Options {
/// `--servers.eu.port` is looked up as `port`.
std::map<std::string, rfl::Box<Options>> open_prefix_options;

/// Paths of fields wrapped in rfl::Hidden, which are parsed normally but
/// omitted from usage() and --help.
std::set<std::string> hidden_options;

/// Whether options that match no field are an error (rfl::NoExtraFields).
bool reject_unknown = false;

Expand Down Expand Up @@ -265,6 +271,7 @@ template <class T>
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;
Expand Down Expand Up @@ -313,6 +320,11 @@ struct cli_annotations<ImplicitValue<T, _implicit>> : cli_annotations_t<T> {
static constexpr auto implicit_value = _implicit;
};

template <class T>
struct cli_annotations<Hidden<T>> : cli_annotations_t<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 <class NamedTupleType, size_t _i>
Expand Down Expand Up @@ -545,6 +557,9 @@ void collect_value_options(Options* _options) {
template <class T, class ProcessorsType, class VisitedType>
void add_option(const std::string& _path, Options* _options) {
using U = unwrapped_option_t<T, ProcessorsType>;
if constexpr (cli_annotations_t<T>::is_hidden) {
_options->hidden_options.insert(_path);
}
if constexpr (cli_annotations_t<T>::num_implicit_value > 0) {
_options->long_options[_path] =
implicit_value_option<T, ProcessorsType>();
Expand Down
3 changes: 3 additions & 0 deletions include/rfl/cli/usage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
66 changes: 66 additions & 0 deletions tests/cli/test_hidden.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <gtest/gtest.h>

#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <optional>
#include <string>
#include <vector>

namespace test_hidden {

struct SortConfig {
rfl::Short<"v", rfl::Description<"Verbose output", rfl::DefaultVal<bool>>>
verbose = false;
rfl::Hidden<rfl::Short<"s", std::optional<std::string>>> strategy;
rfl::Positional<rfl::Hidden<std::optional<std::vector<std::string>>>>
input_filenames;
};

struct HiddenFlattened {
rfl::Short<"d", rfl::Hidden<rfl::DefaultVal<int>>> debug_level = 0;
};

struct WrapperOrderConfig {
rfl::Description<"Visible", std::optional<std::string>> visible;
rfl::Flatten<HiddenFlattened> extra;
};

TEST(cli, test_hidden_parsed_but_omitted_from_usage) {
const std::vector<std::string> args = {"-v", "-s", "multipass", "a.osm",
"b.osm"};
const auto res =
rfl::cli::read<SortConfig, rfl::NoExtraFields,
rfl::cli::SeparateOptionValues,
rfl::cli::ListDelimiter<"">>(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<std::string>{"a.osm", "b.osm"}));

const auto short_usage = rfl::cli::usage<SortConfig>("osmium sort", false);
ASSERT_TRUE(short_usage) << short_usage.error().what();
EXPECT_EQ(*short_usage, "Usage: osmium sort [-v|--verbose[=<bool>]]\n");

const auto detailed = rfl::cli::usage<SortConfig>("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<std::string> args = {"--visible=hi", "-d", "3"};
const auto res = rfl::cli::read<WrapperOrderConfig, rfl::NoExtraFields>(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<WrapperOrderConfig>("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