tclap 1.6.0
ArgException.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: ArgException.h
6 *
7 * Copyright (c) 2003, Michael E. Smoot .
8 * Copyright (c) 2017 Google LLC
9 * All rights reserved.
10 *
11 * See the file COPYING in the top directory of this distribution for
12 * more information.
13 *
14 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 *****************************************************************************/
23
24#ifndef TCLAP_ARG_EXCEPTION_H
25#define TCLAP_ARG_EXCEPTION_H
26
27#include <exception>
28#include <string>
29#include <utility>
30
31namespace TCLAP {
32
37class ArgException : public std::exception {
38public:
46 ArgException(std::string text = "undefined exception",
47 std::string id = "undefined",
48 std::string td = "Generic ArgException")
49 : std::exception(),
50 _errorText(std::move(text)),
51 _argId(std::move(id)),
52 _typeDescription(std::move(td)),
53 _what(_argId + " -- " + _errorText) {}
54
58 ~ArgException() noexcept override = default;
59
63 [[nodiscard]] std::string error() const { return (_errorText); }
64
68 [[nodiscard]] std::string argId() const {
69 if (_argId == "undefined")
70 return " ";
71 else
72 return ("Argument: " + _argId);
73 }
74
78 [[nodiscard]] const char *what() const noexcept override {
79 return _what.c_str();
80 }
81
86 [[nodiscard]] std::string typeDescription() const { return _typeDescription; }
87
88private:
92 std::string _errorText;
93
97 std::string _argId;
98
103 std::string _typeDescription;
104
105 // Prepared in the constructor: what() cannot allocate or throw.
106 std::string _what;
107};
108
114public:
121 ArgParseException(const std::string &text = "undefined exception",
122 const std::string &id = "undefined")
123 : ArgException(text, id,
124 std::string("Exception found while parsing ") +
125 std::string("the value the Arg has been passed.")) {}
126};
127
133public:
140 CmdLineParseException(const std::string &text = "undefined exception",
141 const std::string &id = "undefined")
142 : ArgException(text, id,
143 std::string("Exception found when the values ") +
144 std::string("on the command line do not meet ") +
145 std::string("the requirements of the defined ") +
146 std::string("Args.")) {}
147};
148
154public:
161 SpecificationException(const std::string &text = "undefined exception",
162 const std::string &id = "undefined")
163 : ArgException(text, id,
164 std::string("Exception found when an Arg object ") +
165 std::string("is improperly defined by the ") +
166 std::string("developer.")) {}
167};
168
182public:
183 explicit ExitException(int estat) : _estat(estat) {}
184
185 [[nodiscard]] int getExitStatus() const noexcept { return _estat; }
186
187private:
188 int _estat;
189};
190
191} // namespace TCLAP
192
193#endif // TCLAP_ARG_EXCEPTION_H
A simple class that defines and argument exception.
~ArgException() noexcept override=default
Destructor.
const char * what() const noexcept override
Returns the arg id and error text.
std::string typeDescription() const
Returns the type of the exception.
std::string argId() const
Returns the argument id.
std::string error() const
Returns the error text.
ArgException(std::string text="undefined exception", std::string id="undefined", std::string td="Generic ArgException")
Constructor.
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
ArgParseException(const std::string &text="undefined exception", const std::string &id="undefined")
Constructor.
Thrown from CmdLine when the arguments on the command line are not properly specified,...
CmdLineParseException(const std::string &text="undefined exception", const std::string &id="undefined")
Constructor.
Thrown when TCLAP thinks the program should exit.
int getExitStatus() const noexcept
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
SpecificationException(const std::string &text="undefined exception", const std::string &id="undefined")
Constructor.
Definition Arg.h:54