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
2 changes: 2 additions & 0 deletions include/rfl/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include "cli/LastValueWins.hpp"
#include "cli/ListDelimiter.hpp"
#include "cli/Opt.hpp"
#include "cli/OptGroup.hpp"
#include "cli/OptionsFirst.hpp"
#include "cli/Parser.hpp"
#include "cli/PreserveNames.hpp"
#include "cli/Presets.hpp"
#include "cli/PureFlags.hpp"
#include "cli/Reader.hpp"
#include "cli/RequireEquals.hpp"
Expand Down
16 changes: 16 additions & 0 deletions include/rfl/cli/Opt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,24 @@ struct Opt : rfl::internal::TransparentWrapper<Opt<T, Attrs...>, T> {
using Base::operator=;

using AttrsInfo = internal::opt_attrs<Attrs...>;

/// Contextual boolean conversion when the underlying type supports it
/// (e.g. `Opt<bool>` or `Opt<std::optional<T>>`).
explicit operator bool() const noexcept
requires requires(const T& _v) { static_cast<bool>(_v); }
{
return static_cast<bool>(this->value_);
}
};

/// Shorthand for a boolean CLI flag (`Opt<bool, Attrs...>`).
template <class... Attrs>
using Flag = Opt<bool, Attrs...>;

/// Shorthand alias for a typed CLI argument (`Opt<T, Attrs...>`).
template <class T, class... Attrs>
using Arg = Opt<T, Attrs...>;

