mirror of
https://github.com/cuberite/TCLAP.git
synced 2025-09-10 04:41:57 -04:00
added new Exception classes
This commit is contained in:
parent
66b04fdd11
commit
babf6baac6
@ -364,14 +364,14 @@ inline Arg::Arg(const std::string& flag,
|
|||||||
_xorSet(false)
|
_xorSet(false)
|
||||||
{
|
{
|
||||||
if ( _flag.length() > 1 )
|
if ( _flag.length() > 1 )
|
||||||
throw(ArgException("Argument flag can only be one character long",
|
throw(SpecificationException(
|
||||||
toString() ) );
|
"Argument flag can only be one character long", toString() ) );
|
||||||
|
|
||||||
if ( _name != ignoreNameString() &&
|
if ( _name != ignoreNameString() &&
|
||||||
( _flag == Arg::flagStartString() ||
|
( _flag == Arg::flagStartString() ||
|
||||||
_flag == Arg::nameStartString() ||
|
_flag == Arg::nameStartString() ||
|
||||||
_flag == " " ) )
|
_flag == " " ) )
|
||||||
throw(ArgException("Argument flag cannot be either '" +
|
throw(SpecificationException("Argument flag cannot be either '" +
|
||||||
Arg::flagStartString() + "' or '" +
|
Arg::flagStartString() + "' or '" +
|
||||||
Arg::nameStartString() + "' or a space.",
|
Arg::nameStartString() + "' or a space.",
|
||||||
toString() ) );
|
toString() ) );
|
||||||
@ -379,7 +379,7 @@ inline Arg::Arg(const std::string& flag,
|
|||||||
if ( ( _name.find( Arg::flagStartString(), 0 ) != std::string::npos ) ||
|
if ( ( _name.find( Arg::flagStartString(), 0 ) != std::string::npos ) ||
|
||||||
( _name.find( Arg::nameStartString(), 0 ) != std::string::npos ) ||
|
( _name.find( Arg::nameStartString(), 0 ) != std::string::npos ) ||
|
||||||
( _name.find( " ", 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::flagStartString() + "' or '" +
|
||||||
Arg::nameStartString() + "' or space.",
|
Arg::nameStartString() + "' or space.",
|
||||||
toString() ) );
|
toString() ) );
|
||||||
|
@ -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
|
#endif
|
||||||
|
|
||||||
|
@ -331,8 +331,9 @@ inline void CmdLine::add( Arg* a )
|
|||||||
{
|
{
|
||||||
for( ArgIterator iter = _argList.begin(); iter != _argList.end(); iter++ )
|
for( ArgIterator iter = _argList.begin(); iter != _argList.end(); iter++ )
|
||||||
if ( *a == *(*iter) )
|
if ( *a == *(*iter) )
|
||||||
throw( ArgException( "Argument with same flag/name already exists!",
|
throw( SpecificationException(
|
||||||
a->longID() ) );
|
"Argument with same flag/name already exists!",
|
||||||
|
a->longID() ) );
|
||||||
|
|
||||||
a->addToList( _argList );
|
a->addToList( _argList );
|
||||||
|
|
||||||
@ -420,14 +421,15 @@ inline void CmdLine::parse(int argc, char** argv)
|
|||||||
matched = true;
|
matched = true;
|
||||||
|
|
||||||
if ( !matched && !Arg::ignoreRest() )
|
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 )
|
if ( requiredCount < _numRequired )
|
||||||
throw( ArgException("One or more required arguments missing!") );
|
throw(CmdLineParseException("One or more required arguments missing!"));
|
||||||
|
|
||||||
if ( requiredCount > _numRequired )
|
if ( requiredCount > _numRequired )
|
||||||
throw( ArgException("Too many arguments!") );
|
throw(CmdLineParseException("Too many arguments!"));
|
||||||
|
|
||||||
} catch ( ArgException e )
|
} catch ( ArgException e )
|
||||||
{
|
{
|
||||||
|
@ -423,8 +423,9 @@ bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
|
|||||||
if ( argMatches( flag ) )
|
if ( argMatches( flag ) )
|
||||||
{
|
{
|
||||||
if ( Arg::delimiter() != ' ' && value == "" )
|
if ( Arg::delimiter() != ' ' && value == "" )
|
||||||
throw( ArgException( "Couldn't find delimiter for this argument!",
|
throw( ArgParseException(
|
||||||
toString() ) );
|
"Couldn't find delimiter for this argument!",
|
||||||
|
toString() ) );
|
||||||
|
|
||||||
if ( value == "" )
|
if ( value == "" )
|
||||||
{
|
{
|
||||||
@ -432,8 +433,8 @@ bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
|
|||||||
if ( (unsigned int)*i < args.size() )
|
if ( (unsigned int)*i < args.size() )
|
||||||
_extractValue( args[*i] );
|
_extractValue( args[*i] );
|
||||||
else
|
else
|
||||||
throw( ArgException("Missing a value for this argument!",
|
throw( ArgParseException("Missing a value for this argument!",
|
||||||
toString() ) );
|
toString() ) );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_extractValue( value );
|
_extractValue( value );
|
||||||
@ -455,8 +456,8 @@ void MultiArg<T>::_checkAllowed( const std::string& val )
|
|||||||
if ( _allowed.size() > 0 )
|
if ( _allowed.size() > 0 )
|
||||||
if ( find(_allowed.begin(),_allowed.end(),_values.back())
|
if ( find(_allowed.begin(),_allowed.end(),_values.back())
|
||||||
== _allowed.end() )
|
== _allowed.end() )
|
||||||
throw( ArgException( "Couldn't find '" + val +
|
throw( CmdLineParseException( "Couldn't find '" + val +
|
||||||
"' in allowed list.", toString() ) );
|
"' in allowed list.", toString() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -508,12 +509,13 @@ void MultiArg<T>::_extractValue( const std::string& val )
|
|||||||
int err = ve.extractValue(val);
|
int err = ve.extractValue(val);
|
||||||
|
|
||||||
if ( err == MULTI_ARG_HELPER::EFAIL )
|
if ( err == MULTI_ARG_HELPER::EFAIL )
|
||||||
throw( ArgException("Couldn't read argument value "
|
throw( ArgParseException("Couldn't read argument value "
|
||||||
"from string '" + val + "'", toString() ) );
|
"from string '" + val + "'", toString() ) );
|
||||||
|
|
||||||
if(err == MULTI_ARG_HELPER::EMANY)
|
if(err == MULTI_ARG_HELPER::EMANY)
|
||||||
throw( ArgException("More than one valid value "
|
throw( ArgParseException("More than one valid value "
|
||||||
"parsed from string '" + val + "'", toString() ) );
|
"parsed from string '" + val + "'",
|
||||||
|
toString() ) );
|
||||||
_checkAllowed( val );
|
_checkAllowed( val );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -181,7 +181,7 @@ inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
|
|||||||
ret = true;
|
ret = true;
|
||||||
|
|
||||||
if ( _alreadySet )
|
if ( _alreadySet )
|
||||||
throw(ArgException("Argument already set!", toString()));
|
throw(CmdLineParseException("Argument already set!", toString()));
|
||||||
|
|
||||||
_alreadySet = true;
|
_alreadySet = true;
|
||||||
|
|
||||||
|
@ -463,11 +463,12 @@ bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
|
|||||||
if ( argMatches( flag ) )
|
if ( argMatches( flag ) )
|
||||||
{
|
{
|
||||||
if ( _alreadySet )
|
if ( _alreadySet )
|
||||||
throw( ArgException("Argument already set!", toString()) );
|
throw( CmdLineParseException("Argument already set!", toString()) );
|
||||||
|
|
||||||
if ( Arg::delimiter() != ' ' && value == "" )
|
if ( Arg::delimiter() != ' ' && value == "" )
|
||||||
throw( ArgException( "Couldn't find delimiter for this argument!",
|
throw( ArgParseException(
|
||||||
toString() ) );
|
"Couldn't find delimiter for this argument!",
|
||||||
|
toString() ) );
|
||||||
|
|
||||||
if ( value == "" )
|
if ( value == "" )
|
||||||
{
|
{
|
||||||
@ -475,7 +476,7 @@ bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
|
|||||||
if ( (unsigned int)*i < args.size() )
|
if ( (unsigned int)*i < args.size() )
|
||||||
_extractValue( args[*i] );
|
_extractValue( args[*i] );
|
||||||
else
|
else
|
||||||
throw( ArgException("Missing a value for this argument!",
|
throw( ArgParseException("Missing a value for this argument!",
|
||||||
toString() ) );
|
toString() ) );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -497,8 +498,8 @@ void ValueArg<T>::_checkAllowed( const std::string& val )
|
|||||||
{
|
{
|
||||||
if ( _allowed.size() > 0 )
|
if ( _allowed.size() > 0 )
|
||||||
if ( find(_allowed.begin(),_allowed.end(),_value) == _allowed.end() )
|
if ( find(_allowed.begin(),_allowed.end(),_value) == _allowed.end() )
|
||||||
throw( ArgException( "Couldn't find '" + val +
|
throw( CmdLineParseException( "Couldn't find '" + val +
|
||||||
"' in allowed list.", toString() ) );
|
"' in allowed list.", toString() ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -527,12 +528,13 @@ void ValueArg<T>::_extractValue( const std::string& val )
|
|||||||
int err = ve.extractValue(val);
|
int err = ve.extractValue(val);
|
||||||
|
|
||||||
if ( err == VALUE_ARG_HELPER::EFAIL )
|
if ( err == VALUE_ARG_HELPER::EFAIL )
|
||||||
throw( ArgException("Couldn't read argument value from string '" +
|
throw( ArgParseException("Couldn't read argument value from string '" +
|
||||||
val + "'", toString() ) );
|
val + "'", toString() ) );
|
||||||
|
|
||||||
if ( err == VALUE_ARG_HELPER::EMANY )
|
if ( err == VALUE_ARG_HELPER::EMANY )
|
||||||
throw( ArgException("More than one valid value parsed from string '" +
|
throw( ArgParseException(
|
||||||
val + "'", toString() ) );
|
"More than one valid value parsed from string '" +
|
||||||
|
val + "'", toString() ) );
|
||||||
|
|
||||||
_checkAllowed( val );
|
_checkAllowed( val );
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user