added new Exception classes

This commit is contained in:
mes5k 2004-09-27 21:30:57 +00:00
parent 66b04fdd11
commit babf6baac6
6 changed files with 92 additions and 31 deletions

View File

@ -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() ) );

View File

@ -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

View File

@ -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 )
{

View File

@ -423,8 +423,9 @@ bool MultiArg<T>::processArg(int *i, std::vector<std::string>& 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<T>::processArg(int *i, std::vector<std::string>& 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<T>::_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<T>::_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 );
}

View File

@ -181,7 +181,7 @@ inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
ret = true;
if ( _alreadySet )
throw(ArgException("Argument already set!", toString()));
throw(CmdLineParseException("Argument already set!", toString()));
_alreadySet = true;

View File

@ -463,11 +463,12 @@ bool ValueArg<T>::processArg(int *i, std::vector<std::string>& 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<T>::processArg(int *i, std::vector<std::string>& 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<T>::_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<T>::_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 );
}