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 @@ -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"
Expand Down
52 changes: 52 additions & 0 deletions include/rfl/Group.hpp
Original file line number Diff line number Diff line change
@@ -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 <internal::StringLiteral _group, class T>
struct Group : internal::TransparentWrapper<Group<_group, T>, T> {
using Base = internal::TransparentWrapper<Group<_group, T>, 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<T>
= default;
Group(const Group&) = default;
Group(Group&&) = default;
Group& operator=(const Group&) = default;
Group& operator=(Group&&) = default;

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

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

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

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

} // namespace rfl

#endif
16 changes: 16 additions & 0 deletions include/rfl/cli/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -112,6 +113,9 @@ struct Options {
/// omitted from usage() and --help.
std::set<std::string> hidden_options;

/// Section header name for fields wrapped in rfl::Group, by path.
std::map<std::string, std::string> option_groups;

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

Expand Down Expand Up @@ -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 <class T>
Expand Down Expand Up @@ -325,6 +331,12 @@ struct cli_annotations<Hidden<T>> : cli_annotations_t<T> {
static constexpr bool is_hidden = true;
};

template <rfl::internal::StringLiteral _group, class T>
struct cli_annotations<Group<_group, T>> : cli_annotations_t<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 <class NamedTupleType, size_t _i>
Expand Down Expand Up @@ -560,6 +572,10 @@ void add_option(const std::string& _path, Options* _options) {
if constexpr (cli_annotations_t<T>::is_hidden) {
_options->hidden_options.insert(_path);
}
if constexpr (cli_annotations_t<T>::has_group) {
_options->option_groups[_path] =
std::string(cli_annotations_t<T>::group_name.string_view());
}
if constexpr (cli_annotations_t<T>::num_implicit_value > 0) {
_options->long_options[_path] =
implicit_value_option<T, ProcessorsType>();
Expand Down
52 changes: 46 additions & 6 deletions include/rfl/cli/usage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ struct UsageArg {
/// Rendered constraints, such as "Range: 1..65535.".
std::vector<std::string> constraints;
std::optional<std::string> default_value;
/// Optional help section header (from rfl::Group).
std::string group;
};

template <class NamedTupleType, class ProcessorsType, size_t _i>
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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<std::string> 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;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/cli/test_group.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <gtest/gtest.h>

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

namespace test_group {

struct InputMixin {
rfl::Group<"INPUT OPTIONS",
rfl::Short<"F", rfl::Description<"Format of input file",
std::optional<std::string>>>>
input_format;
};

struct OutputMixin {
rfl::Group<"OUTPUT OPTIONS",
rfl::Short<"o", rfl::Description<"Output file",
std::optional<std::string>>>>
output;
rfl::Short<"O", rfl::Group<"OUTPUT OPTIONS",
rfl::Description<"Allow overwrite",
rfl::DefaultVal<bool>>>>
overwrite = false;
};

struct CatConfig {
rfl::Group<"COMMAND OPTIONS",
rfl::Description<"Buffer data in memory", rfl::DefaultVal<bool>>>
buffer_data = false;
rfl::Flatten<InputMixin> input;
rfl::Flatten<OutputMixin> output;
};

TEST(cli, test_group_sections_and_flatten) {
const std::vector<std::string> args = {"--buffer-data", "-F", "osm", "-o",
"out.pbf", "-O"};
const auto res =
rfl::cli::read<CatConfig, rfl::NoExtraFields,
rfl::cli::SeparateOptionValues>(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<CatConfig>("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