/****************************************************************************** * * file: ValueArg.h * * Copyright (c) 2003, Michael E. Smoot . * All rights reverved. * * See the file COPYING in the top directory of this distribution for * more information. * * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * *****************************************************************************/ #ifndef __VALUE_ARGUMENT_HH__ #define __VALUE_ARGUMENT_HH__ #include #include #include #include #include using namespace std; namespace TCLAP { /** * The basic labeled argument that parses a value. * This is a template class, which means the type T defines the type * that a given object will attempt to parse when the flag/name is matched * on the command line. While there is nothing stopping you from creating * an unflagged ValueArg, it is unwise and would cause significant problems. * Instead use an UnlabeledValueArg. */ template class ValueArg : public Arg { protected: /** * The value parsed from the command line. * Can be of any type, as long as the >> operator for the type * is defined. */ T _value; /** * A human readable description of the type to be parsed. * This is a hack, plain and simple. Ideally we would use RTTI to * return the name of type T, but until there is some sort of * consistent support for human readable names, we are left to our * own devices. */ string _typeDesc; /** * Extracts the string at position i from the args list. * 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. */ void _extractValue( int i, vector& args ); public: /** * 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 v - An optional visitor. You probably should not * use this unless you have a very good reason. */ ValueArg(const string& flag, const string& name, const string& desc, bool req, T value, const string& typeDesc, Visitor* v = NULL); /** * Destructor. */ ~ValueArg(); /** * Handles the processing of the argument. * This re-implements the Arg version of this method to set the * _value of the argument appropriately. It knows the difference * between labeled and unlabeled. * \param i - Pointer the the current argument in the list. * \param args - Mutable list of strings. Passed * in from main(). */ virtual bool processArg(int* i, vector& args ); /** * Returns the value of the argument. */ T& getValue() ; /** * Specialization of shortID. */ virtual string shortID(const string& val = "val") const; /** * Specialization of longID. */ virtual string longID(const string& val = "val") const; }; /** * Constructor implementation. */ template ValueArg::ValueArg(const string& flag, const string& name, const string& desc, bool req, T val, const string& typeDesc, Visitor* v) : Arg(flag, name, desc, req, true, v), _value( val ), _typeDesc( typeDesc ) { }; /** * Destructor implementation. */ template ValueArg::~ValueArg() { }; /** * Implementation of getValue(). */ template T& ValueArg::getValue() { return _value; }; /** * Implementation of processArg(). */ template bool ValueArg::processArg(int *i, vector& args) { if ( _ignoreable && Arg::ignoreRest() ) return false; string flag = args[*i]; if ( argMatches( flag ) ) { if ( _alreadySet ) throw( ArgException("Argument already set!", toString()) ); (*i)++; if (*i < args.size() ) { _extractValue( *i, args); _alreadySet = true; _checkWithVisitor(); return true; } else throw( ArgException("Missing a value for this argument!", toString() ) ); } else return false; } /** * Implementation of _extractValue. */ template void ValueArg::_extractValue(int i, vector& args) { istringstream is(args[i]); is >> _value; if ( is.fail() ) throw( ArgException("Couldn't read argument value!", toString() ) ); } /** * Implementation of shortID. */ template string ValueArg::shortID(const string& val) const { return Arg::shortID( _typeDesc ); } /** * Implementation of longID. */ template string ValueArg::longID(const string& val) const { return Arg::longID( _typeDesc ); } } #endif