From babf6baac627ac0d0f38fcfb65681254e4f36e2f Mon Sep 17 00:00:00 2001 From: mes5k Date: Mon, 27 Sep 2004 21:30:57 +0000 Subject: [PATCH] added new Exception classes --- include/tclap/Arg.h | 8 ++--- include/tclap/ArgException.h | 57 +++++++++++++++++++++++++++++++++++- include/tclap/CmdLine.h | 12 ++++---- include/tclap/MultiArg.h | 22 +++++++------- include/tclap/SwitchArg.h | 2 +- include/tclap/ValueArg.h | 22 +++++++------- 6 files changed, 92 insertions(+), 31 deletions(-) diff --git a/include/tclap/Arg.h b/include/tclap/Arg.h index 60ca805..46c8015 100644 --- a/include/tclap/Arg.h +++ b/include/tclap/Arg.h @@ -364,14 +364,14 @@ inline Arg::Arg(const std::string& flag, _xorSet(false) { if ( _flag.length() > 1 ) - throw(ArgException("Argument flag can only be one character long", - toString() ) ); + throw(SpecificationException( + "Argument flag can only be one character long", toString() ) ); if ( _name != ignoreNameString() && ( _flag == Arg::flagStartString() || _flag == Arg::nameStartString() || _flag == " " ) ) - throw(ArgException("Argument flag cannot be either '" + + throw(SpecificationException("Argument flag cannot be either '" + Arg::flagStartString() + "' or '" + Arg::nameStartString() + "' or a space.", toString() ) ); @@ -379,7 +379,7 @@ inline Arg::Arg(const std::string& flag, if ( ( _name.find( Arg::flagStartString(), 0 ) != std::string::npos ) || ( _name.find( Arg::nameStartString(), 0 ) != std::string::npos ) || ( _name.find( " ", 0 ) != std::string::npos ) ) - throw(ArgException("Argument name cannot contain either '" + + throw(SpecificationException("Argument name cannot contain either '" + Arg::flagStartString() + "' or '" + Arg::nameStartString() + "' or space.", toString() ) ); diff --git a/include/tclap/ArgException.h b/include/tclap/ArgException.h index 0117f9b..6dce841 100644 --- a/include/tclap/ArgException.h +++ b/include/tclap/ArgException.h @@ -90,7 +90,62 @@ class ArgException : public std::exception }; -} +/** + * 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 ) { } +}; + +/** + * 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 ) { } +}; + +/** + * 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 ) { } + +}; + +} // namespace TCLAP #endif diff --git a/include/tclap/CmdLine.h b/include/tclap/CmdLine.h index c47ca48..6d23226 100644 --- a/include/tclap/CmdLine.h +++ b/include/tclap/CmdLine.h @@ -331,8 +331,9 @@ inline void CmdLine::add( Arg* a ) { for( ArgIterator iter = _argList.begin(); iter != _argList.end(); iter++ ) if ( *a == *(*iter) ) - throw( ArgException( "Argument with same flag/name already exists!", - a->longID() ) ); + throw( SpecificationException( + "Argument with same flag/name already exists!", + a->longID() ) ); a->addToList( _argList ); @@ -420,14 +421,15 @@ inline void CmdLine::parse(int argc, char** argv) matched = true; if ( !matched && !Arg::ignoreRest() ) - throw( ArgException("Couldn't find match for argument",args[i])); + throw(CmdLineParseException("Couldn't find match for argument", + args[i])); } if ( requiredCount < _numRequired ) - throw( ArgException("One or more required arguments missing!") ); + throw(CmdLineParseException("One or more required arguments missing!")); if ( requiredCount > _numRequired ) - throw( ArgException("Too many arguments!") ); + throw(CmdLineParseException("Too many arguments!")); } catch ( ArgException e ) { diff --git a/include/tclap/MultiArg.h b/include/tclap/MultiArg.h index 29784fa..1b1c374 100644 --- a/include/tclap/MultiArg.h +++ b/include/tclap/MultiArg.h @@ -423,8 +423,9 @@ bool MultiArg::processArg(int *i, std::vector& args) if ( argMatches( flag ) ) { if ( Arg::delimiter() != ' ' && value == "" ) - throw( ArgException( "Couldn't find delimiter for this argument!", - toString() ) ); + throw( ArgParseException( + "Couldn't find delimiter for this argument!", + toString() ) ); if ( value == "" ) { @@ -432,8 +433,8 @@ bool MultiArg::processArg(int *i, std::vector& args) if ( (unsigned int)*i < args.size() ) _extractValue( args[*i] ); else - throw( ArgException("Missing a value for this argument!", - toString() ) ); + throw( ArgParseException("Missing a value for this argument!", + toString() ) ); } else _extractValue( value ); @@ -455,8 +456,8 @@ void MultiArg::_checkAllowed( const std::string& val ) if ( _allowed.size() > 0 ) if ( find(_allowed.begin(),_allowed.end(),_values.back()) == _allowed.end() ) - throw( ArgException( "Couldn't find '" + val + - "' in allowed list.", toString() ) ); + throw( CmdLineParseException( "Couldn't find '" + val + + "' in allowed list.", toString() ) ); } /** @@ -508,12 +509,13 @@ void MultiArg::_extractValue( const std::string& val ) int err = ve.extractValue(val); if ( err == MULTI_ARG_HELPER::EFAIL ) - throw( ArgException("Couldn't read argument value " - "from string '" + val + "'", toString() ) ); + throw( ArgParseException("Couldn't read argument value " + "from string '" + val + "'", toString() ) ); if(err == MULTI_ARG_HELPER::EMANY) - throw( ArgException("More than one valid value " - "parsed from string '" + val + "'", toString() ) ); + throw( ArgParseException("More than one valid value " + "parsed from string '" + val + "'", + toString() ) ); _checkAllowed( val ); } diff --git a/include/tclap/SwitchArg.h b/include/tclap/SwitchArg.h index 9ebe902..190291b 100644 --- a/include/tclap/SwitchArg.h +++ b/include/tclap/SwitchArg.h @@ -181,7 +181,7 @@ inline bool SwitchArg::processArg(int *i, std::vector& args) ret = true; if ( _alreadySet ) - throw(ArgException("Argument already set!", toString())); + throw(CmdLineParseException("Argument already set!", toString())); _alreadySet = true; diff --git a/include/tclap/ValueArg.h b/include/tclap/ValueArg.h index 21cb283..64e589c 100644 --- a/include/tclap/ValueArg.h +++ b/include/tclap/ValueArg.h @@ -463,11 +463,12 @@ bool ValueArg::processArg(int *i, std::vector& args) if ( argMatches( flag ) ) { if ( _alreadySet ) - throw( ArgException("Argument already set!", toString()) ); + throw( CmdLineParseException("Argument already set!", toString()) ); if ( Arg::delimiter() != ' ' && value == "" ) - throw( ArgException( "Couldn't find delimiter for this argument!", - toString() ) ); + throw( ArgParseException( + "Couldn't find delimiter for this argument!", + toString() ) ); if ( value == "" ) { @@ -475,7 +476,7 @@ bool ValueArg::processArg(int *i, std::vector& args) if ( (unsigned int)*i < args.size() ) _extractValue( args[*i] ); else - throw( ArgException("Missing a value for this argument!", + throw( ArgParseException("Missing a value for this argument!", toString() ) ); } else @@ -497,8 +498,8 @@ void ValueArg::_checkAllowed( const std::string& val ) { if ( _allowed.size() > 0 ) if ( find(_allowed.begin(),_allowed.end(),_value) == _allowed.end() ) - throw( ArgException( "Couldn't find '" + val + - "' in allowed list.", toString() ) ); + throw( CmdLineParseException( "Couldn't find '" + val + + "' in allowed list.", toString() ) ); } /** @@ -527,12 +528,13 @@ void ValueArg::_extractValue( const std::string& val ) int err = ve.extractValue(val); if ( err == VALUE_ARG_HELPER::EFAIL ) - throw( ArgException("Couldn't read argument value from string '" + - val + "'", toString() ) ); + throw( ArgParseException("Couldn't read argument value from string '" + + val + "'", toString() ) ); if ( err == VALUE_ARG_HELPER::EMANY ) - throw( ArgException("More than one valid value parsed from string '" + - val + "'", toString() ) ); + throw( ArgParseException( + "More than one valid value parsed from string '" + + val + "'", toString() ) ); _checkAllowed( val ); }