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