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: 1 addition & 1 deletion include/rfl/Short.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Short {
/// The short name.
static constexpr auto short_name_ = _name;

static_assert(_name.length == 1, "Short name must be exactly one character.");
static_assert(_name.length >= 1, "Short name cannot be empty.");

/// @brief Default constructor.
/// @details Constructs the underlying value using its default constructor.
Expand Down
2 changes: 2 additions & 0 deletions include/rfl/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
#include "cli/AllowAbbreviations.hpp"
#include "cli/App.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/PureFlags.hpp"
#include "cli/Reader.hpp"
#include "cli/RequireEquals.hpp"
#include "cli/SeparateOptionValues.hpp"
#include "cli/options.hpp"
#include "cli/parse_argv.hpp"
Expand Down
41 changes: 41 additions & 0 deletions include/rfl/cli/LastValueWins.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef RFL_CLI_LAST_VALUE_WINS_HPP_
#define RFL_CLI_LAST_VALUE_WINS_HPP_

#include <type_traits>

#include "../Processors.hpp"

namespace rfl::cli {

/// A setting that makes repeated non-list options overwrite their previous
/// value instead of failing with a duplicate/conflict error (last occurrence
/// in argv wins, as in GNU/compiler CLIs such as gcc, cppcheck, and aria2).
/// Use `rfl::cli::read_detailed` to also inspect every occurrence in argv order.
struct LastValueWins {
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

namespace internal {

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

template <>
struct last_value_wins<LastValueWins> : std::true_type {};

template <class... Ps>
struct last_value_wins<Processors<Ps...>>
: std::disjunction<last_value_wins<Ps>...> {};

template <class T>
constexpr bool last_value_wins_v =
last_value_wins<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

} // namespace internal

} // namespace rfl::cli

#endif
11 changes: 11 additions & 0 deletions include/rfl/cli/Opt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ struct Implicit {

struct MultiToken {};

/// Marks an option's value as sensitive so mutable `char* argv[]` overloads
/// overwrite the value in place with `'*'` after parsing (hiding secrets from
/// process listings).
struct Sensitive {};

template <rfl::internal::StringLiteral _desc>
struct Desc {
static constexpr auto value = _desc;
Expand Down Expand Up @@ -71,6 +76,7 @@ struct opt_attrs<> {
static constexpr bool has_group = false;
static constexpr bool has_value_name = false;
static constexpr bool is_multi_token = false;
static constexpr bool is_sensitive = false;
static constexpr size_t num_implicit_value = 0;
static constexpr int count = 0;
static constexpr rfl::internal::StringLiteral<1> short_name = "";
Expand Down Expand Up @@ -138,6 +144,11 @@ struct opt_attrs<attr::MultiToken, Rest...> : opt_attrs<Rest...> {
static constexpr bool is_multi_token = true;
};

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

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

#include <type_traits>

#include "../Processors.hpp"

namespace rfl::cli {

/// A setting that requires long options taking a value (`OptionKind::value`)
/// to be given with `=` (`--key=value`) when `SeparateOptionValues` is not
/// enabled, failing with a missing-value error if bare `--key` is passed.
/// Flags and `rfl::ImplicitValue` options still work without `=`.
struct RequireEquals {
template <class StructType>
static auto process(auto&& _named_tuple) {
return _named_tuple;
}
};

namespace internal {

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

template <>
struct require_equals<RequireEquals> : std::true_type {};

template <class... Ps>
struct require_equals<Processors<Ps...>>
: std::disjunction<require_equals<Ps>...> {};

template <class T>
constexpr bool require_equals_v =
require_equals<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

} // namespace internal

} // namespace rfl::cli

#endif
29 changes: 25 additions & 4 deletions include/rfl/cli/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
#include "../internal/processed_t.hpp"
#include "../parsing/is_string_map.hpp"
#include "AllowAbbreviations.hpp"
#include "LastValueWins.hpp"
#include "Opt.hpp"
#include "OptionsFirst.hpp"
#include "Parser.hpp"
#include "PureFlags.hpp"
#include "SeparateOptionValues.hpp"
#include "Reader.hpp"
#include "RequireEquals.hpp"
#include "SeparateOptionValues.hpp"

namespace rfl::cli::internal {

Expand Down Expand Up @@ -80,6 +82,10 @@ struct Option {
/// Whether a list option consumes multiple space-separated tokens after one
/// occurrence (rfl::MultiToken).
bool multi_token = false;

/// Whether the option carries a sensitive value (rfl::cli::attr::Sensitive)
/// that should be overwritten with `'*'` in mutable `char* argv[]`.
bool sensitive = false;
};

/// The options that a target struct accepts, built once per <T, ProcessorsType>
Expand Down Expand Up @@ -167,6 +173,14 @@ struct Options {
/// token (rfl::cli::PureFlags).
bool pure_flags = false;

/// Whether repeated non-list options overwrite their previous value instead
/// of failing (rfl::cli::LastValueWins).
bool last_value_wins = false;

/// Whether long value options require `=` when `separate_values` is false
/// (rfl::cli::RequireEquals).
bool require_equals = false;

/// Records one leaf long option along with its optional hidden/group/value_name
/// metadata. Non-template to avoid per-field template instantiations.
void add_leaf_option(std::string _path, const Option& _opt,
Expand Down Expand Up @@ -388,6 +402,7 @@ struct cli_annotations {
static constexpr bool has_group = false;
static constexpr bool has_value_name = false;
static constexpr bool is_multi_token = false;
static constexpr bool is_sensitive = false;
static constexpr size_t num_implicit_value = 0;
static constexpr rfl::internal::StringLiteral<1> short_name = "";
static constexpr int count = 0;
Expand Down Expand Up @@ -463,6 +478,8 @@ struct cli_annotations<Opt<T, Attrs...>> {
Info::has_value_name || Inner::has_value_name;
static constexpr bool is_multi_token =
Info::is_multi_token || Inner::is_multi_token;
static constexpr bool is_sensitive =
Info::is_sensitive || Inner::is_sensitive;
static constexpr size_t num_implicit_value =
Info::num_implicit_value + Inner::num_implicit_value;
static constexpr int count = Info::count + Inner::count;
Expand Down Expand Up @@ -664,7 +681,8 @@ Option implicit_value_option() {
"bool, a std::string or a list, such as "
"rfl::ImplicitValue<int, \"1\">.");
return Option{OptionKind::implicit_value, is_option_list_v<U>,
Annotations::implicit_value.string_view()};
Annotations::implicit_value.string_view(), false,
Annotations::is_sensitive};
}

/// The option for a field, or a key of a map or of rfl::ExtraFields, with
Expand All @@ -686,7 +704,8 @@ Option option_for() {
}
else {
return Option{OptionKind::value, is_option_list_v<U>, {},
cli_annotations_t<T>::is_multi_token};
cli_annotations_t<T>::is_multi_token,
cli_annotations_t<T>::is_sensitive};
}
}

Expand Down Expand Up @@ -792,7 +811,7 @@ void collect_options(const std::string& _prefix, Options* _options) {
_options->add_leaf_option(
std::move(path),
Option{OptionKind::value, is_option_list_v<U>, {},
Ann::is_multi_token},
Ann::is_multi_token, Ann::is_sensitive},
is_hidden, group_name, value_name);
}
}
Expand Down Expand Up @@ -870,6 +889,8 @@ Options make_options() {
options.allow_abbreviations = allow_abbreviations_v<ProcessorsType>;
options.options_first = options_first_v<ProcessorsType>;
options.pure_flags = pure_flags_v<ProcessorsType>;
options.last_value_wins = last_value_wins_v<ProcessorsType>;
options.require_equals = require_equals_v<ProcessorsType>;
using U = unwrapped_option_t<T, ProcessorsType>;
if constexpr (_all_paths) {
collect_value_options<T, ProcessorsType, type_list<>>(&options);
Expand Down
Loading