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
3 changes: 3 additions & 0 deletions include/rfl/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
#include "../rfl.hpp"
#include "cli/AllowAbbreviations.hpp"
#include "cli/App.hpp"
#include "cli/BoolValueTokens.hpp"
#include "cli/KeepEmptyItems.hpp"
#include "cli/LastValueWins.hpp"
#include "cli/ListDelimiter.hpp"
#include "cli/Opt.hpp"
#include "cli/OptionsFirst.hpp"
#include "cli/Parser.hpp"
#include "cli/PreserveNames.hpp"
#include "cli/PureFlags.hpp"
#include "cli/Reader.hpp"
#include "cli/RequireEquals.hpp"
#include "cli/SeparateOptionValues.hpp"
#include "cli/SingleDashLong.hpp"
#include "cli/options.hpp"
#include "cli/parse_argv.hpp"
#include "cli/read.hpp"
Expand Down
36 changes: 36 additions & 0 deletions include/rfl/cli/BoolValueTokens.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef RFL_CLI_BOOLVALUETOKENS_HPP_
#define RFL_CLI_BOOLVALUETOKENS_HPP_

#include <type_traits>

namespace rfl::cli {

/// Processor allowing long boolean flags to consume a separate following
/// boolean literal token (`--use_gpu 0`, `--use_gpu 1`, `--use_gpu true`,
/// `--use_gpu false`), matching Boost.Program_options `po::value<bool>()`.
/// Non-boolean following tokens are left untouched.
struct BoolValueTokens {
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

namespace internal {

template <class ProcessorsType>
struct bool_value_tokens;

template <template <class...> class ProcessorsType, class... Ps>
struct bool_value_tokens<ProcessorsType<Ps...>> {
static constexpr bool value = (std::is_same_v<BoolValueTokens, Ps> || ...);
};

template <class ProcessorsType>
constexpr bool bool_value_tokens_v = bool_value_tokens<ProcessorsType>::value;

} // namespace internal

} // namespace rfl::cli

#endif
108 changes: 106 additions & 2 deletions include/rfl/cli/Opt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,47 @@ struct MultiToken {};
/// process listings).
struct Sensitive {};

/// Adds an alternate long option name (without leading `--`) that maps to the
/// same field as its canonical long name (may be repeated on one `Opt`).
template <rfl::internal::StringLiteral _name>
struct Alias {
static constexpr auto value = _name;
};

/// Marks an integral field as a repeatable counter flag (`-v`, `-vvv`,
/// `--verbose --verbose` → 1, 3, 2).
struct Counter {};

/// Declares that this option cannot be combined with `--_other` (canonical long
/// name). May be repeated on one `Opt`.
template <rfl::internal::StringLiteral _other>
struct Excludes {
static constexpr auto value = _other;
};

/// Declares that this option requires `--_other` (canonical long name) to also
/// be supplied. May be repeated on one `Opt`.
template <rfl::internal::StringLiteral _other>
struct Needs {
static constexpr auto value = _other;
};

/// Allows negating a boolean/option flag via `--<prefix><name>` (default
/// `"no-"`, e.g. `--pyc` / `--no-pyc`, or `"no"` for `-nofoo`).
template <rfl::internal::StringLiteral _prefix = "no-">
struct Negatable {
static constexpr auto prefix = _prefix;
};

/// Registers an explicit negated long option name (without leading `--`) that
/// sets this option to `false` (e.g. `--skip-run-link-scripts` with
/// `NegatedName<"run-link-scripts">`, or `-finline` with
/// `NegatedName<"fno-inline">`). May be repeated on one `Opt`.
template <rfl::internal::StringLiteral _name>
struct NegatedName {
static constexpr auto value = _name;
};

template <rfl::internal::StringLiteral _desc>
struct Desc {
static constexpr auto value = _desc;
Expand Down Expand Up @@ -77,6 +118,7 @@ struct opt_attrs<> {
static constexpr bool has_value_name = false;
static constexpr bool is_multi_token = false;
static constexpr bool is_sensitive = false;
static constexpr bool is_counter = false;
static constexpr size_t num_implicit_value = 0;
static constexpr int count = 0;
static constexpr rfl::internal::StringLiteral<1> short_name = "";
Expand All @@ -87,15 +129,27 @@ struct opt_attrs<> {
static constexpr bool is_deprecated = false;
static constexpr std::string_view deprecation_message = {};
static constexpr std::string_view default_hint = {};

template <class Fn>
static void for_each_alias(Fn&&) {}
template <class Fn>
static void for_each_excludes(Fn&&) {}
template <class Fn>
static void for_each_needs(Fn&&) {}
template <class Fn>
static void for_each_negated_prefix(Fn&&) {}
template <class Fn>
static void for_each_negated_name(Fn&&) {}
};

template <class First, class... Rest>
struct opt_attrs<First, Rest...> {
static_assert(
!std::is_same_v<First, First>,
"Unknown attribute passed to rfl::cli::Opt. Use tags from "
"rfl::cli::attr (Short, Positional, Hidden, Group, ValueName, "
"Implicit, MultiToken, Desc, Deprecated, DefaultHint).");
"rfl::cli::attr (Short, Alias, Positional, Hidden, Group, ValueName, "
"Implicit, MultiToken, Sensitive, Counter, Excludes, Needs, Negatable, "
"NegatedName, Desc, Deprecated, DefaultHint).");
};

template <rfl::internal::StringLiteral _name, class... Rest>
Expand Down Expand Up @@ -149,6 +203,56 @@ struct opt_attrs<attr::Sensitive, Rest...> : opt_attrs<Rest...> {
static constexpr bool is_sensitive = true;
};

template <rfl::internal::StringLiteral _name, class... Rest>
struct opt_attrs<attr::Alias<_name>, Rest...> : opt_attrs<Rest...> {
template <class Fn>
static void for_each_alias(Fn&& _fn) {
_fn(_name.string_view());
opt_attrs<Rest...>::for_each_alias(_fn);
}
};

template <class... Rest>
struct opt_attrs<attr::Counter, Rest...> : opt_attrs<Rest...> {
static constexpr bool is_counter = true;
};

template <rfl::internal::StringLiteral _other, class... Rest>
struct opt_attrs<attr::Excludes<_other>, Rest...> : opt_attrs<Rest...> {
template <class Fn>
static void for_each_excludes(Fn&& _fn) {
_fn(_other.string_view());
opt_attrs<Rest...>::for_each_excludes(_fn);
}
};

template <rfl::internal::StringLiteral _other, class... Rest>
struct opt_attrs<attr::Needs<_other>, Rest...> : opt_attrs<Rest...> {
template <class Fn>
static void for_each_needs(Fn&& _fn) {
_fn(_other.string_view());
opt_attrs<Rest...>::for_each_needs(_fn);
}
};

template <rfl::internal::StringLiteral _prefix, class... Rest>
struct opt_attrs<attr::Negatable<_prefix>, Rest...> : opt_attrs<Rest...> {
template <class Fn>
static void for_each_negated_prefix(Fn&& _fn) {
_fn(_prefix.string_view());
opt_attrs<Rest...>::for_each_negated_prefix(_fn);
}
};

template <rfl::internal::StringLiteral _name, class... Rest>
struct opt_attrs<attr::NegatedName<_name>, Rest...> : opt_attrs<Rest...> {
template <class Fn>
static void for_each_negated_name(Fn&& _fn) {
_fn(_name.string_view());
opt_attrs<Rest...>::for_each_negated_name(_fn);
}
};

template <rfl::internal::StringLiteral _desc, class... Rest>
struct opt_attrs<attr::Desc<_desc>, Rest...> : opt_attrs<Rest...> {
using Next = opt_attrs<Rest...>;
Expand Down
47 changes: 47 additions & 0 deletions include/rfl/cli/PreserveNames.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef RFL_CLI_PRESERVENAMES_HPP_
#define RFL_CLI_PRESERVENAMES_HPP_

#include <type_traits>

#include "../Processors.hpp"
#include "../SnakeCaseToKebabCase.hpp"

namespace rfl::cli {

/// Processor that disables the automatic `rfl::SnakeCaseToKebabCase` conversion
/// applied by `rfl::cli::read`, `rfl::cli::read_detailed`, `rfl::cli::read_app`,
/// and `rfl::cli::usage`, preserving C++ field names verbatim (e.g.
/// `--database_path`, `--SiftExtraction.use_gpu`).
struct PreserveNames {
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

namespace internal {

template <class ProcessorsType>
struct preserve_names;

template <template <class...> class ProcessorsType, class... Ps>
struct preserve_names<ProcessorsType<Ps...>> {
static constexpr bool value = (std::is_same_v<PreserveNames, Ps> || ...);
};

template <class ProcessorsType>
constexpr bool preserve_names_v = preserve_names<ProcessorsType>::value;

/// Flattens CLI processors, prepending `SnakeCaseToKebabCase` unless
/// `PreserveNames` is present.
template <class... Ps>
using cli_processors_t = std::conditional_t<
preserve_names_v<rfl::internal::flatten_processors_t<Ps...>>,
rfl::internal::flatten_processors_t<Ps...>,
rfl::internal::flatten_processors_t<SnakeCaseToKebabCase, Ps...>>;

} // namespace internal

} // namespace rfl::cli

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

#include <type_traits>

namespace rfl::cli {

/// Processor telling the CLI parser to accept single-dash long options
/// (`-datadir=/tmp`, `-lint-only`) as equivalent to double-dash long options
/// (`--datadir=/tmp`, `--lint-only`), as used by Bitcoin Core, Verilator, and
/// classic compiler/linker CLIs.
struct SingleDashLong {
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

namespace internal {

template <class ProcessorsType>
struct single_dash_long;

template <template <class...> class ProcessorsType, class... Ps>
struct single_dash_long<ProcessorsType<Ps...>> {
static constexpr bool value =
(std::is_same_v<SingleDashLong, Ps> || ...);
};

template <class ProcessorsType>
constexpr bool single_dash_long_v = single_dash_long<ProcessorsType>::value;

} // namespace internal

} // namespace rfl::cli

#endif
Loading