namespace internal {

template <class T>
Expand Down
70 changes: 70 additions & 0 deletions include/rfl/cli/OptGroup.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef RFL_CLI_OPTGROUP_HPP_
#define RFL_CLI_OPTGROUP_HPP_

#include <string_view>
#include <type_traits>

#include "../Flatten.hpp"
#include "../internal/StringLiteral.hpp"
#include "../internal/is_flatten_field.hpp"

namespace rfl::cli {

/// Embeds a sub-struct's CLI options flat into the parent struct (just like
/// `rfl::Flatten<T>`) while automatically assigning every ungrouped option in
/// `T` to help section `_group`.
///
/// Individual fields inside `T` may still specify their own `attr::Group<...>`
/// or `rfl::Group<...>` to override the inherited group.
///
/// Example:
/// ```cpp
/// struct OutputArgs {
/// rfl::cli::Opt<std::optional<std::string>, rfl::cli::attr::Short<"o">> out;
/// rfl::cli::Flag<rfl::cli::attr::Short<"f">> force = false;
/// };
/// struct MyCli {
/// rfl::cli::OptGroup<"Output Options", OutputArgs> output;
/// };
/// ```
template <rfl::internal::StringLiteral _group, class T>
struct OptGroup : rfl::Flatten<T> {
using Base = rfl::Flatten<T>;
using Type = std::remove_cvref_t<T>;
static constexpr auto group_name_ = _group;
static constexpr bool is_opt_group_ = true;

using Base::Base;
using Base::operator=;

OptGroup() = default;
OptGroup(const OptGroup&) = default;
OptGroup(OptGroup&&) noexcept = default;
OptGroup& operator=(const OptGroup&) = default;
OptGroup& operator=(OptGroup&&) noexcept = default;
};

namespace internal {

template <class T>
struct is_opt_group : std::false_type {};

template <rfl::internal::StringLiteral _group, class T>
struct is_opt_group<OptGroup<_group, T>> : std::true_type {};

template <class T>
constexpr bool is_opt_group_v =
is_opt_group<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

} // namespace internal

} // namespace rfl::cli

namespace rfl::internal {

template <StringLiteral _group, class T>
class is_flatten_field<rfl::cli::OptGroup<_group, T>> : public std::true_type {};

} // namespace rfl::internal

#endif
42 changes: 42 additions & 0 deletions include/rfl/cli/Presets.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef RFL_CLI_PRESETS_HPP_
#define RFL_CLI_PRESETS_HPP_

#include "../DefaultIfMissing.hpp"
#include "../NoExtraFields.hpp"
#include "../Processors.hpp"
#include "LastValueWins.hpp"
#include "OptionsFirst.hpp"
#include "PureFlags.hpp"
#include "SeparateOptionValues.hpp"

namespace rfl::cli {

/// Standard GNU-style CLI processor bundle:
/// - `rfl::DefaultIfMissing`: unspecified fields take their struct defaults
/// - `rfl::NoExtraFields`: unknown options are rejected
/// - `rfl::cli::SeparateOptionValues`: `--key value` as well as `--key=value`
/// - `rfl::cli::PureFlags`: short boolean flags never consume positional tokens
using GnuStyle = rfl::Processors<rfl::DefaultIfMissing,
rfl::NoExtraFields,
SeparateOptionValues,
PureFlags>;

/// GNU-style CLI processor bundle where repeated scalar options overwrite
/// earlier occurrences (`--out a --out b` → `"b"`).
using GnuLastWins = rfl::Processors<rfl::DefaultIfMissing,
rfl::NoExtraFields,
SeparateOptionValues,
PureFlags,
LastValueWins>;

/// Strict POSIX-style CLI processor bundle where options stop at the first
/// positional argument (`OptionsFirst`), suitable for wrapper/runner commands.
using PosixStrict = rfl::Processors<rfl::DefaultIfMissing,
rfl::NoExtraFields,
SeparateOptionValues,
PureFlags,
OptionsFirst>;

} // namespace rfl::cli

#endif
78 changes: 78 additions & 0 deletions include/rfl/cli/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "BoolValueTokens.hpp"
#include "LastValueWins.hpp"
#include "Opt.hpp"
#include "OptGroup.hpp"
#include "OptionsFirst.hpp"
#include "Parser.hpp"
#include "PreserveNames.hpp"
Expand Down Expand Up @@ -844,6 +845,9 @@ void collect_options(const std::string& _prefix, Options* _options);
template <class T, class ProcessorsType, class VisitedType>
void collect_value_options(Options* _options);

template <class T, class ProcessorsType, class VisitedType>
void collect_opt_groups(const std::string& _prefix, Options* _options);

/// Adds the open prefix `_prefix` of a map or of rfl::ExtraFields with
/// values of type T. If the values are structs or maps, also adds the
/// options inside one of them.
Expand Down Expand Up @@ -872,6 +876,7 @@ void collect_value_options(Options* _options) {
}
else if constexpr (is_option_struct_v<U>) {
collect_options<U, ProcessorsType, VisitedType>("", _options);
collect_opt_groups<U, ProcessorsType, VisitedType>("", _options);
}
else {
_options->open_prefixes.emplace_back("", Option{OptionKind::other});
Expand Down Expand Up @@ -964,6 +969,8 @@ void collect_options(const std::string& _prefix, Options* _options) {
} else {
collect_options<U, ProcessorsType, NextVisitedType>(
path + path_separator, _options);
collect_opt_groups<U, ProcessorsType, NextVisitedType>(
path + path_separator, _options);
}
} else {
static_assert(
Expand All @@ -981,6 +988,77 @@ void collect_options(const std::string& _prefix, Options* _options) {
}(std::make_index_sequence<NT::size()>());
}

/// Assigns `_group` to every leaf option inside `Sub` that does not already
/// have an explicit group in `_options->option_groups`.
template <class Sub, class ProcessorsType, class VisitedType>
void apply_inherited_group(const std::string& _prefix,
const std::string_view _group, Options* _options) {
using SubNT = option_fields_t<Sub, ProcessorsType>;
using NextVisited = typename VisitedType::template add<Sub>;
const auto set_group_if_unset = [&](const std::string& _path) {
const bool already_set = std::any_of(
_options->option_groups.begin(), _options->option_groups.end(),
[&](const auto& _entry) { return _entry.first == _path; });
if (!already_set) {
_options->option_groups.emplace_back(_path, std::string(_group));
}
};
[&]<size_t... _is>(std::index_sequence<_is...>) {
auto apply_one = [&]<size_t _i>() {
using FieldType = rfl::tuple_element_t<_i, typename SubNT::Fields>;
using FT = std::remove_cvref_t<typename FieldType::Type>;
if constexpr (!rfl::internal::is_extra_fields_v<FT>) {
using U = unwrapped_option_t<FT, ProcessorsType>;
const std::string path = _prefix + std::string(FieldType::name());
if constexpr (is_option_struct_v<U> && !is_open_option_v<U>
&& !parsing::is_string_map_v<U>
&& !NextVisited::template contains<U>) {
apply_inherited_group<U, ProcessorsType, NextVisited>(
path + path_separator, _group, _options);
} else {
set_group_if_unset(path);
}
}
};
(apply_one.template operator()<_is>(), ...);
}(std::make_index_sequence<SubNT::size()>());
}

/// Walks the unflattened members of `T` to propagate `rfl::cli::OptGroup`
/// section names across flattened sub-structs (inner groups take precedence
/// over outer groups; explicit per-field groups take precedence over both).
template <class T, class ProcessorsType, class VisitedType>
void collect_opt_groups(const std::string& _prefix, Options* _options) {
using RawT = std::remove_cvref_t<T>;
if constexpr (!is_option_struct_v<RawT>
|| rfl::internal::is_named_tuple_v<RawT>
|| VisitedType::template contains<RawT>) {
return;
} else {
using NextVisited = typename VisitedType::template add<RawT>;
using PtrTuple = rfl::internal::ptr_tuple_t<RawT>;
[&]<size_t... _is>(std::index_sequence<_is...>) {
auto visit = [&]<size_t _i>() {
using MemberType = std::remove_cvref_t<
std::remove_pointer_t<rfl::tuple_element_t<_i, PtrTuple>>>;
if constexpr (is_opt_group_v<MemberType>) {
using Sub = typename MemberType::Type;
// Recurse first so inner OptGroups take precedence over outer ones.
collect_opt_groups<Sub, ProcessorsType, NextVisited>(_prefix,
_options);
apply_inherited_group<Sub, ProcessorsType, NextVisited>(
_prefix, MemberType::group_name_.string_view(), _options);
} else if constexpr (rfl::internal::is_flatten_field_v<MemberType>) {
using Sub = typename MemberType::Type;
collect_opt_groups<Sub, ProcessorsType, NextVisited>(_prefix,
_options);
}
};
(visit.template operator()<_is>(), ...);
}(std::make_index_sequence<rfl::tuple_size_v<PtrTuple>>());
}
}

template <class T, class ProcessorsType>
void collect_short_and_positional(Options* _options) {
using NT = option_fields_t<T, ProcessorsType>;
Expand Down
Loading