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/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "cli/App.hpp"
#include "cli/KeepEmptyItems.hpp"
#include "cli/ListDelimiter.hpp"
#include "cli/Opt.hpp"
#include "cli/OptionsFirst.hpp"
#include "cli/Parser.hpp"
#include "cli/PureFlags.hpp"
Expand Down
204 changes: 204 additions & 0 deletions include/rfl/cli/Opt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
#ifndef RFL_CLI_OPT_HPP_
#define RFL_CLI_OPT_HPP_

#include <cstddef>
#include <string_view>
#include <type_traits>

#include "../internal/StringLiteral.hpp"
#include "../internal/TransparentWrapper.hpp"

namespace rfl::cli {

/// Attribute tags accepted by `rfl::cli::Opt<T, Attrs...>` in any order.
namespace attr {

template <rfl::internal::StringLiteral _name>
struct Short {
static constexpr auto value = _name;
};

struct Positional {};

struct Hidden {};

template <rfl::internal::StringLiteral _group>
struct Group {
static constexpr auto value = _group;
};

template <rfl::internal::StringLiteral _name>
struct ValueName {
static constexpr auto value = _name;
};

template <rfl::internal::StringLiteral _val = "">
struct Implicit {
static constexpr auto value = _val;
};

struct MultiToken {};

template <rfl::internal::StringLiteral _desc>
struct Desc {
static constexpr auto value = _desc;
};

template <rfl::internal::StringLiteral _msg,
rfl::internal::StringLiteral _desc = "">
struct Deprecated {
static constexpr auto message = _msg;
static constexpr auto description = _desc;
};

template <rfl::internal::StringLiteral _hint>
struct DefaultHint {
static constexpr auto value = _hint;
};

} // namespace attr

namespace internal {

template <class... Attrs>
struct opt_attrs;

template <>
struct opt_attrs<> {
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 bool has_value_name = false;
static constexpr bool is_multi_token = false;
static constexpr size_t num_implicit_value = 0;
static constexpr int count = 0;
static constexpr rfl::internal::StringLiteral<1> short_name = "";
static constexpr rfl::internal::StringLiteral<1> implicit_value = "";
static constexpr rfl::internal::StringLiteral<1> group_name = "";
static constexpr rfl::internal::StringLiteral<1> value_name = "";
static constexpr std::string_view description = {};
static constexpr bool is_deprecated = false;
static constexpr std::string_view deprecation_message = {};
static constexpr std::string_view default_hint = {};
};

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).");
};

template <rfl::internal::StringLiteral _name, class... Rest>
struct opt_attrs<attr::Short<_name>, Rest...> : opt_attrs<Rest...> {
using Next = opt_attrs<Rest...>;
static constexpr bool is_short = true;
static constexpr bool is_positional = false;
static constexpr auto short_name = _name;
static constexpr int count = Next::count + 1;
};

template <class... Rest>
struct opt_attrs<attr::Positional, Rest...> : opt_attrs<Rest...> {
using Next = opt_attrs<Rest...>;
static constexpr bool is_short = false;
static constexpr bool is_positional = true;
static constexpr int count = Next::count + 1;
};

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

template <rfl::internal::StringLiteral _group, class... Rest>
struct opt_attrs<attr::Group<_group>, Rest...> : opt_attrs<Rest...> {
static constexpr bool has_group = true;
static constexpr auto group_name = _group;
};

template <rfl::internal::StringLiteral _name, class... Rest>
struct opt_attrs<attr::ValueName<_name>, Rest...> : opt_attrs<Rest...> {
static constexpr bool has_value_name = true;
static constexpr auto value_name = _name;
};

template <rfl::internal::StringLiteral _val, class... Rest>
struct opt_attrs<attr::Implicit<_val>, Rest...> : opt_attrs<Rest...> {
using Next = opt_attrs<Rest...>;
static constexpr size_t num_implicit_value = Next::num_implicit_value + 1;
static constexpr auto implicit_value = _val;
};

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

template <rfl::internal::StringLiteral _desc, class... Rest>
struct opt_attrs<attr::Desc<_desc>, Rest...> : opt_attrs<Rest...> {
using Next = opt_attrs<Rest...>;
static constexpr std::string_view description =
_desc.string_view().empty() ? Next::description : _desc.string_view();
};

template <rfl::internal::StringLiteral _msg,
rfl::internal::StringLiteral _desc, class... Rest>
struct opt_attrs<attr::Deprecated<_msg, _desc>, Rest...> : opt_attrs<Rest...> {
using Next = opt_attrs<Rest...>;
static constexpr std::string_view description =
_desc.string_view().empty() ? Next::description : _desc.string_view();
static constexpr bool is_deprecated = true;
static constexpr std::string_view deprecation_message =
Next::is_deprecated ? Next::deprecation_message : _msg.string_view();
};

template <rfl::internal::StringLiteral _hint, class... Rest>
struct opt_attrs<attr::DefaultHint<_hint>, Rest...> : opt_attrs<Rest...> {
using Next = opt_attrs<Rest...>;
static constexpr std::string_view default_hint =
_hint.string_view().empty() ? Next::default_hint : _hint.string_view();
};

} // namespace internal

/// Single-layer variadic CLI option wrapper carrying any combination of
/// `rfl::cli::attr::*` metadata tags in any order, replacing deeply nested
/// `Group<Short<ValueName<Description<T>>>>` stacks with one wrapper.
///
/// Example:
/// ```cpp
/// using namespace rfl::cli::attr;
/// rfl::cli::Opt<std::optional<std::string>,
/// Short<"o">, Group<"Output">, ValueName<"path">,
/// Desc<"Output directory">> output_dir;
/// ```
template <class T, class... Attrs>
struct Opt : rfl::internal::TransparentWrapper<Opt<T, Attrs...>, T> {
using Base = rfl::internal::TransparentWrapper<Opt<T, Attrs...>, T>;
using Base::Base;
using Base::operator=;

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

namespace internal {

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

template <class T, class... Attrs>
struct is_cli_opt<Opt<T, Attrs...>> : std::true_type {};

template <class T>
constexpr bool is_cli_opt_v =
is_cli_opt<std::remove_cvref_t<std::remove_pointer_t<T>>>::value;

} // namespace internal

} // namespace rfl::cli

#endif
Loading