added support for resetting a command line

This commit is contained in:
mes5k 2009-01-10 00:05:57 +00:00
parent 352a838666
commit 65376234b2
7 changed files with 110 additions and 6 deletions

View File

@ -351,9 +351,23 @@ class Arg
*/ */
void setRequireLabel( const std::string& s ); void setRequireLabel( const std::string& s );
/**
* Used for MultiArgs and XorHandler to determine whether args
* can still be set.
*/
virtual bool allowMore(); virtual bool allowMore();
/**
* Use by output classes to determine whether an Arg accepts
* multiple values.
*/
virtual bool acceptsMultipleValues(); 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; return _acceptsMultipleValues;
} }
inline void Arg::reset()
{
_xorSet = false;
_alreadySet = false;
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
//END Arg.cpp //END Arg.cpp
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////

View File

@ -301,6 +301,12 @@ private:
* @retval false Parsing exceptions are propagated to the caller. * @retval false Parsing exceptions are propagated to the caller.
*/ */
bool getExceptionHandling() const; bool getExceptionHandling() const;
/**
* Allows the CmdLine object to be reused.
*/
void reset();
}; };
@ -595,6 +601,16 @@ inline bool CmdLine::getExceptionHandling() const
return _handleExceptions; return _handleExceptions;
} }
inline void CmdLine::reset()
{
for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
{
(*it)->reset();
}
_progName.clear();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
//End CmdLine.cpp //End CmdLine.cpp
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -136,6 +136,12 @@ class CmdLineInterface
* automatically. * automatically.
*/ */
virtual bool hasHelpAndVersion()=0; 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 } //namespace

View File

@ -68,6 +68,9 @@ protected:
*/ */
void _extractValue( const std::string& val ); void _extractValue( const std::string& val );
/**
* Used by XorHandler to decide whether to keep parsing for this arg.
*/
bool _allowMore; bool _allowMore;
public: public:
@ -215,6 +218,8 @@ public:
virtual bool isRequired() const; virtual bool isRequired() const;
virtual bool allowMore(); virtual bool allowMore();
virtual void reset();
}; };
@ -405,6 +410,13 @@ bool MultiArg<T>::allowMore()
return am; return am;
} }
template<class T>
void MultiArg<T>::reset()
{
Arg::reset();
_values.clear();
}
} // namespace TCLAP } // namespace TCLAP
#endif #endif

View File

