arg_router  1.4.0
C++ command line argument parsing and routing
token_type.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/traits.hpp"
8 #include "arg_router/utility/string_view_ops.hpp"
9 
10 namespace arg_router::parsing
11 {
13 enum class prefix_type : std::uint8_t {
14  long_,
15  short_,
16  none
17 };
18 
25 [[nodiscard]] constexpr std::string_view to_string(prefix_type prefix) noexcept
26 {
27  switch (prefix) {
28  case prefix_type::long_: return config::long_prefix;
30  default: return "";
31  }
32 }
33 
36 struct token_type {
42  constexpr token_type(prefix_type p, std::string_view n) noexcept : prefix{p}, name{n} {}
43 
49  [[nodiscard]] constexpr bool operator==(const token_type& other) const noexcept
50  {
51  return prefix == other.prefix && name == other.name;
52  }
53 
59  [[nodiscard]] constexpr bool operator!=(const token_type& other) const noexcept
60  {
61  return !(*this == other);
62  }
63 
65  std::string_view name;
66 };
67 
74 [[nodiscard]] inline string to_string(const token_type& token)
75 {
76  using namespace utility::string_view_ops;
77  return to_string(token.prefix) + token.name;
78 }
79 
85 [[nodiscard]] inline string to_string(const vector<token_type>& view)
86 {
87  auto str = string{};
88  for (auto i = 0u; i < view.size(); ++i) {
89  str += to_string(view[i]);
90  if (i != (view.size() - 1)) {
91  str += ", ";
92  }
93  }
94  return str;
95 }
96 
103 [[nodiscard]] inline token_type get_token_type(std::string_view token)
104 {
105  using namespace config;
106 
107  if (token.substr(0, long_prefix.size()) == long_prefix) {
108  token.remove_prefix(long_prefix.size());
109  return {prefix_type::long_, token};
110  }
111  if (token.substr(0, short_prefix.size()) == short_prefix) {
112  token.remove_prefix(short_prefix.size());
113  return {prefix_type::short_, token};
114  }
115  return {prefix_type::none, token};
116 }
117 
126 template <typename Node>
127 [[nodiscard]] inline token_type get_token_type([[maybe_unused]] const Node& node,
128  std::string_view token)
129 {
130  using namespace config;
131 
132  if constexpr (traits::has_long_name_method_v<Node>) {
133  if (token.substr(0, long_prefix.size()) == long_prefix) {
134  token.remove_prefix(long_prefix.size());
135  return {prefix_type::long_, token};
136  }
137  }
138  if constexpr (traits::has_short_name_method_v<Node>) {
139  if ((token.substr(0, short_prefix.size()) == short_prefix) &&
140  (token.substr(0, long_prefix.size()) != long_prefix)) {
141  token.remove_prefix(short_prefix.size());
142  return {prefix_type::short_, token};
143  }
144  }
145  return {prefix_type::none, token};
146 }
147 } // namespace arg_router::parsing
constexpr auto short_prefix
Definition: config.hpp:58
constexpr auto long_prefix
Definition: config.hpp:52
constexpr std::string_view to_string(prefix_type prefix) noexcept
Definition: token_type.hpp:25
token_type get_token_type(std::string_view token)
Definition: token_type.hpp:103
std::vector< T, config::allocator< T > > vector
Definition: basic_types.hpp:39
std::string_view name
Token name, stripped of prefix (if any)
Definition: token_type.hpp:65
constexpr bool operator==(const token_type &other) const noexcept
Definition: token_type.hpp:49
prefix_type prefix
Prefix type.
Definition: token_type.hpp:64
constexpr token_type(prefix_type p, std::string_view n) noexcept
Definition: token_type.hpp:42
constexpr bool operator!=(const token_type &other) const noexcept
Definition: token_type.hpp:59