TAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Exceptions.hpp
Go to the documentation of this file.
1 
25 #pragma once
26 
27 #include <vector>
28 #include <stdexcept>
29 #include <memory>
30 
31 namespace TAP {
32 
36 class exception : public std::exception {
37 protected:
39  std::string m_what;
40 
41 public:
46  exception(std::string what) : std::exception(), m_what(what) {};
47 
51  exception() : std::exception(), m_what() {};
52 
56  const char* what() const noexcept override {
57  return m_what.c_str();
58  }
59 };
60 
65 class command_error: public exception {
66 public:
68 };
69 
74 public:
79  command_error("No positional arguments are supported") {
80  }
81 
86  unknown_argument(char flag) :
87  command_error(std::string("The flag argument ") + flag + " is unknown") {
88  }
89 
94  unknown_argument(std::string name) :
95  command_error(std::string("The named argument ") + name + " is unknown") {
96  }
97 };
98 
103 class argument_error : public exception {
104 protected:
106  std::unique_ptr<Argument> m_arg;
107 
108 public:
114  argument_error(const Argument& arg);
115 
121  argument_error(const Argument& arg, const std::string& reason);
122 
127  const Argument& arg() const {
128  return *m_arg;
129  }
130 };
131 
137 public:
146  argument_count_mismatch(const Argument& arg, unsigned int count, unsigned int expected) :
147  argument_error(arg) {
148  if (count < expected) {
149  if (expected > 1) {
150  m_what += " is required to occur at least " + std::to_string(expected) + " times";
151  } else {
152  m_what += " is required";
153  }
154  } else {
155  if (expected > 1) {
156  m_what += " can occur at most " + std::to_string(expected) + " times";
157  } else {
158  m_what += " can only be set once";
159  }
160  }
161  }
162 };
163 
168 public:
174  argument_invalid_value(const Argument& arg, const std::string& value) :
175  argument_error(arg, std::string("does not accept the value ") + value) {
176  }
177 };
178 
184 public:
189  argument_error(arg, "requires a value") {
190  }
191 };
192 
197 public:
203  argument_error(arg, "does not accept a value") {
204  }
205 };
206 
210 class constraint_error : public exception {
211 public:
218  constraint_error(const std::string& reason, const std::vector<const BaseArgument*>& args);
219 };
220 }
argument_invalid_value(const Argument &arg, const std::string &value)
Creates the exception for the given argument, with the incorrect value.
Definition: Exceptions.hpp:174
Exception class raised when an argument value is given but not expected.
Definition: Exceptions.hpp:196
Exception class raised when an error occurs verifying a command line argument.
Definition: Exceptions.hpp:103
std::string m_what
Description of the exception.
Definition: Exceptions.hpp:39
argument_count_mismatch(const Argument &arg, unsigned int count, unsigned int expected)
Creates the exception for the given argument, with the number of occurrences and the expected number ...
Definition: Exceptions.hpp:146
exception(std::string what)
Creates the exception with the given message.
Definition: Exceptions.hpp:46
std::unique_ptr< Argument > m_arg
Copy of the argument involved in the error.
Definition: Exceptions.hpp:106
unknown_argument(char flag)
Creates the exception for flag arguments.
Definition: Exceptions.hpp:86
Simple argument class.
Definition: Argument.hpp:48
constraint_error(const std::string &reason, const std::vector< const BaseArgument * > &args)
Creates the exception with a reason for a constraint failure, and the list of arguments involved...
Standard exception class for TAP.
Definition: Exceptions.hpp:36
unknown_argument(std::string name)
Creates the exception for name arguments.
Definition: Exceptions.hpp:94
argument_no_value(const Argument &arg)
Creates the exception for the given argument, which was given a value but does not accept one...
Definition: Exceptions.hpp:202
argument_missing_value(const Argument &arg)
Creates the exception for the given argument, which misses a value.
Definition: Exceptions.hpp:188
Exception class raised when an argument value is incorrect.
Definition: Exceptions.hpp:167
Exception class raised when an argument is used an incorrect amount of times.
Definition: Exceptions.hpp:136
Exception class raised when an argument value is missing from the command line.
Definition: Exceptions.hpp:183
const char * what() const noexceptoverride
See std::exception::what().
Definition: Exceptions.hpp:56
Exception class raised when an unknown argument is encountered.
Definition: Exceptions.hpp:73
const Argument & arg() const
Returns the argument triggering the error.
Definition: Exceptions.hpp:127
Exception class for errors in the command line (such as unknown arguments or syntax errors)...
Definition: Exceptions.hpp:65
unknown_argument()
Creates the exception for positional arguments.
Definition: Exceptions.hpp:78
argument_error(const Argument &arg)
Creates the exception for the given argument, without a specified reason.
exception()
Creates the exception with no message.
Definition: Exceptions.hpp:51
Exception class raised when an argument constraint is not satisfied.
Definition: Exceptions.hpp:210