@ -45,6 +45,11 @@ class MultiSwitchArg : public SwitchArg
*/ */
int _value; int _value;
/**
* Used to support the reset() method so that ValueArg can be
* reset to their constructed value.
*/
int _default;
public: public:
@ -114,6 +119,9 @@ class MultiSwitchArg : public SwitchArg
* Returns the longID for this Arg. * Returns the longID for this Arg.
*/ */
std::string longID(const std::string& val) const; std::string longID(const std::string& val) const;
void reset();
}; };
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -125,7 +133,8 @@ inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
int init, int init,
Visitor* v ) Visitor* v )
: SwitchArg(flag, name, desc, false, v), : SwitchArg(flag, name, desc, false, v),
_value( init ) _value( init ),
_default( init )
{ } { }
inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
@ -135,7 +144,8 @@ inline MultiSwitchArg::MultiSwitchArg(const std::string& flag,
int init, int init,
Visitor* v ) Visitor* v )
: SwitchArg(flag, name, desc, false, v), : SwitchArg(flag, name, desc, false, v),
_value( init ) _value( init ),
_default( init )
{ {
parser.add( this ); parser.add( this );
} }
@ -191,6 +201,12 @@ MultiSwitchArg::longID(const std::string& val) const
return Arg::longID(val) + " (accepted multiple times)"; return Arg::longID(val) + " (accepted multiple times)";
} }
inline void
MultiSwitchArg::reset()
{
MultiSwitchArg::_value = MultiSwitchArg::_default;
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
//END MultiSwitchArg.cpp //END MultiSwitchArg.cpp
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////

View File

@ -45,6 +45,12 @@ class SwitchArg : public Arg
*/ */
bool _value; bool _value;
/**
* Used to support the reset() method so that ValueArg can be
* reset to their constructed value.
*/
bool _default;
public: public:
/** /**
@ -107,6 +113,8 @@ class SwitchArg : public Arg
* Returns bool, whether or not the switch has been set. * Returns bool, whether or not the switch has been set.
*/ */
bool getValue(); bool getValue();
virtual void reset();
}; };
@ -116,20 +124,22 @@ class SwitchArg : public Arg
inline SwitchArg::SwitchArg(const std::string& flag, inline SwitchArg::SwitchArg(const std::string& flag,
const std::string& name, const std::string& name,
const std::string& desc, const std::string& desc,
bool _default, bool default_val,
Visitor* v ) Visitor* v )
: Arg(flag, name, desc, false, false, v), : Arg(flag, name, desc, false, false, v),
_value( _default ) _value( default_val ),
_default( default_val )
{ } { }
inline SwitchArg::SwitchArg(const std::string& flag, inline SwitchArg::SwitchArg(const std::string& flag,
const std::string& name, const std::string& name,
const std::string& desc, const std::string& desc,
CmdLineInterface& parser, CmdLineInterface& parser,
bool _default, bool default_val,
Visitor* v ) Visitor* v )
: Arg(flag, name, desc, false, false, v), : Arg(flag, name, desc, false, false, v),
_value( _default ) _value( default_val ),
_default(default_val)
{ {
parser.add( this ); parser.add( this );
} }
@ -204,6 +214,11 @@ inline bool SwitchArg::processArg(int *i, std::vector<std::string>& args)
return false; return false;
} }
inline void SwitchArg::reset()
{
Arg::reset();
_value = _default;
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
//End SwitchArg.cpp //End SwitchArg.cpp
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////

View File

@ -51,6 +51,12 @@ class ValueArg : public Arg
*/ */
T _value; 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. * A human readable description of the type to be parsed.
* This is a hack, plain and simple. Ideally we would use RTTI to * 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. * \param val - value to be used.
*/ */
virtual std::string longID(const std::string& val = "val") const; virtual std::string longID(const std::string& val = "val") const;
virtual void reset() ;
}; };
@ -244,6 +252,7 @@ ValueArg<T>::ValueArg(const std::string& flag,
Visitor* v) Visitor* v)
: Arg(flag, name, desc, req, true, v), : Arg(flag, name, desc, req, true, v),
_value( val ), _value( val ),
_default( val ),
_typeDesc( typeDesc ), _typeDesc( typeDesc ),
_constraint( NULL ) _constraint( NULL )
{ } { }
@ -259,6 +268,7 @@ ValueArg<T>::ValueArg(const std::string& flag,
Visitor* v) Visitor* v)
: Arg(flag, name, desc, req, true, v), : Arg(flag, name, desc, req, true, v),
_value( val ), _value( val ),
_default( val ),
_typeDesc( typeDesc ), _typeDesc( typeDesc ),
_constraint( NULL ) _constraint( NULL )
{ {
@ -275,6 +285,7 @@ ValueArg<T>::ValueArg(const std::string& flag,
Visitor* v) Visitor* v)
: Arg(flag, name, desc, req, true, v), : Arg(flag, name, desc, req, true, v),
_value( val ), _value( val ),
_default( val ),
_typeDesc( constraint->shortID() ), _typeDesc( constraint->shortID() ),
_constraint( constraint ) _constraint( constraint )
{ } { }
@ -290,6 +301,7 @@ ValueArg<T>::ValueArg(const std::string& flag,
Visitor* v) Visitor* v)
: Arg(flag, name, desc, req, true, v), : Arg(flag, name, desc, req, true, v),
_value( val ), _value( val ),
_default( val ),
_typeDesc( constraint->shortID() ), _typeDesc( constraint->shortID() ),
_constraint( constraint ) _constraint( constraint )
{ {
@ -387,6 +399,13 @@ void ValueArg<T>::_extractValue( const std::string& val )
toString() ) ); toString() ) );
} }
template<class T>
void ValueArg<T>::reset()
{
Arg::reset();
_value = _default;
}
} // namespace TCLAP } // namespace TCLAP
#endif #endif