Main MRPT website > C++ reference
MRPT logo

ArgException.h

Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  *
00004  *  file:  ArgException.h
00005  *
00006  *  Copyright (c) 2003, Michael E. Smoot .
00007  *  All rights reverved.
00008  *
00009  *  See the file COPYING in the top directory of this distribution for
00010  *  more information.
00011  *
00012  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
00013  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00014  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00015  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00016  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00017  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00018  *  DEALINGS IN THE SOFTWARE.
00019  *
00020  *****************************************************************************/
00021 
00022 
00023 #ifndef TCLAP_ARG_EXCEPTION_H
00024 #define TCLAP_ARG_EXCEPTION_H
00025 
00026 #include <string>
00027 #include <exception>
00028 
00029 namespace TCLAP {
00030 
00031 /**
00032  * A simple class that defines and argument exception.  Should be caught
00033  * whenever a CmdLine is created and parsed.
00034  */
00035 class ArgException : public std::exception
00036 {
00037         public:
00038 
00039                 /**
00040                  * Constructor.
00041                  * \param text - The text of the exception.
00042                  * \param id - The text identifying the argument source.
00043                  * \param td - Text describing the type of ArgException it is.
00044                  * of the exception.
00045                  */
00046                 ArgException( const std::string& text = "undefined exception",
00047                                           const std::string& id = "undefined",
00048                                           const std::string& td = "Generic ArgException")
00049                         : std::exception(),
00050                           _errorText(text),
00051                           _argId( id ),
00052                           _typeDescription(td)
00053                 { }
00054 
00055                 /**
00056                  * Destructor.
00057                  */
00058                 virtual ~ArgException() throw() { }
00059 
00060                 /**
00061                  * Returns the error text.
00062                  */
00063                 std::string error() const { return ( _errorText ); }
00064 
00065                 /**
00066                  * Returns the argument id.
00067                  */
00068                 std::string argId() const
00069                 {
00070                         if ( _argId == "undefined" )
00071                                 return " ";
00072                         else
00073                                 return ( "Argument: " + _argId );
00074                 }
00075 
00076                 /**
00077                  * Returns the arg id and error text.
00078                  */
00079                 const char* what() const throw()
00080                 {
00081                         static std::string ex;
00082                         ex = _argId + " -- " + _errorText;
00083                         return ex.c_str();
00084                 }
00085 
00086                 /**
00087                  * Returns the type of the exception.  Used to explain and distinguish
00088                  * between different child exceptions.
00089                  */
00090                 std::string typeDescription() const
00091                 {
00092                         return _typeDescription;
00093                 }
00094 
00095 
00096         private:
00097 
00098                 /**
00099                  * The text of the exception message.
00100                  */
00101                 std::string _errorText;
00102 
00103                 /**
00104                  * The argument related to this exception.
00105                  */
00106                 std::string _argId;
00107 
00108                 /**
00109                  * Describes the type of the exception.  Used to distinguish
00110                  * between different child exceptions.
00111                  */
00112                 std::string _typeDescription;
00113 
00114 };
00115 
00116 /**
00117  * Thrown from within the child Arg classes when it fails to properly
00118  * parse the argument it has been passed.
00119  */
00120 class ArgParseException : public ArgException
00121 {
00122         public:
00123                 /**
00124                  * Constructor.
00125                  * \param text - The text of the exception.
00126                  * \param id - The text identifying the argument source
00127                  * of the exception.
00128                  */
00129                 ArgParseException( const std::string& text = "undefined exception",
00130                                                const std::string& id = "undefined" )
00131                         : ArgException( text,
00132                                         id,
00133                                                         std::string( "Exception found while parsing " ) +
00134                                                         std::string( "the value the Arg has been passed." ))
00135                         { }
00136 };
00137 
00138 /**
00139  * Thrown from CmdLine when the arguments on the command line are not
00140  * properly specified, e.g. too many arguments, required argument missing, etc.
00141  */
00142 class CmdLineParseException : public ArgException
00143 {
00144         public:
00145                 /**
00146                  * Constructor.
00147                  * \param text - The text of the exception.
00148                  * \param id - The text identifying the argument source
00149                  * of the exception.
00150                  */
00151                 CmdLineParseException( const std::string& text = "undefined exception",
00152                                                    const std::string& id = "undefined" )
00153                         : ArgException( text,
00154                                         id,
00155                                                         std::string( "Exception found when the values ") +
00156                                                         std::string( "on the command line do not meet ") +
00157                                                         std::string( "the requirements of the defined ") +
00158                                                         std::string( "Args." ))
00159                 { }
00160 };
00161 
00162 /**
00163  * Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
00164  * same flag as another Arg, same name, etc.
00165  */
00166 class SpecificationException : public ArgException
00167 {
00168         public:
00169                 /**
00170                  * Constructor.
00171                  * \param text - The text of the exception.
00172                  * \param id - The text identifying the argument source
00173                  * of the exception.
00174                  */
00175                 SpecificationException( const std::string& text = "undefined exception",
00176                                                     const std::string& id = "undefined" )
00177                         : ArgException( text,
00178                                         id,
00179                                                         std::string("Exception found when an Arg object ")+
00180                                                         std::string("is improperly defined by the ") +
00181                                                         std::string("developer." ))
00182                 { }
00183 
00184 };
00185 
00186 /**
00187  * (Added by JLBC for MRPT): An exception that indicates to CmdLine::parse that
00188  *   help,version,... have been invoked so it should return false for the main program to exit.
00189  */
00190 class ActionDoneException : public std::runtime_error
00191 {
00192 public:
00193         ActionDoneException(const std::string &text = std::string() ) :
00194           std::runtime_error(text.c_str())
00195         {
00196         }
00197 };
00198 
00199 
00200 } // namespace TCLAP
00201 
00202 #endif
00203 



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011