From bbbadfddc94ada3cf2f9c7f06ddd1bfaf6c65f59 Mon Sep 17 00:00:00 2001 From: mes5k Date: Mon, 22 Dec 2003 01:48:53 +0000 Subject: [PATCH] delimiter changes --- include/tclap/Arg.h | 27 ++++++++++--- include/tclap/ArgException.h | 8 +++- include/tclap/CmdLine.h | 5 ++- include/tclap/MultiArg.h | 33 +++++++++++----- include/tclap/UnlabeledValueArg.h | 4 +- include/tclap/ValueArg.h | 64 +++++++++++++++++-------------- 6 files changed, 91 insertions(+), 50 deletions(-) diff --git a/include/tclap/Arg.h b/include/tclap/Arg.h index 4e0e238..52365d1 100644 --- a/include/tclap/Arg.h +++ b/include/tclap/Arg.h @@ -43,10 +43,14 @@ namespace TCLAP { class Arg { private: + static bool _ignoreRest; + protected: + static char _delimiter; + /** * The single char flag used to identify the argument. * This value (preceded by a dash {-}), can be used to identify @@ -98,21 +102,26 @@ class Arg */ Visitor* _visitor; - /** - * Performs the special handling described by the Vistitor. - */ - void _checkWithVisitor() const; - /** * Whether this argument can be ignored, if desired. */ bool _ignoreable; + /** + * Performs the special handling described by the Vistitor. + */ + void _checkWithVisitor() const; + public: static void beginIgnoring() { Arg::_ignoreRest = true; } static bool ignoreRest() { return Arg::_ignoreRest; } + /** + * Sets the delimiter for all arguments. + */ + static void setDelimiter( char c ) { Arg::_delimiter = c; } + /** * Primary constructor. */ @@ -143,7 +152,7 @@ class Arg /** * Destructor. */ - ~Arg(); + virtual ~Arg(); /** * Processes the argument. @@ -224,6 +233,12 @@ class Arg */ virtual string longID( const string& valueId = "val" ) const; + /** + * Trims a value off of the flag. + */ + virtual void trimFlag( string& flag, string& value ) const; + + }; /** diff --git a/include/tclap/ArgException.h b/include/tclap/ArgException.h index a113b46..87fb496 100644 --- a/include/tclap/ArgException.h +++ b/include/tclap/ArgException.h @@ -54,7 +54,13 @@ class ArgException }; string error() { return ( _errorText ); }; - string argId() { return ( _argId ); }; + string argId() + { + if ( _argId == "undefined" ) + return " "; + else + return ( "Argument: " + _argId ); + }; private: diff --git a/include/tclap/CmdLine.h b/include/tclap/CmdLine.h index b7fc930..ac542c5 100644 --- a/include/tclap/CmdLine.h +++ b/include/tclap/CmdLine.h @@ -51,10 +51,11 @@ class CmdLine string _version; int _maxLength; int _numRequired; + char _delimiter; public: - CmdLine(char* progName, - const string& message, + CmdLine(const string& message, + const char delimiter = ' ', const string& version = "none" ); void add( Arg& a ); diff --git a/include/tclap/MultiArg.h b/include/tclap/MultiArg.h index 5569304..fc39aa8 100644 --- a/include/tclap/MultiArg.h +++ b/include/tclap/MultiArg.h @@ -41,7 +41,7 @@ class MultiArg : public Arg string _typeDesc; - void _extractValue(int i, vector& args); + void _extractValue( const string& val ); public: @@ -87,27 +87,40 @@ bool MultiArg::processArg(int *i, vector& args) if ( _ignoreable && Arg::ignoreRest() ) return false; - if ( argMatches( args[*i] ) ) + string flag = args[*i]; + string value = ""; + + trimFlag( flag, value ); + + if ( argMatches( flag ) ) { - (*i)++; - if ( *i < args.size() ) + if ( Arg::_delimiter != ' ' && value == "" ) + throw( ArgException( "Couldn't find delimiter for this argument!", + toString() ) ); + + if ( value == "" ) { - _extractValue( *i, args ); - return true; + (*i)++; + if ( (unsigned int)*i < args.size() ) + _extractValue( args[*i] ); + else + throw( ArgException("Missing a value for this argument!", + toString() ) ); } else - throw( ArgException("Missing a value for this argument!", - toString() ) ); + _extractValue( value ); + + return true; } else return false; } template -void MultiArg::_extractValue(int i, vector& args) +void MultiArg::_extractValue( const string& val ) { T temp; - istringstream is(args[i]); + istringstream is(val); is >> temp; if ( is.fail() ) throw( ArgException("Couldn't read argument value!", toString())); diff --git a/include/tclap/UnlabeledValueArg.h b/include/tclap/UnlabeledValueArg.h index 1713653..0fd6b4a 100644 --- a/include/tclap/UnlabeledValueArg.h +++ b/include/tclap/UnlabeledValueArg.h @@ -83,7 +83,7 @@ class UnlabeledValueArg : public ValueArg * \param i - Pointer the the current argument in the list. * \param args - Mutable list of strings. */ - virtual bool processArg(int* i, vector& args ); + virtual bool processArg(int* i, vector& args); /** * Overrides shortID for specific behavior. @@ -128,7 +128,7 @@ bool UnlabeledValueArg::processArg(int *i, vector& args) if ( _alreadySet ) return false; - _extractValue( *i, args ); + _extractValue( args[*i] ); _alreadySet = true; return true; } diff --git a/include/tclap/ValueArg.h b/include/tclap/ValueArg.h index 7dc2d5b..48425b1 100644 --- a/include/tclap/ValueArg.h +++ b/include/tclap/ValueArg.h @@ -63,13 +63,12 @@ class ValueArg : public Arg 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 * is thrown. - * \param i - The index of the argument to extract. - * \param args - Mutable list of strings. + * \param val - string value to be parsed. */ - void _extractValue( int i, vector& args ); + void _extractValue( const string& val ); public: @@ -118,7 +117,7 @@ class ValueArg : public Arg * \param args - Mutable list of strings. Passed * in from main(). */ - virtual bool processArg(int* i, vector& args ); + virtual bool processArg(int* i, vector& args); /** * Returns the value of the argument. @@ -171,44 +170,52 @@ T& ValueArg::getValue() { return _value; }; * Implementation of processArg(). */ template -bool ValueArg::processArg(int *i, vector& args) +bool ValueArg::processArg(int *i, vector& args) { - if ( _ignoreable && Arg::ignoreRest() ) - return false; + if ( _ignoreable && Arg::ignoreRest() ) + 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)++; - if (*i < args.size() ) - { - _extractValue( *i, args); - - _alreadySet = true; - - _checkWithVisitor(); - - return true; - } + if ( (unsigned int)*i < args.size() ) + _extractValue( args[*i] ); else throw( ArgException("Missing a value for this argument!", - toString() ) ); - } + toString() ) ); + } else - return false; + _extractValue( value ); + + _alreadySet = true; + _checkWithVisitor(); + return true; + } + else + return false; } /** * Implementation of _extractValue. */ template -void ValueArg::_extractValue(int i, vector& args) +void ValueArg::_extractValue( const string& val ) { - istringstream is(args[i]); + istringstream is(val); is >> _value; if ( is.fail() ) throw( ArgException("Couldn't read argument value!", toString() ) ); @@ -232,6 +239,5 @@ string ValueArg::longID(const string& val) const return Arg::longID( _typeDesc ); } - } #endif