arg_router  1.4.0
C++ command line argument parsing and routing
exception.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/basic_types.hpp"
8 #include "arg_router/traits.hpp"
9 #include "arg_router/utility/exception_formatter.hpp"
10 
11 #include <exception>
12 
13 namespace arg_router
14 {
33 enum class error_code : std::size_t {
34  // Common
35  unknown_argument = 0,
50 
51  // Builtin node specific
63 };
64 
68  using error_code_translations = std::tuple<
69  std::pair<traits::integral_constant<error_code::unknown_argument>,
70  AR_STRING("Unknown argument")>,
71  std::pair<traits::integral_constant<error_code::unhandled_arguments>,
72  AR_STRING("Unhandled arguments")>,
73  std::pair<traits::integral_constant<error_code::argument_has_already_been_set>,
74  AR_STRING("Argument has already been set")>,
75  std::pair<traits::integral_constant<error_code::failed_to_parse>,
76  AR_STRING("Failed to parse")>,
77  std::pair<traits::integral_constant<error_code::no_arguments_passed>,
78  AR_STRING("No arguments passed")>,
79  std::pair<traits::integral_constant<error_code::minimum_value_not_reached>,
80  AR_STRING("Minimum value not reached")>,
81  std::pair<traits::integral_constant<error_code::maximum_value_exceeded>,
82  AR_STRING("Maximum value exceeded")>,
83  std::pair<traits::integral_constant<error_code::minimum_count_not_reached>,
84  AR_STRING("Minimum count not reached")>,
85  std::pair<traits::integral_constant<error_code::maximum_count_exceeded>,
86  AR_STRING("Maximum count exceeded")>,
87  std::pair<traits::integral_constant<error_code::unknown_argument_with_suggestion>,
88  AR_STRING("Unknown argument: {}. Did you mean { }?")>,
89  std::pair<traits::integral_constant<error_code::mode_requires_arguments>,
90  AR_STRING("Mode requires arguments")>,
91  std::pair<traits::integral_constant<error_code::missing_required_argument>,
92  AR_STRING("Missing required argument")>,
93  std::pair<traits::integral_constant<error_code::too_few_values_for_alias>,
94  AR_STRING("Too few values for alias")>,
95  std::pair<traits::integral_constant<error_code::dependent_argument_missing>,
96  AR_STRING("Dependent argument missing (needs to be before the "
97  "requiring token on the command line)")>,
98  std::pair<traits::integral_constant<error_code::one_of_selected_type_mismatch>,
99  AR_STRING("Only one argument from a \"One Of\" can be used at once")>,
100  std::pair<traits::integral_constant<error_code::missing_value_separator>,
101  AR_STRING("Expected a value separator")>>;
102 };
103 
110 class multi_lang_exception : public std::exception
111 {
112 public:
117  explicit multi_lang_exception(error_code ec) noexcept : ec_{ec} {}
118 
124  multi_lang_exception(error_code ec, const parsing::token_type& token) : ec_{ec}, tokens_{token}
125  {
126  }
127 
134  ec_{ec}, tokens_(std::move(tokens))
135  {
136  }
137 
140  [[nodiscard]] error_code ec() const noexcept { return ec_; }
141 
144  [[nodiscard]] const vector<parsing::token_type>& tokens() const noexcept { return tokens_; }
145 
146 private:
147  error_code ec_;
149 };
150 
160 class parse_exception : public std::exception
161 {
162 public:
168  explicit parse_exception(const string& message,
169  const vector<parsing::token_type>& tokens = {}) :
170  message_{message + (tokens.empty() ? "" : ": " + parsing::to_string(tokens))}
171  {
172  }
173 
179  parse_exception(const string& message, const parsing::token_type& token) :
180  message_{message + ": " + parsing::to_string(token)}
181  {
182  }
183 
190  template <typename S>
192  const vector<parsing::token_type>& tokens = {}) :
193  message_{cts.format(tokens)}
194  {
195  }
196 
197  [[nodiscard]] const char* what() const noexcept override { return message_.data(); }
198 
199 private:
200  string message_;
201 };
202 } // namespace arg_router
error_code ec() const noexcept
Definition: exception.hpp:140
multi_lang_exception(error_code ec, vector< parsing::token_type > tokens) noexcept
Definition: exception.hpp:133
multi_lang_exception(error_code ec, const parsing::token_type &token)
Definition: exception.hpp:124
multi_lang_exception(error_code ec) noexcept
Definition: exception.hpp:117
const vector< parsing::token_type > & tokens() const noexcept
Definition: exception.hpp:144
parse_exception(utility::exception_formatter< S > cts, const vector< parsing::token_type > &tokens={})
Definition: exception.hpp:191
parse_exception(const string &message, const parsing::token_type &token)
Definition: exception.hpp:179
parse_exception(const string &message, const vector< parsing::token_type > &tokens={})
Definition: exception.hpp:168
static string format(const vector< parsing::token_type > &tokens)
constexpr std::string_view to_string(prefix_type prefix) noexcept
Definition: token_type.hpp:25
std::vector< T, config::allocator< T > > vector
Definition: basic_types.hpp:39
@ minimum_count_not_reached
Minimum number of value tokens for node not reached.
@ failed_to_parse
A value token could not be converted into its target value.
@ maximum_count_exceeded
Maximum number of value tokens exceeded.
@ missing_required_argument
A node has been marked as required but no token matches it.
@ minimum_value_not_reached
Parsed value is below the minimum.
@ maximum_value_exceeded
Parsed value is above the maximum.