arg_router  1.4.0
C++ command line argument parsing and routing
multi_arg.hpp
1 // Copyright (C) 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 {
23 template <typename T, typename... Policies>
24 class multi_arg_t : public multi_arg_base_t<T, 1, std::decay_t<Policies>...>
25 {
27 
28  static_assert(!traits::has_none_name_method_v<multi_arg_t>,
29  "Multi arg must not have a none name policy");
30  static_assert(!traits::has_display_name_method_v<multi_arg_t>,
31  "Multi arg must not have a display name policy");
32 
33  static_assert(!((parent_type::minimum_count() == parent_type::maximum_count()) &&
34  parent_type::minimum_count() == 1),
35  "Multi arg set to a fixed count of 1, use arg_t instead");
36  static_assert(
37  parent_type::minimum_count() >= 1,
38  "Multi arg requires a minimum of one value token, use min_max_count_t to define the range");
39 
40 public:
41  using typename parent_type::policies_type;
42 
45 
47  template <bool Flatten>
49  {
50  public:
51  using label = std::decay_t<decltype(
52  parent_type::template default_leaf_help_data_type<Flatten>::label_generator() +
53  AR_STRING(" "){} +
54  parent_type::template default_leaf_help_data_type<Flatten>::count_suffix())>;
55  using description =
56  typename parent_type::template default_leaf_help_data_type<Flatten>::description;
57  using children = std::tuple<>;
58  };
59 
64  constexpr explicit multi_arg_t(Policies... policies) noexcept :
65  parent_type{std::move(policies)...}
66  {
67  }
68 
69  template <typename Validator, bool HasTarget, typename... Parents>
70  [[nodiscard]] std::optional<parsing::parse_target> pre_parse(
71  parsing::pre_parse_data<Validator, HasTarget> pre_parse_data,
72  const Parents&... parents) const
73 
74  {
75  return parent_type::pre_parse(pre_parse_data, *this, parents...);
76  }
77 
78  template <typename... Parents>
79  value_type parse(parsing::parse_target&& target, const Parents&... parents) const
80  {
81  return parent_type::parse(target, *this, parents...);
82  }
83 
84 private:
85  static_assert(!parent_type::template any_phases_v<value_type, policy::has_routing_phase_method>,
86  "Multi arg does not support policies with routing phases "
87  "(e.g. router)");
88 };
89 
110 template <typename T, typename... Policies>
111 [[nodiscard]] constexpr auto multi_arg(Policies... policies) noexcept
112 {
113  return std::apply(
114  [](auto... converted_policies) {
115  return multi_arg_t<T, std::decay_t<decltype(converted_policies)>...>{
116  std::move(converted_policies)...};
117  },
122  std::move(policies)...));
123 }
124 } // namespace arg_router
value_type parse(parsing::parse_target target, const Parents &... parents) const
typename parent_type::value_type value_type
Definition: multi_arg.hpp:44
constexpr multi_arg_t(Policies... policies) noexcept
Definition: multi_arg.hpp:64
constexpr auto convert(Params &&... params) noexcept
constexpr auto multi_arg(Policies... policies) noexcept
Definition: multi_arg.hpp:111