Support for automatic addition to a CmdLine parser

This commit is contained in:
macbishop 2004-09-09 19:55:33 +00:00
parent 1064a196d1
commit 35a072e0b6
6 changed files with 439 additions and 33 deletions

View File

@ -32,6 +32,8 @@
namespace TCLAP {
class CmdLine;
/**
* A base class that defines the essential data for all arguments.
* This is not an abstract class, although it can't really be used
@ -197,7 +199,26 @@ class Arg
bool req,
bool valreq,
Visitor* v = NULL);
/**
* Constructor with default parser, the argument is automatically added
* to a parser (i.e. no subsequent call to add() is requried).
*
* \param flag - The flag identifying the argument.
* \param name - The name identifying the argument.
* \param desc - The description of the argument, used in the usage.
* \param req - Whether the argument is required.
* \param valreq - Whether the a value is required for the argument.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - The visitor checked by the argument. Defaults to NULL.
*/
Arg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
bool valreq,
CmdLine &parser,
Visitor* v = NULL);
/**
* Null constructor.
* Everything set to null/blank/0 values.
@ -342,7 +363,8 @@ class Arg
* \param s - Set the requireLabel to this value.
*/
void setRequireLabel( const std::string& s );
private:
void init();
};
/**

View File

@ -27,6 +27,7 @@
#include <vector>
#include <sstream>
#include <tclap/Visitor.h>
#include <tclap/CommandLine.h>
namespace TCLAP {
@ -99,6 +100,31 @@ class MultiArg : public Arg
const std::string& typeDesc,
Visitor* v = NULL);
/**
* Constructor.
* \param flag - The one character flag that identifies this
* argument on the command line.
* \param name - A one word name for the argument. Can be
* used as a long flag on the command line.
* \param desc - A description of what the argument is for or
* does.
* \param req - Whether the argument is required on the command
* line.
* \param typeDesc - A short, human readable description of the
* type that this object expects. This is used in the generation
* of the USAGE statement. The goal is to be helpful to the end user
* of the program.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
MultiArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
const std::string& typeDesc,
CmdLine &parser,
Visitor* v);
/**
* Constructor.
* \param flag - The one character flag that identifies this
@ -120,8 +146,32 @@ class MultiArg : public Arg
bool req,
const std::vector<T>& allowed,
Visitor* v = NULL);
/**
* Constructor.
* \param flag - The one character flag that identifies this
* argument on the command line.
* \param name - A one word name for the argument. Can be
* used as a long flag on the command line.
* \param desc - A description of what the argument is for or
* does.
* \param req - Whether the argument is required on the command
* line.
* \param allowed - A vector of type T that where the values in the
* vector are the only values allowed for the arg.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
MultiArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
const std::vector<T>& allowed,
CmdLine &parser,
Visitor* v);
/**
* Destructor.
*/
~MultiArg();
@ -159,9 +209,26 @@ class MultiArg : public Arg
* required.
*/
virtual bool isRequired() const;
private:
void init();
};
template<class T>
void MultiArg<T>::init()
{
for ( unsigned int i = 0; i < _allowed.size(); i++ )
{
std::ostringstream os;
os << _allowed[i];
std::string temp( os.str() );
if ( i > 0 )
_typeDesc += "|";
_typeDesc += temp;
}
}
/**
*
*/
@ -176,6 +243,22 @@ MultiArg<T>::MultiArg(const std::string& flag,
_typeDesc( typeDesc )
{ };
template<class T>
MultiArg<T>::MultiArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
const std::string& typeDesc,
CmdLine &parser,
Visitor* v)
: Arg( flag, name, desc, req, true, v ),
_typeDesc( typeDesc )
{
//Do explicit add here instead of using Args parser constructor
//becase then we can not initialize first. Perhaps init would like
//to throw an exception or something in the future.
parser.add(*this);
}
/**
*
@ -190,19 +273,26 @@ MultiArg<T>::MultiArg(const std::string& flag,
: Arg( flag, name, desc, req, true, v ),
_allowed( allowed )
{
for ( unsigned int i = 0; i < _allowed.size(); i++ )
{
std::ostringstream os;
os << _allowed[i];
std::string temp( os.str() );
if ( i > 0 )
_typeDesc += "|";
_typeDesc += temp;
}
init();
};
template<class T>
MultiArg<T>::MultiArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
const std::vector<T>& allowed,
CmdLine &parser,
Visitor* v)
: Arg( flag, name, desc, req, true, v ),
_allowed( allowed )
{
init();
//Do explicit add here instead of using Args parser constructor
//becase then we can not initialize first. Perhaps init would like
//to throw an exception or something in the future.
parser.add(*this);
}
/**
*

View File

@ -66,6 +66,28 @@ class SwitchArg : public Arg
bool def,
Visitor* v = NULL);
/**
* SwitchArg constructor.
* \param flag - The one character flag that identifies this
* argument on the command line.
* \param name - A one word name for the argument. Can be
* used as a long flag on the command line.
* \param desc - A description of what the argument is for or
* does.
* \param def - The default value for this Switch.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
SwitchArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool def,
CmdLine &parser,
Visitor* v = NULL);
/**
* Destructor.
*/

