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
26 changes: 26 additions & 0 deletions include/rfl/Processors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ struct Processors<Head, Tail...> {
}
};

namespace internal {

template <class... Ps>
struct flatten_processors;

template <class... Acc>
struct flatten_processors<Processors<Acc...>> {
using type = Processors<Acc...>;
};

template <class... Acc, class... Inner, class... Rest>
struct flatten_processors<Processors<Acc...>, Processors<Inner...>, Rest...>
: flatten_processors<Processors<Acc...>, Inner..., Rest...> {};

template <class... Acc, class Head, class... Rest>
struct flatten_processors<Processors<Acc...>, Head, Rest...>
: flatten_processors<Processors<Acc..., Head>, Rest...> {};

/// Flattens arbitrarily nested rfl::Processors<...> bundles into a single
/// flat rfl::Processors<...> list, preserving left-to-right order.
template <class... Ps>
using flatten_processors_t =
typename flatten_processors<Processors<>, Ps...>::type;

} // namespace internal

} // namespace rfl

#endif
3 changes: 2 additions & 1 deletion include/rfl/cli/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ rfl::Result<T> read_parsed(ParsedArgs _parsed, const Options& _options) {
/// (see make_options) and returns ParsedArgs, and turns them into a struct.
template <class T, class... Ps, class ParseFn>
rfl::Result<T> parse_and_read(const ParseFn& _parse) {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
using ProcessorsType =
rfl::internal::flatten_processors_t<SnakeCaseToKebabCase, Ps...>;
static_assert(!internal::has_app_v<ProcessorsType>,
"rfl::cli::App turns on --help and --version, which "
"rfl::cli::read cannot report. Call rfl::cli::read_app "
Expand Down
6 changes: 4 additions & 2 deletions include/rfl/cli/read_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ template <class T, class... Ps, class ParseFn>
rfl::Result<std::variant<T, HelpOrVersion>> parse_and_read_app(
const std::string& _program_name, const ParseFn& _parse,
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
using ProcessorsType =
rfl::internal::flatten_processors_t<SnakeCaseToKebabCase, Ps...>;
using ResultType = std::variant<T, HelpOrVersion>;
auto options = make_options<T, ProcessorsType>();
const auto builtin = add_builtin_options<T, ProcessorsType>(&options);
Expand Down Expand Up @@ -202,7 +203,8 @@ template <class T, class... Ps,
rfl::Result<std::variant<T, HelpOrVersion>> read_app(
R&& _args,
const UsageOptions& _usage_opts = UsageOptions{.detailed = true}) {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
using ProcessorsType =
rfl::internal::flatten_processors_t<SnakeCaseToKebabCase, Ps...>;
return read_app<T, Ps...>(internal::app_settings_t<ProcessorsType>::name,
std::forward<R>(_args), _usage_opts);
}
Expand Down
3 changes: 2 additions & 1 deletion include/rfl/cli/resolve_args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ rfl::Result<std::map<std::string, std::string>> resolve_args(
return rfl::error("Duplicate argument: --"
+ _parsed.later_occurrences.begin()->first);
}
const auto options = internal::make_options<T, ProcessorsType>();
using FlatProcessors = rfl::internal::flatten_processors_t<ProcessorsType>;
const auto options = internal::make_options<T, FlatProcessors>();
const auto short_names = internal::check_short_names(options);
if (!short_names) {
return rfl::error(short_names.error().what());
Expand Down
6 changes: 4 additions & 2 deletions include/rfl/cli/usage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,8 @@ inline std::string format_usage(const std::string& _program_name,
/// default-constructible, which read<T, rfl::DefaultIfMissing> requires, too.
template <class T, class... Ps>
std::optional<Generic> usage_defaults() {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
using ProcessorsType =
rfl::internal::flatten_processors_t<SnakeCaseToKebabCase, Ps...>;
if constexpr (rfl::internal::default_if_missing_v<ProcessorsType>
&& std::is_default_constructible_v<T>) {
try {
Expand Down Expand Up @@ -844,7 +845,8 @@ std::optional<Generic> usage_defaults() {
template <class T, class... Ps>
rfl::Result<std::string> usage(const std::string& _program_name,
const UsageOptions& _opts) {
using ProcessorsType = Processors<SnakeCaseToKebabCase, Ps...>;
using ProcessorsType =
rfl::internal::flatten_processors_t<SnakeCaseToKebabCase, Ps...>;
using NamedTupleType = rfl::internal::processed_t<T, ProcessorsType>;
constexpr auto delimiter = internal::list_delimiter_v<ProcessorsType>;
// The table that read() parses with, so the help shows what the parser
Expand Down
2 changes: 1 addition & 1 deletion include/rfl/generic/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace generic {
template <class T, class... Ps>
auto read(const Generic& _g) {
const auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, _g);
return Parser<T, rfl::internal::flatten_processors_t<Ps...>>::read(r, _g);
}

} // namespace generic
Expand Down
3 changes: 2 additions & 1 deletion include/rfl/generic/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Generic write(const auto& _t) {
using T = std::remove_cvref_t<decltype(_t)>;
using ParentType = parsing::Parent<Writer>;
auto w = Writer();
Parser<T, Processors<Ps...>>::write(w, _t, typename ParentType::Root{});
Parser<T, rfl::internal::flatten_processors_t<Ps...>>::write(
w, _t, typename ParentType::Root{});
return w.root();
}

Expand Down
79 changes: 79 additions & 0 deletions tests/cli/test_processor_bundles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <gtest/gtest.h>

#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>

namespace test_processor_bundles {

using BaseCli = rfl::Processors<rfl::NoExtraFields,
rfl::cli::SeparateOptionValues,
rfl::cli::ListDelimiter<"">>;
using FullCli =
rfl::Processors<BaseCli, rfl::cli::PureFlags, rfl::Processors<>>;
using AppBundle =
rfl::Processors<rfl::cli::App<"demo", "2.0", "Demo app">, FullCli>;

static_assert(
std::is_same_v<
rfl::internal::flatten_processors_t<rfl::SnakeCaseToKebabCase, FullCli>,
rfl::Processors<rfl::SnakeCaseToKebabCase, rfl::NoExtraFields,
rfl::cli::SeparateOptionValues,
rfl::cli::ListDelimiter<"">, rfl::cli::PureFlags>>);

struct Config {
rfl::Short<"v", rfl::DefaultVal<bool>> verbose = false;
std::optional<std::string> output;
std::optional<std::vector<std::string>> items;
rfl::Positional<rfl::DefaultVal<std::vector<std::string>>> ids;
};

TEST(cli, test_processor_bundle_read) {
const std::vector<std::string> args = {
"--output", "out.txt", "--items", "a,b", "-v", "1", "2"};
const auto res = rfl::cli::read<Config, FullCli>(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<std::string>{"a,b"}));
EXPECT_EQ(rfl::unwrap(res->ids), (std::vector<std::string>{"1", "2"}));

const std::vector<std::string> bad_args = {"--unknown"};
const auto err = rfl::cli::read<Config, FullCli>(bad_args);
EXPECT_FALSE(err);
}

TEST(cli, test_processor_bundle_read_app_and_usage) {
const std::vector<std::string> help_args = {"--help"};
const auto res = rfl::cli::read_app<Config, AppBundle>(help_args);
ASSERT_TRUE(res) << res.error().what();
const auto* info = std::get_if<rfl::cli::HelpOrVersion>(&*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("[=<bool>]"), std::string::npos) << info->text;
EXPECT_EQ(info->text.find("Default: false."), std::string::npos)
<< info->text;

const auto direct = rfl::cli::usage<Config, AppBundle>("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<rfl::DefaultIfMissing, rfl::cli::PureFlags>;
const auto text = rfl::cli::usage<Simple, Bundle>("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