summaryrefslogtreecommitdiffstats
path: root/src/tclap/ArgException.h
blob: ea08bd6c9560cbf817c2ba762b5f207785fddc1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/******************************************************************************
 *
 *  file:  ArgException.h
 *
 *  Copyright (c) 2003, Michael E. Smoot .
 *  All rights reverved.
 *
 *  See the file COPYING in the top directory of this distribution for
 *  more information.
 *
 *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 *
 *****************************************************************************/


#ifndef TCLAP_ARG_EXCEPTION_H
#define TCLAP_ARG_EXCEPTION_H

#include <string>
#include <exception>

namespace TCLAP
{
	/**
	 * A simple class that defines and argument exception.  Should be caught
	 * whenever a CmdLine is created and parsed.
	 */
	class ArgException : public std::exception
	{
		public:
			/**
			 * Constructor.
			 * \param text - The text of the exception.
			 * \param id - The text identifying the argument source.
			 * \param td - Text describing the type of ArgException it is.
			 * of the exception.
			 */
			ArgException(const std::string &text = "undefined exception",
			        const std::string &id = "undefined",
			        const std::string &td = "Generic ArgException") :
					std::exception(), _errorText(text), _argId(id), _typeDescription(td)
			{
			}

			/**
			 * Destructor.
			 */
			virtual ~ArgException() throw ()
			{
			}

			/**
			 * Returns the error text.
			 */
			std::string error() const
			{
				return _errorText;
			}

			/**
			 * Returns the argument id.
			 */
			std::string argId() const
			{
				if (_argId == "undefined")
				{
					return " ";
				}
				else
				{
					return "Argument: " + _argId;
				}
			}

			/**
			 * Returns the arg id and error text.
			 */
			const char* what() const throw ()
			{
				static std::string ex;
				ex = _argId + " -- " + _errorText;
				return ex.c_str();
			}

			/**
			 * Returns the type of the exception.  Used to explain and distinguish
			 * between different child exceptions.
			 */
			std::string typeDescription() const
			{
				return _typeDescription;
			}

		private:
			/**
			 * The text of the exception message.
			 */
			std::string _errorText;

			/**
			 * The argument related to this exception.
			 */
			std::string _argId;

			/**
			 * Describes the type of the exception.  Used to distinguish
			 * between different child exceptions.
			 */
			std::string _typeDescription;
	};

	/**
	 * Thrown from within the child Arg classes when it fails to properly
	 * parse the argument it has been passed.
	 */
	class ArgParseException : public ArgException
	{
		public:
			/**
			 * Constructor.
			 * \param text - The text of the exception.
			 * \param id - The text identifying the argument source
			 * of the exception.
			 */
			ArgParseException(const std::string &text = "undefined exception",
			        const std::string &id = "undefined") :
					ArgException(text, id, std::string("Exception found while parsing ") +
					        std::string("the value the Arg has been passed."))
			{
			}
	};

	/**
	 * Thrown from CmdLine when the arguments on the command line are not
	 * properly specified, e.g. too many arguments, required argument missing, etc.
	 */
	class CmdLineParseException : public ArgException
	{
		public:
			/**
			 * Constructor.
			 * \param text - The text of the exception.
			 * \param id - The text identifying the argument source
			 * of the exception.
			 */
			CmdLineParseException(const std::string &text = "undefined exception",
			        const std::string &id = "undefined") :
					ArgException(text, id, std::string("Exception found when the values ") +
					        std::string("on the command line do not meet ") +
					        std::string("the requirements of the defined ") + std::string("Args."))
			{
			}
	};

	/**
	 * Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
	 * same flag as another Arg, same name, etc.
	 */
	class SpecificationException : public ArgException
	{
		public:
			/**
			 * Constructor.
			 * \param text - The text of the exception.
			 * \param id - The text identifying the argument source
			 * of the exception.
			 */
			SpecificationException(const std::string &text = "undefined exception",
			        const std::string &id = "undefined") :
					ArgException(text, id, std::string("Exception found when an Arg object ") +
					        std::string("is improperly defined by the ") + std::string("developer."))
			{
			}
	};

	class ExitException
	{
		public:
			ExitException(int estat) :
					_estat(estat)
			{
			}

			int getExitStatus() const
			{
				return _estat;
			}

		private:
			int _estat;
	};
}

#endif