View File

@ -28,7 +28,7 @@
#include <sstream>
#include <tclap/Visitor.h>
#include <tclap/Arg.h>
#include <tclap/CommandLine.h>
namespace TCLAP {
@ -74,6 +74,47 @@ class UnlabeledMultiArg : public MultiArg<T>
const std::string& typeDesc,
bool ignoreable = false,
Visitor* v = NULL );
/**
* Constructor.
* \param name - The name of the Arg. Note that this is used for
* identification, not as a long flag.
* \param desc - A description of what the argument is for or
* does.
* \param typeDesc - A short, human readable description of the
* type that this object expects. This is used in the generation
* of the USAGE statement. The goal is to be helpful to the end user
* of the program.
* \param ignoreable - Whether or not this argument can be ignored
* using the "--" flag.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
UnlabeledMultiArg( const std::string& name,
const std::string& desc,
const std::string& typeDesc,
bool ignoreable = false,
CmdLine &parser,
Visitor* v = NULL );
/**
* Constructor.
* \param name - The name of the Arg. Note that this is used for
* identification, not as a long flag.
* \param desc - A description of what the argument is for or
* does.
* \param allowed - A vector of type T that where the values in the
* vector are the only values allowed for the arg.
* \param ignoreable - Whether or not this argument can be ignored
* using the "--" flag.
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
UnlabeledMultiArg( const std::string& name,
const std::string& desc,
const std::vector<T>& allowed,
bool ignoreable = false,
Visitor* v = NULL );
/**
* Constructor.
@ -85,15 +126,17 @@ class UnlabeledMultiArg : public MultiArg<T>
* vector are the only values allowed for the arg.
* \param ignoreable - Whether or not this argument can be ignored
* using the "--" flag.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
UnlabeledMultiArg( const std::string& name,
const std::string& desc,
const std::vector<T>& allowed,
bool ignoreable = false,
Visitor* v = NULL );
UnlabeledMultiArg(const std::string& name,
const std::string& desc,
const std::vector<T>& allowed,
bool ignoreable,
CmdLine &parser,
Visitor* v);
/**
* Handles the processing of the argument.
* This re-implements the Arg version of this method to set the
@ -140,6 +183,20 @@ UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
_ignoreable = ignoreable;
};
template<class T>
UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
const std::string& desc,
const std::string& typeDesc,
bool ignoreable,
CmdLine &parser,
Visitor* v)
: MultiArg<T>("", name, desc, false, typeDesc, v)
{
_ignoreable = ignoreable;
parser.add(*this);
}
template<class T>
UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
const std::string& desc,
@ -151,6 +208,20 @@ UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
_ignoreable = ignoreable;
};
template<class T>
UnlabeledMultiArg<T>::UnlabeledMultiArg(const std::string& name,
const std::string& desc,
const std::vector<T>& allowed,
bool ignoreable,
CmdLine &parser,
Visitor* v)
: MultiArg<T>("", name, desc, false, allowed, v)
{
_ignoreable = ignoreable;
parser.add(*this);
}
template<class T>
bool UnlabeledMultiArg<T>::processArg(int *i, std::vector<std::string>& args)
{

View File

@ -29,6 +29,7 @@
#include <tclap/Visitor.h>
#include <tclap/Arg.h>
#include <tclap/ValueArg.h>
#include <tclap/CommandLine.h>
namespace TCLAP {
@ -86,6 +87,38 @@ class UnlabeledValueArg : public ValueArg<T>
bool ignoreable = false,
Visitor* v = NULL);
/**
* UnlabeledValueArg constructor.
* Note that this constructor does not have a required flag. Any
* unlabeled argument added to the CmdLine is by default required.
* If you want optional, unlabeled arguments then use an
* UnlabeledMultiArg.
* \param name - A one word name for the argument. Can be
* used as a long flag on the command line.
* \param desc - A description of what the argument is for or
* does.
* \param value - The default value assigned to this argument if it
* is not present on the command line.
* \param typeDesc - A short, human readable description of the
* type that this object expects. This is used in the generation
* of the USAGE statement. The goal is to be helpful to the end user
* of the program.
* \param ignoreable - Allows you to specify that this argument can be
* ignored if the '--' flag is set. This defaults to false (cannot
* be ignored) and should generally stay that way unless you have
* some special need for certain arguments to be ignored.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - Optional Vistor. You should leave this blank unless
* you have a very good reason.
*/
UnlabeledValueArg(const std::string& name,
const std::string& desc,
T value,
const std::string& typeDesc,
bool ignoreable = false,
CmdLine &parser,
Visitor* v = NULL);
/**
* UnlabeledValueArg constructor.
* Note that this constructor does not have a required flag. Any
@ -114,6 +147,37 @@ class UnlabeledValueArg : public ValueArg<T>
bool ignoreable = false,
Visitor* v = NULL);
/**
* UnlabeledValueArg constructor.
* Note that this constructor does not have a required flag. Any
* unlabeled argument added to the CmdLine is by default required.
* If you want optional, unlabeled arguments then use an
* UnlabeledMultiArg.
* \param name - A one word name for the argument. Can be
* used as a long flag on the command line.
* \param desc - A description of what the argument is for or
* does.
* \param value - The default value assigned to this argument if it
* is not present on the command line.
* \param allowed - A vector of type T that where the values in the
* vector are the only values allowed for the arg.
* \param ignoreable - Allows you to specify that this argument can be
* ignored if the '--' flag is set. This defaults to false (cannot
* be ignored) and should generally stay that way unless you have
* some special need for certain arguments to be ignored.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - Optional Vistor. You should leave this blank unless
* you have a very good reason.
*/
UnlabeledValueArg(const std::string& name,
const std::string& desc,
T value,
const std::vector<T>& allowed,
bool ignoreable = false,
CmdLine &parser,
Visitor* v = NULL);
/**
* Handles the processing of the argument.
* This re-implements the Arg version of this method to set the
@ -161,6 +225,20 @@ UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
_ignoreable = ignoreable;
};
template<class T>
UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
const std::string& desc,
T val,
const std::string& typeDesc,
bool ignoreable,
CmdLine &parser,
Visitor* v)
: ValueArg<T>("", name, desc, true, val, typeDesc, v)
{
_ignoreable = ignoreable;
parser.add(*this);
};
/**
* Constructor implemenation.
*/
@ -176,6 +254,20 @@ UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
_ignoreable = ignoreable;
};
template<class T>
UnlabeledValueArg<T>::UnlabeledValueArg(const std::string& name,
const std::string& desc,
T val,
const std::vector<T>& allowed,
bool ignoreable,
CmdLine &parser,
Visitor* v)
: ValueArg<T>("", name, desc, true, val, allowed, v)
{
_ignoreable = ignoreable;
parser.add(*this);
}
/**
* Implementation of processArg().
*/

