Main MRPT website > C++ reference
MRPT logo
ArgException.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: ArgException.h
5  *
6  * Copyright (c) 2003, Michael E. Smoot .
7  * All rights reverved.
8  *
9  * See the file COPYING in the top directory of this distribution for
10  * more information.
11  *
12  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
13  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18  * DEALINGS IN THE SOFTWARE.
19  *
20  *****************************************************************************/
21 
22 
23 #ifndef TCLAP_ARG_EXCEPTION_H
24 #define TCLAP_ARG_EXCEPTION_H
25 
26 #include <string>
27 #include <exception>
28 
29 namespace TCLAP {
30 
31 /**
32  * A simple class that defines and argument exception. Should be caught
33  * whenever a CmdLine is created and parsed.
34  */
35 class ArgException : public std::exception
36 {
37  public:
38 
39  /**
40  * Constructor.
41  * \param text - The text of the exception.
42  * \param id - The text identifying the argument source.
43  * \param td - Text describing the type of ArgException it is.
44  * of the exception.
45  */
46  ArgException( const std::string& text = "undefined exception",
47  const std::string& id = "undefined",
48  const std::string& td = "Generic ArgException")
49  : std::exception(),
50  _errorText(text),
51  _argId( id ),
53  { }
54 
55  /**
56  * Destructor.
57  */
58  virtual ~ArgException() throw() { }
59 
60  /**
61  * Returns the error text.
62  */
63  std::string error() const { return ( _errorText ); }
64 
65  /**
66  * Returns the argument id.
67  */
68  std::string argId() const
69  {
70  if ( _argId == "undefined" )
71  return " ";
72  else
73  return ( "Argument: " + _argId );
74  }
75 
76  /**
77  * Returns the arg id and error text.
78  */
79  const char* what() const throw()
80  {
81  static std::string ex;
82  ex = _argId + " -- " + _errorText;
83  return ex.c_str();
84  }
85 
86  /**
87  * Returns the type of the exception. Used to explain and distinguish
88  * between different child exceptions.
89  */
90  std::string typeDescription() const
91  {
92  return _typeDescription;
93  }
94 
95 
96  private:
97 
98  /**
99  * The text of the exception message.
100  */
101  std::string _errorText;
102 
103  /**
104  * The argument related to this exception.
105  */
106  std::string _argId;
107 
108  /**
109  * Describes the type of the exception. Used to distinguish
110  * between different child exceptions.
111  */
112  std::string _typeDescription;
113 
114 };
115 
116 /**
117  * Thrown from within the child Arg classes when it fails to properly
118  * parse the argument it has been passed.
119  */
121 {
122  public:
123  /**
124  * Constructor.
125  * \param text - The text of the exception.
126  * \param id - The text identifying the argument source
127  * of the exception.
128  */
129  ArgParseException( const std::string& text = "undefined exception",
130  const std::string& id = "undefined" )
131  : ArgException( text,
132  id,
133  std::string( "Exception found while parsing " ) +
134  std::string( "the value the Arg has been passed." ))
135  { }
136 };
137 
138 /**
139  * Thrown from CmdLine when the arguments on the command line are not
140  * properly specified, e.g. too many arguments, required argument missing, etc.
141  */
143 {
144  public:
145  /**
146  * Constructor.
147  * \param text - The text of the exception.
148  * \param id - The text identifying the argument source
149  * of the exception.
150  */
151  CmdLineParseException( const std::string& text = "undefined exception",
152  const std::string& id = "undefined" )
153  : ArgException( text,
154  id,
155  std::string( "Exception found when the values ") +
156  std::string( "on the command line do not meet ") +
157  std::string( "the requirements of the defined ") +
158  std::string( "Args." ))
159  { }
160 };
161 
162 /**
163  * Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
164  * same flag as another Arg, same name, etc.
165  */
167 {
168  public:
169  /**
170  * Constructor.
171  * \param text - The text of the exception.
172  * \param id - The text identifying the argument source
173  * of the exception.
174  */
175  SpecificationException( const std::string& text = "undefined exception",
176  const std::string& id = "undefined" )
177  : ArgException( text,
178  id,
179  std::string("Exception found when an Arg object ")+
180  std::string("is improperly defined by the ") +
181  std::string("developer." ))
182  { }
183 
184 };
185 
186 /**
187  * (Added by JLBC for MRPT): An exception that indicates to CmdLine::parse that
188  * help,version,... have been invoked so it should return false for the main program to exit.
189  */
190 class ActionDoneException : public std::runtime_error
191 {
192 public:
193  ActionDoneException(const std::string &text = std::string() ) :
194  std::runtime_error(text.c_str())
195  {
196  }
197 };
198 
199 
200 } // namespace TCLAP
201 
202 #endif
203 



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013