Skip to content
Open
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
51 changes: 35 additions & 16 deletions include/rfl/cli/Reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <cstdlib>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <type_traits>
Expand Down Expand Up @@ -181,13 +180,12 @@ struct Reader {
if (!_var.args) {
return true;
}
if (_var.args->count(_var.path)) {
if (_var.args->contains(_var.path)) {
return false;
}
const auto prefix = _var.path + path_separator;
const auto it = _var.args->lower_bound(prefix);
return it == _var.args->end()
|| it->first.substr(0, prefix.size()) != prefix;
return it == _var.args->end() || !it->first.starts_with(prefix);
}

/// Reads all elements from a CLI array using the provided array reader.
Expand All @@ -211,7 +209,8 @@ struct Reader {
}

/// Reads all fields from a CLI object using the provided object reader.
/// Iterates through all arguments with the object's prefix and extracts child field names.
/// Iterates through arguments matching the object's prefix, extracting
/// direct child field names and skipping nested subtrees via lower_bound.
/// @tparam ObjectReader The type of reader that processes individual object fields
/// @param _object_reader The reader object that processes each field
/// @param _obj The CLI object to read from
Expand All @@ -221,28 +220,48 @@ struct Reader {
const ObjectReader& _object_reader,
const InputObjectType& _obj
) const noexcept {
std::set<std::string> seen;
if (!_obj.args) {
return std::nullopt;
}
static_assert(
static_cast<unsigned char>(path_separator) < 255,
"path_separator must allow incrementing for range skipping.");
constexpr char after_separator =
static_cast<char>(static_cast<unsigned char>(path_separator) + 1);
auto it = _obj.prefix.empty()
? _obj.args->begin()
: _obj.args->lower_bound(_obj.prefix);
while (it != _obj.args->end()) {
if (!_obj.prefix.empty()
&& it->first.substr(0, _obj.prefix.size()) != _obj.prefix) {
if (!_obj.prefix.empty() && !it->first.starts_with(_obj.prefix)) {
break;
}
const auto rest = std::string_view(it->first).substr(_obj.prefix.size());
const auto separator_pos = rest.find(path_separator);
const auto child = std::string(
separator_pos == std::string_view::npos
? rest
: rest.substr(0, separator_pos));
if (!child.empty() && seen.insert(child).second) {
const auto child_path = _obj.prefix + child;
if (separator_pos == std::string_view::npos) {
if (!rest.empty()) {
_object_reader.read(
rest,
InputVarType{_obj.args, it->first, std::nullopt});
}
++it;
continue;
}
const auto child = rest.substr(0, separator_pos);
std::string sub_prefix;
sub_prefix.reserve(_obj.prefix.size() + child.size() + 1);
sub_prefix.append(_obj.prefix);
sub_prefix.append(child);
if (!child.empty() && !_obj.args->contains(sub_prefix)) {
_object_reader.read(
std::string_view(child),
InputVarType{_obj.args, child_path, std::nullopt});
child,
InputVarType{_obj.args, sub_prefix, std::nullopt});
}
sub_prefix.push_back(path_separator);
++it;
if (it != _obj.args->end() && it->first.starts_with(sub_prefix)) {
sub_prefix.back() = after_separator;
it = _obj.args->lower_bound(sub_prefix);
}
}
return std::nullopt;
}
Expand Down
148 changes: 80 additions & 68 deletions include/rfl/cli/options.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#ifndef RFL_CLI_OPTIONS_HPP_
#define RFL_CLI_OPTIONS_HPP_

#include <array>
#include <cstddef>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

#include "../DefaultVal.hpp"
#include "../Deprecated.hpp"
#include "../Description.hpp"
#include "../NamedTuple.hpp"
#include "../Positional.hpp"
#include "../Rename.hpp"
#include "../Short.hpp"
#include "../Tuple.hpp"
#include "../internal/StringLiteral.hpp"
#include "../internal/has_reflection_type_v.hpp"
#include "../internal/has_reflector.hpp"
Expand Down Expand Up @@ -155,11 +155,10 @@ struct cli_annotations<Short<_name, T>> : cli_annotations_t<T> {
static constexpr int count = cli_annotations_t<T>::count + 1;
};

/// The CLI metadata of the _i-th field of a NamedTuple. InnerType is the
/// The CLI metadata of a single NamedTuple field. InnerType is the
/// declared type of the field, whichever wrappers it consists of.
template <class NamedTupleType, size_t _i>
template <class FieldType>
struct field_info {
using FieldType = rfl::tuple_element_t<_i, typename NamedTupleType::Fields>;
using InnerType = typename FieldType::Type;
using OuterType = std::remove_pointer_t<std::remove_cvref_t<InnerType>>;
using Annotations = cli_annotations_t<OuterType>;
Expand All @@ -174,18 +173,6 @@ struct field_info {
static constexpr std::string_view name() { return FieldType::name(); }
};

/// The short name of the _i-th field, or an empty string if it has none.
template <class NamedTupleType, size_t _i>
constexpr auto get_short_name() {
using Info = field_info<NamedTupleType, _i>;
if constexpr (Info::is_short) {
return Info::Annotations::short_name;
}
else {
return rfl::internal::StringLiteral<1>("");
}
}

/// Strips everything that does not change how a field is written on the
/// command line: optionals, pointers, rfl::Short, rfl::Positional,
/// rfl::Rename, rfl::DefaultVal, rfl::Skip, types with a
Expand Down Expand Up @@ -267,67 +254,99 @@ using option_fields_t = typename std::conditional_t<
rfl::internal::is_named_tuple_v<T>, std::type_identity<T>,
rfl::internal::Processed<T, ProcessorsType>>::type;

/// Adds the option of the top-level field `_name` of type T.
/// Compile-time metadata for one top-level field of a target struct.
struct OptionFieldDescriptor {
std::string_view name;
std::string_view short_name;
OptionKind kind = OptionKind::unknown;
bool has_long_option = false;
bool is_short = false;
bool is_positional = false;
};

/// Returns the OptionKind of a field type T after unwrapping CLI wrappers.
template <class T, class ProcessorsType>
void add_option(const std::string& _name, Options* _options) {
constexpr OptionKind option_kind_for_type() {
using U = unwrapped_option_t<T, ProcessorsType>;
if constexpr (std::is_same_v<U, bool>) {
_options->long_options[_name] = Option{OptionKind::flag};
}
else if constexpr (is_open_option_v<U> || parsing::is_string_map_v<U>
|| is_option_struct_v<U>) {
_options->long_options[_name] = Option{OptionKind::other};
}
else {
_options->long_options[_name] = Option{OptionKind::value};
return OptionKind::flag;
} else if constexpr (is_open_option_v<U> || parsing::is_string_map_v<U>
|| is_option_struct_v<U>) {
return OptionKind::other;
} else {
return OptionKind::value;
}
}

/// Builds the compile-time option descriptor for a single field.
/// Parameterized only on (FieldType, ProcessorsType) — never the enclosing
/// NamedTuple or struct type — so template instantiations stay O(F).
template <class FieldType, class ProcessorsType>
void add_option_for_field(Options* _options) {
constexpr OptionFieldDescriptor make_option_field_descriptor() {
using T = std::remove_cvref_t<typename FieldType::Type>;
using Info = field_info<FieldType>;
OptionFieldDescriptor desc{};
desc.name = Info::name();
desc.is_short = Info::is_short;
desc.is_positional = Info::is_positional;
if constexpr (!rfl::internal::is_extra_fields_v<T>) {
add_option<T, ProcessorsType>(std::string(FieldType::name()), _options);
desc.has_long_option = true;
desc.kind = option_kind_for_type<T, ProcessorsType>();
}
if constexpr (Info::is_short) {
desc.short_name = Info::Annotations::short_name.string_view();
}
return desc;
}

/// Adds the options of the top-level fields of T.
template <class T, class ProcessorsType>
void collect_options(Options* _options) {
using NT = option_fields_t<T, ProcessorsType>;
[&]<size_t... _is>(std::index_sequence<_is...>) {
(add_option_for_field<rfl::tuple_element_t<_is, typename NT::Fields>,
ProcessorsType>(_options),
...);
}(std::make_index_sequence<NT::size()>());
template <class ProcessorsType, class... FieldTypes>
constexpr std::array<OptionFieldDescriptor, sizeof...(FieldTypes)>
make_option_descriptors(const NamedTuple<FieldTypes...>*) {
return std::array<OptionFieldDescriptor, sizeof...(FieldTypes)>{{
make_option_field_descriptor<std::remove_cvref_t<FieldTypes>,
ProcessorsType>()...}};
}

/// Adds the short alias and the positional slot of the _i-th top-level
/// field, if it has them. Two fields with the same short name are kept as
/// an error (Options::duplicate_short_name).
template <class NamedTupleType, size_t _i>
void add_short_and_positional(Options* _options) {
using Info = field_info<NamedTupleType, _i>;
if constexpr (Info::is_short) {
const auto name =
std::string(get_short_name<NamedTupleType, _i>().string_view());
if (!_options->short_options.emplace(name, std::string(Info::name()))
.second
&& _options->duplicate_short_name.empty()) {
_options->duplicate_short_name = name;
/// Populates Options from a flat table of compile-time field descriptors.
/// Two fields with the same short name are kept as an error
/// (Options::duplicate_short_name).
inline void populate_options(const OptionFieldDescriptor* _descriptors,
const size_t _count, Options* _options) {
for (size_t i = 0; i < _count; ++i) {
const auto& desc = _descriptors[i];
if (desc.has_long_option) {
_options->long_options[std::string(desc.name)] = Option{desc.kind};
}
if (desc.is_short) {
const auto short_name = std::string(desc.short_name);
if (!_options->short_options
.emplace(short_name, std::string(desc.name))
.second
&& _options->duplicate_short_name.empty()) {
_options->duplicate_short_name = short_name;
}
if (!short_name.empty()
&& ((short_name[0] >= '0' && short_name[0] <= '9')
|| short_name[0] == '.')) {
_options->negative_numbers_are_options = true;
}
}
if (desc.is_positional) {
_options->positionals.emplace_back(desc.name);
}
}
if constexpr (Info::is_positional) {
_options->positionals.emplace_back(Info::name());
}
}

/// Adds the options, short aliases, and positional slots of the top-level
/// fields of T.
template <class T, class ProcessorsType>
void collect_short_and_positional(Options* _options) {
void collect_options(Options* _options) {
using NT = option_fields_t<T, ProcessorsType>;
[&]<size_t... _is>(std::index_sequence<_is...>) {
(add_short_and_positional<NT, _is>(_options), ...);
}(std::make_index_sequence<NT::size()>());
if constexpr (NT::size() > 0) {
static constexpr auto descriptors = make_option_descriptors<ProcessorsType>(
static_cast<const NT*>(nullptr));
populate_options(descriptors.data(), descriptors.size(), _options);
}
}

/// Derives the options that T accepts from its fields: the option of every
Expand All @@ -340,19 +359,12 @@ template <class T, class ProcessorsType>
Options make_options() {
Options options;
options.group_short_options = true;
using U = unwrapped_option_t<T, ProcessorsType>;
if constexpr (is_option_struct_v<U>) {
collect_options<U, ProcessorsType>(&options);
collect_short_and_positional<U, ProcessorsType>(&options);
}
// As in argparse, -5 is a negative number unless a short option's name
// looks like a number (-1).
options.negative_numbers_are_options = false;
for (const auto& [name, path] : options.short_options) {
if (!name.empty()
&& ((name[0] >= '0' && name[0] <= '9') || name[0] == '.')) {
options.negative_numbers_are_options = true;
}
using U = unwrapped_option_t<T, ProcessorsType>;
if constexpr (is_option_struct_v<U>) {
collect_options<U, ProcessorsType>(&options);
}
return options;
}
Expand Down
Loading
Loading