diff --git a/include/tclap/Arg.h b/include/tclap/Arg.h index 7312423..3d44c85 100644 --- a/include/tclap/Arg.h +++ b/include/tclap/Arg.h @@ -351,9 +351,23 @@ class Arg */ void setRequireLabel( const std::string& s ); + /** + * Used for MultiArgs and XorHandler to determine whether args + * can still be set. + */ virtual bool allowMore(); + + /** + * Use by output classes to determine whether an Arg accepts + * multiple values. + */ virtual bool acceptsMultipleValues(); + /** + * Clears the Arg object and allows it to be reused by new + * command lines. + */ + virtual void reset(); }; /** @@ -642,6 +656,12 @@ inline bool Arg::acceptsMultipleValues() return _acceptsMultipleValues; } +inline void Arg::reset() +{ + _xorSet = false; + _alreadySet = false; +} + ////////////////////////////////////////////////////////////////////// //END Arg.cpp ////////////////////////////////////////////////////////////////////// diff --git a/include/tclap/CmdLine.h b/include/tclap/CmdLine.h index afe8fef..90d0e65 100644 --- a/include/tclap/CmdLine.h +++ b/include/tclap/CmdLine.h @@ -301,6 +301,12 @@ private: * @retval false Parsing exceptions are propagated to the caller. */ bool getExceptionHandling() const; + + /** + * Allows the CmdLine object to be reused. + */ + void reset(); + }; @@ -595,6 +601,16 @@ inline bool CmdLine::getExceptionHandling() const return _handleExceptions; } +inline void CmdLine::reset() +{ + for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ ) + { + (*it)->reset(); + } + + _progName.clear(); +} + /////////////////////////////////////////////////////////////////////////////// //End CmdLine.cpp /////////////////////////////////////////////////////////////////////////////// diff --git a/include/tclap/CmdLineInterface.h b/include/tclap/CmdLineInterface.h index e3f73fd..1b25e9b 100644 --- a/include/tclap/CmdLineInterface.h +++ b/include/tclap/CmdLineInterface.h @@ -136,6 +136,12 @@ class CmdLineInterface * automatically. */ virtual bool hasHelpAndVersion()=0; + + /** + * Resets the instance as if it had just been constructed so that the + * instance can be reused. + */ + virtual void reset()=0; }; } //namespace diff --git a/include/tclap/MultiArg.h b/include/tclap/MultiArg.h index 53115c2..460e5cb 100644 --- a/include/tclap/MultiArg.h +++ b/include/tclap/MultiArg.h @@ -68,6 +68,9 @@ protected: */ void _extractValue( const std::string& val ); + /** + * Used by XorHandler to decide whether to keep parsing for this arg. + */ bool _allowMore; public: @@ -215,6 +218,8 @@ public: virtual bool isRequired() const; virtual bool allowMore(); + + virtual void reset(); }; @@ -405,6 +410,13 @@ bool MultiArg::allowMore() return am; } +template +void MultiArg::reset() +{ + Arg::reset(); + _values.clear(); +} + } // namespace TCLAP #endif diff --git a/include/tclap/MultiSwitchArg.h b/include/tclap/MultiSwitchArg.h index 5435851..8820b64 100644 --- a/include/tclap/MultiSwitchArg.h +++ b/include/tclap/MultiSwitchArg.h @@ -45,6 +45,11 @@ class MultiSwitchArg : public SwitchArg */ int _value; + /** + * Used to support the reset() method so that ValueArg can be + * reset to their constructed value. + */ + int _default; public: @@ -114,6 +119,9 @@ class MultiSwitchArg : public SwitchArg * Returns the longID for this Arg. */ std::string longID(const std::string& val) const; + + void reset(); + }; ////////////////////////////////////////////////////////////////////// @@ -125,7 +133,8 @@ inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, int init, Visitor* v ) : SwitchArg(flag, name, desc, false, v), -_value( init ) +_value( init ), +_default( init ) { } inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, @@ -135,7 +144,8 @@ inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, int init, Visitor* v ) : SwitchArg(flag, name, desc, false, v), -_value( init ) +_value( init ), +_default( init ) { parser.add( this ); } @@ -191,6 +201,12 @@ MultiSwitchArg::longID(const std::string& val) const return Arg::longID(val) + " (accepted multiple times)"; } +inline void +MultiSwitchArg::reset() +{ + MultiSwitchArg::_value = MultiSwitchArg::_default; +} + ////////////////////////////////////////////////////////////////////// //END MultiSwitchArg.cpp ////////////////////////////////////////////////////////////////////// diff --git a/include/tclap/SwitchArg.h b/include/tclap/SwitchArg.h index e2c541c..dc4952e 100644 --- a/include/tclap/SwitchArg.h +++ b/include/tclap/SwitchArg.h @@ -45,6 +45,12 @@ class SwitchArg : public Arg */ bool _value; + /** + * Used to support the reset() method so that ValueArg can be + * reset to their constructed value. + */ + bool _default; + public: /** @@ -107,6 +113,8 @@ class SwitchArg : public Arg * Returns bool, whether or not the switch has been set. */ bool getValue(); + + virtual void reset(); }; @@ -116,20 +124,22 @@ class SwitchArg : public Arg inline SwitchArg::SwitchArg(const std::string& flag, const std::string& name, const std::string& desc, - bool _default, + bool default_val, Visitor* v ) : Arg(flag, name, desc, false, false, v), - _value( _default ) + _value( default_val ), + _default( default_val ) { } inline SwitchArg::SwitchArg(const std::string& flag, const std::string& name, const std::string& desc, CmdLineInterface& parser, - bool _default, + bool default_val, Visitor* v ) : Arg(flag, name, desc, false, false, v), - _value( _default ) + _value( default_val ), + _default(default_val) { parser.add( this ); } @@ -204,6 +214,11 @@ inline bool SwitchArg::processArg(int *i, std::vector& args) return false; } +inline void SwitchArg::reset() +{ + Arg::reset(); + _value = _default; +} ////////////////////////////////////////////////////////////////////// //End SwitchArg.cpp ////////////////////////////////////////////////////////////////////// diff --git a/include/tclap/ValueArg.h b/include/tclap/ValueArg.h index 5a383d5..28117f6 100644 --- a/include/tclap/ValueArg.h +++ b/include/tclap/ValueArg.h @@ -51,6 +51,12 @@ class ValueArg : public Arg */ T _value; + /** + * Used to support the reset() method so that ValueArg can be + * reset to their constructed value. + */ + T _default; + /** * A human readable description of the type to be parsed. * This is a hack, plain and simple. Ideally we would use RTTI to @@ -227,6 +233,8 @@ class ValueArg : public Arg * \param val - value to be used. */ virtual std::string longID(const std::string& val = "val") const; + + virtual void reset() ; }; @@ -244,6 +252,7 @@ ValueArg::ValueArg(const std::string& flag, Visitor* v) : Arg(flag, name, desc, req, true, v), _value( val ), + _default( val ), _typeDesc( typeDesc ), _constraint( NULL ) { } @@ -259,6 +268,7 @@ ValueArg::ValueArg(const std::string& flag, Visitor* v) : Arg(flag, name, desc, req, true, v), _value( val ), + _default( val ), _typeDesc( typeDesc ), _constraint( NULL ) { @@ -275,6 +285,7 @@ ValueArg::ValueArg(const std::string& flag, Visitor* v) : Arg(flag, name, desc, req, true, v), _value( val ), + _default( val ), _typeDesc( constraint->shortID() ), _constraint( constraint ) { } @@ -290,6 +301,7 @@ ValueArg::ValueArg(const std::string& flag, Visitor* v) : Arg(flag, name, desc, req, true, v), _value( val ), + _default( val ), _typeDesc( constraint->shortID() ), _constraint( constraint ) { @@ -387,6 +399,13 @@ void ValueArg::_extractValue( const std::string& val ) toString() ) ); } +template +void ValueArg::reset() +{ + Arg::reset(); + _value = _default; +} + } // namespace TCLAP #endif