View File

@ -114,7 +114,72 @@ class ValueArg : public Arg
T value,
const std::string& typeDesc,
Visitor* v = NULL);
/**
* Labeled ValueArg constructor.
* You could conceivably call this constructor with a blank flag,
* but that would make you a bad person. It would also cause
* an exception to be thrown. If you want an unlabeled argument,
* use the other constructor.
* \param flag - The one character flag that identifies this
* argument on the command line.
* \param name - A one word name for the argument. Can be
* used as a long flag on the command line.
* \param desc - A description of what the argument is for or
* does.
* \param req - Whether the argument is required on the command
* line.
* \param value - The default value assigned to this argument if it
* is not present on the command line.
* \param typeDesc - A short, human readable description of the
* type that this object expects. This is used in the generation
* of the USAGE statement. The goal is to be helpful to the end user
* of the program.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
ValueArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T value,
const std::string& typeDesc,
CmdLine &parser,
Visitor* v = NULL);
/**
* Labeled ValueArg constructor.
* You could conceivably call this constructor with a blank flag,
* but that would make you a bad person. It would also cause
* an exception to be thrown. If you want an unlabeled argument,
* use the other constructor.
* \param flag - The one character flag that identifies this
* argument on the command line.
* \param name - A one word name for the argument. Can be
* used as a long flag on the command line.
* \param desc - A description of what the argument is for or
* does.
* \param req - Whether the argument is required on the command
* line.
* \param value - The default value assigned to this argument if it
* is not present on the command line.
* \param allowed - A vector of type T that where the values in the
* vector are the only values allowed for the arg.
* \param parser - A CmdLine parser object to add this Arg to
* \param v - An optional visitor. You probably should not
* use this unless you have a very good reason.
*/
ValueArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T val,
const std::vector<T>& allowed,
CmdLine &parser,
Visitor* v);
/**
* Labeled ValueArg constructor.
* You could conceivably call this constructor with a blank flag,
@ -177,8 +242,26 @@ class ValueArg : public Arg
*/
virtual std::string longID(const std::string& val = "val") const;
private:
void init();
};
template<class T>
void ValueArg<T>::init()
{
for ( unsigned int i = 0; i < _allowed.size(); i++ )
{
std::ostringstream os;
os << _allowed[i];
std::string temp( os.str() );
if ( i > 0 )
_typeDesc += "|";
_typeDesc += temp;
}
}
/**
* Constructor implementation.
@ -196,6 +279,22 @@ ValueArg<T>::ValueArg(const std::string& flag,
_typeDesc( typeDesc )
{ }
template<class T>
ValueArg<T>::ValueArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T val,
const std::string& typeDesc,
CmdLine& parser,
Visitor* v)
: Arg(flag, name, desc, req, true, v),
_value( val ),
_typeDesc( typeDesc )
{
parser.add(*this);
}
/**
* Constructor with allowed list.
*/
@ -211,17 +310,27 @@ ValueArg<T>::ValueArg(const std::string& flag,
_value( val ),
_allowed( allowed )
{
for ( unsigned int i = 0; i < _allowed.size(); i++ )
{
std::ostringstream os;
os << _allowed[i];
init();
}
std::string temp( os.str() );
if ( i > 0 )
_typeDesc += "|";
_typeDesc += temp;
}
template<class T>
ValueArg<T>::ValueArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T val,
const std::vector<T>& allowed,
CmdLine &parser,
Visitor* v)
: Arg(flag, name, desc, req, true, v),
_value( val ),
_allowed( allowed )
{
init();
//Do explicit add here instead of using Args parser constructor
//becase then we can not initialize first. Perhaps init would like
//to throw an exception or something in the future.
parser.add();
}