delimiter changes

This commit is contained in:
mes5k 2003-12-22 01:48:53 +00:00
parent 3bf473496a
commit bbbadfddc9
6 changed files with 91 additions and 50 deletions

View File

@ -43,10 +43,14 @@ namespace TCLAP {
class Arg class Arg
{ {
private: private:
static bool _ignoreRest; static bool _ignoreRest;
protected: protected:
static char _delimiter;
/** /**
* The single char flag used to identify the argument. * The single char flag used to identify the argument.
* This value (preceded by a dash {-}), can be used to identify * This value (preceded by a dash {-}), can be used to identify
@ -98,21 +102,26 @@ class Arg
*/ */
Visitor* _visitor; Visitor* _visitor;
/**
* Performs the special handling described by the Vistitor.
*/
void _checkWithVisitor() const;
/** /**
* Whether this argument can be ignored, if desired. * Whether this argument can be ignored, if desired.
*/ */
bool _ignoreable; bool _ignoreable;
/**
* Performs the special handling described by the Vistitor.
*/
void _checkWithVisitor() const;
public: public:
static void beginIgnoring() { Arg::_ignoreRest = true; } static void beginIgnoring() { Arg::_ignoreRest = true; }
static bool ignoreRest() { return Arg::_ignoreRest; } static bool ignoreRest() { return Arg::_ignoreRest; }
/**
* Sets the delimiter for all arguments.
*/
static void setDelimiter( char c ) { Arg::_delimiter = c; }
/** /**
* Primary constructor. * Primary constructor.
*/ */
@ -143,7 +152,7 @@ class Arg
/** /**
* Destructor. * Destructor.
*/ */
~Arg(); virtual ~Arg();
/** /**
* Processes the argument. * Processes the argument.
@ -224,6 +233,12 @@ class Arg
*/ */
virtual string longID( const string& valueId = "val" ) const; virtual string longID( const string& valueId = "val" ) const;
/**
* Trims a value off of the flag.
*/
virtual void trimFlag( string& flag, string& value ) const;
}; };
/** /**

View File

@ -54,7 +54,13 @@ class ArgException
}; };
string error() { return ( _errorText ); }; string error() { return ( _errorText ); };
string argId() { return ( _argId ); }; string argId()
{
if ( _argId == "undefined" )
return " ";
else
return ( "Argument: " + _argId );
};
private: private:

View File

@ -51,10 +51,11 @@ class CmdLine
string _version; string _version;
int _maxLength; int _maxLength;
int _numRequired; int _numRequired;
char _delimiter;
public: public:
CmdLine(char* progName, CmdLine(const string& message,
const string& message, const char delimiter = ' ',
const string& version = "none" ); const string& version = "none" );
void add( Arg& a ); void add( Arg& a );

View File

@ -41,7 +41,7 @@ class MultiArg : public Arg
string _typeDesc; string _typeDesc;
void _extractValue(int i, vector<string>& args); void _extractValue( const string& val );
public: public:
@ -87,27 +87,40 @@ bool MultiArg<T>::processArg(int *i, vector<string>& args)
if ( _ignoreable && Arg::ignoreRest() ) if ( _ignoreable && Arg::ignoreRest() )
return false; return false;
if ( argMatches( args[*i] ) ) string flag = args[*i];
string value = "";
trimFlag( flag, value );
if ( argMatches( flag ) )
{ {
(*i)++; if ( Arg::_delimiter != ' ' && value == "" )
if ( *i < args.size() ) throw( ArgException( "Couldn't find delimiter for this argument!",
toString() ) );
if ( value == "" )
{ {
_extractValue( *i, args ); (*i)++;
return true; if ( (unsigned int)*i < args.size() )
_extractValue( args[*i] );
else
throw( ArgException("Missing a value for this argument!",
toString() ) );
} }
else else
throw( ArgException("Missing a value for this argument!", _extractValue( value );
toString() ) );
return true;
} }
else else
return false; return false;
} }
template<class T> template<class T>
void MultiArg<T>::_extractValue(int i, vector<string>& args) void MultiArg<T>::_extractValue( const string& val )
{ {
T temp; T temp;
istringstream is(args[i]); istringstream is(val);
is >> temp; is >> temp;
if ( is.fail() ) if ( is.fail() )
throw( ArgException("Couldn't read argument value!", toString())); throw( ArgException("Couldn't read argument value!", toString()));

View File

@ -83,7 +83,7 @@ class UnlabeledValueArg : public ValueArg<T>
* \param i - Pointer the the current argument in the list. * \param i - Pointer the the current argument in the list.
* \param args - Mutable list of strings. * \param args - Mutable list of strings.
*/ */
virtual bool processArg(int* i, vector<string>& args ); virtual bool processArg(int* i, vector<string>& args);
/** /**
* Overrides shortID for specific behavior. * Overrides shortID for specific behavior.
@ -128,7 +128,7 @@ bool UnlabeledValueArg<T>::processArg(int *i, vector<string>& args)
if ( _alreadySet ) if ( _alreadySet )
return false; return false;
_extractValue( *i, args ); _extractValue( args[*i] );
_alreadySet = true; _alreadySet = true;
return true; return true;
} }

View File

@ -63,13 +63,12 @@ class ValueArg : public Arg
string _typeDesc; string _typeDesc;
/** /**
* Extracts the string at position i from the args list. * Extracts the value from the string.
* Attempts to parse string as type T, if this fails an exception * Attempts to parse string as type T, if this fails an exception
* is thrown. * is thrown.
* \param i - The index of the argument to extract. * \param val - string value to be parsed.
* \param args - Mutable list of strings.
*/ */
void _extractValue( int i, vector<string>& args ); void _extractValue( const string& val );
public: public:
@ -118,7 +117,7 @@ class ValueArg : public Arg
* \param args - Mutable list of strings. Passed * \param args - Mutable list of strings. Passed
* in from main(). * in from main().
*/ */
virtual bool processArg(int* i, vector<string>& args ); virtual bool processArg(int* i, vector<string>& args);
/** /**
* Returns the value of the argument. * Returns the value of the argument.
@ -173,42 +172,50 @@ T& ValueArg<T>::getValue() { return _value; };
template<class T> template<class T>
bool ValueArg<T>::processArg(int *i, vector<string>& args) bool ValueArg<T>::processArg(int *i, vector<string>& args)
{ {
if ( _ignoreable && Arg::ignoreRest() ) if ( _ignoreable && Arg::ignoreRest() )
return false; return false;
string flag = args[*i]; string flag = args[*i];
if ( argMatches( flag ) ) string value = "";
trimFlag( flag, value );
if ( argMatches( flag ) )
{
if ( _alreadySet )
throw( ArgException("Argument already set!", toString()) );
if ( Arg::_delimiter != ' ' && value == "" )
throw( ArgException( "Couldn't find delimiter for this argument!",
toString() ) );
if ( value == "" )
{ {
if ( _alreadySet )
throw( ArgException("Argument already set!", toString()) );
(*i)++; (*i)++;
if (*i < args.size() ) if ( (unsigned int)*i < args.size() )
{ _extractValue( args[*i] );
_extractValue( *i, args);
_alreadySet = true;
_checkWithVisitor();
return true;
}
else else
throw( ArgException("Missing a value for this argument!", throw( ArgException("Missing a value for this argument!",
toString() ) ); toString() ) );
} }
else else
return false; _extractValue( value );
_alreadySet = true;
_checkWithVisitor();
return true;
}
else
return false;
} }
/** /**
* Implementation of _extractValue. * Implementation of _extractValue.
*/ */
template<class T> template<class T>
void ValueArg<T>::_extractValue(int i, vector<string>& args) void ValueArg<T>::_extractValue( const string& val )
{ {
istringstream is(args[i]); istringstream is(val);
is >> _value; is >> _value;
if ( is.fail() ) if ( is.fail() )
throw( ArgException("Couldn't read argument value!", toString() ) ); throw( ArgException("Couldn't read argument value!", toString() ) );
@ -232,6 +239,5 @@ string ValueArg<T>::longID(const string& val) const
return Arg::longID( _typeDesc ); return Arg::longID( _typeDesc );
} }
} }
#endif #endif