diff --git a/include/tclap/Arg.h b/include/tclap/Arg.h index 32562b4..8127be9 100644 --- a/include/tclap/Arg.h +++ b/include/tclap/Arg.h @@ -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(); }; /** diff --git a/include/tclap/MultiArg.h b/include/tclap/MultiArg.h index a58c1b1..e2e2fe9 100644 --- a/include/tclap/MultiArg.h +++ b/include/tclap/MultiArg.h @@ -27,6 +27,7 @@ #include #include #include +#include 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& 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& allowed, + CmdLine &parser, + Visitor* v); + + /** * Destructor. */ ~MultiArg(); @@ -159,9 +209,26 @@ class MultiArg : public Arg * required. */ virtual bool isRequired() const; - +private: + void init(); }; +template +void MultiArg::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::MultiArg(const std::string& flag, _typeDesc( typeDesc ) { }; +template +MultiArg::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::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 +MultiArg::MultiArg(const std::string& flag, + const std::string& name, + const std::string& desc, + bool req, + const std::vector& 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); +} /** * diff --git a/include/tclap/SwitchArg.h b/include/tclap/SwitchArg.h index 63dbb3f..537acc8 100644 --- a/include/tclap/SwitchArg.h +++ b/include/tclap/SwitchArg.h @@ -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. */ diff --git a/include/tclap/UnlabeledMultiArg.h b/include/tclap/UnlabeledMultiArg.h index 2afec70..5b61edc 100644 --- a/include/tclap/UnlabeledMultiArg.h +++ b/include/tclap/UnlabeledMultiArg.h @@ -28,7 +28,7 @@ #include #include -#include +#include namespace TCLAP { @@ -74,6 +74,47 @@ class UnlabeledMultiArg : public MultiArg 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& allowed, + bool ignoreable = false, + Visitor* v = NULL ); /** * Constructor. @@ -85,15 +126,17 @@ class UnlabeledMultiArg : public MultiArg * 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& allowed, - bool ignoreable = false, - Visitor* v = NULL ); - + UnlabeledMultiArg(const std::string& name, + const std::string& desc, + const std::vector& 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::UnlabeledMultiArg(const std::string& name, _ignoreable = ignoreable; }; +template +UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, + const std::string& desc, + const std::string& typeDesc, + bool ignoreable, + CmdLine &parser, + Visitor* v) +: MultiArg("", name, desc, false, typeDesc, v) +{ + _ignoreable = ignoreable; + parser.add(*this); +} + + template UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, const std::string& desc, @@ -151,6 +208,20 @@ UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, _ignoreable = ignoreable; }; +template +UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, + const std::string& desc, + const std::vector& allowed, + bool ignoreable, + CmdLine &parser, + Visitor* v) +: MultiArg("", name, desc, false, allowed, v) +{ + _ignoreable = ignoreable; + parser.add(*this); +} + + template bool UnlabeledMultiArg::processArg(int *i, std::vector& args) { diff --git a/include/tclap/UnlabeledValueArg.h b/include/tclap/UnlabeledValueArg.h index 6d55845..4e292dc 100644 --- a/include/tclap/UnlabeledValueArg.h +++ b/include/tclap/UnlabeledValueArg.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace TCLAP { @@ -86,6 +87,38 @@ class UnlabeledValueArg : public ValueArg 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 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& 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::UnlabeledValueArg(const std::string& name, _ignoreable = ignoreable; }; +template +UnlabeledValueArg::UnlabeledValueArg(const std::string& name, + const std::string& desc, + T val, + const std::string& typeDesc, + bool ignoreable, + CmdLine &parser, + Visitor* v) +: ValueArg("", name, desc, true, val, typeDesc, v) +{ + _ignoreable = ignoreable; + parser.add(*this); +}; + /** * Constructor implemenation. */ @@ -176,6 +254,20 @@ UnlabeledValueArg::UnlabeledValueArg(const std::string& name, _ignoreable = ignoreable; }; +template +UnlabeledValueArg::UnlabeledValueArg(const std::string& name, + const std::string& desc, + T val, + const std::vector& allowed, + bool ignoreable, + CmdLine &parser, + Visitor* v) +: ValueArg("", name, desc, true, val, allowed, v) +{ + _ignoreable = ignoreable; + parser.add(*this); +} + /** * Implementation of processArg(). */ diff --git a/include/tclap/ValueArg.h b/include/tclap/ValueArg.h index ecec527..01d68ec 100644 --- a/include/tclap/ValueArg.h +++ b/include/tclap/ValueArg.h @@ -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& 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 +void ValueArg::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::ValueArg(const std::string& flag, _typeDesc( typeDesc ) { } +template +ValueArg::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::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 +ValueArg::ValueArg(const std::string& flag, + const std::string& name, + const std::string& desc, + bool req, + T val, + const std::vector& 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(); }