arg_router  1.4.0
C++ command line argument parsing and routing
arg.hpp
1 // Copyright (C) 2022-2023 by Camden Mannett.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
4 
5 #pragma once
6 
7 #include "arg_router/multi_arg_base.hpp"
8 #include "arg_router/policy/description.hpp"
9 #include "arg_router/policy/long_name.hpp"
10 #include "arg_router/policy/short_name.hpp"
11 #include "arg_router/utility/string_to_policy.hpp"
12 
13 namespace arg_router
14 {
21 template <typename T, typename... Policies>
22 class arg_t :
23  public multi_arg_base_t<T,
24  1,
25  policy::min_max_count_t<traits::integral_constant<std::size_t{1}>,
26  traits::integral_constant<std::size_t{1}>>,
27  std::decay_t<Policies>...>
28 {
29  using parent_type =
31  1,
33  traits::integral_constant<std::size_t{1}>>,
34  std::decay_t<Policies>...>;
35 
36  static_assert(!traits::has_none_name_method_v<arg_t>, "Arg must not have a none name policy");
37  static_assert(!traits::has_display_name_method_v<arg_t>,
38  "Arg must not have a display name policy");
39 
40 public:
41  using typename parent_type::policies_type;
42 
45 
47  template <bool Flatten>
48  using help_data_type = typename parent_type::template default_leaf_help_data_type<Flatten>;
49 
54  constexpr explicit arg_t(Policies... policies) noexcept :
56  traits::integral_constant<std::size_t{1}>>{},
57  std::move(policies)...}
58  {
59  }
60 
61  template <typename Validator, bool HasTarget, typename... Parents>
62  [[nodiscard]] std::optional<parsing::parse_target> pre_parse(
63  parsing::pre_parse_data<Validator, HasTarget> pre_parse_data,
64  const Parents&... parents) const
65 
66  {
67  return parent_type::pre_parse(pre_parse_data, *this, parents...);
68  }
69 
70  template <typename... Parents>
71  value_type parse(parsing::parse_target&& target, const Parents&... parents) const
72  {
73  return parent_type::parse(target, *this, parents...);
74  }
75 };
76 
97 template <typename T, typename... Policies>
98 [[nodiscard]] constexpr auto arg(Policies... policies) noexcept
99 {
100  return std::apply(
101  [](auto... converted_policies) {
102  return arg_t<T, std::decay_t<decltype(converted_policies)>...>{
103  std::move(converted_policies)...};
104  },
109  std::move(policies)...));
110 }
111 } // namespace arg_router
typename parent_type::value_type value_type
Definition: arg.hpp:44
typename parent_type::template default_leaf_help_data_type< Flatten > help_data_type
Definition: arg.hpp:48
constexpr arg_t(Policies... policies) noexcept
Definition: arg.hpp:54
std::integral_constant< decltype(Value), Value > integral_constant
Definition: traits.hpp:210
constexpr auto convert(Params &&... params) noexcept
constexpr auto arg(Policies... policies) noexcept
Definition: arg.hpp:98