ValueArg.h
Go to the documentation of this file.
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/******************************************************************************
*
* file: ValueArg.h
*
* Copyright (c) 2003, Michael E. Smoot .
* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
* Copyright (c) 2017, Google LLC
* All rights reserved.
*
* 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 TCLAP_VALUE_ARGUMENT_H
#define TCLAP_VALUE_ARGUMENT_H
#include <string>
#include <vector>
#include <tclap/Arg.h>
#include <tclap/Constraint.h>
namespace TCLAP {
template<class T>
class ValueArg : public Arg
{
protected:
T _value;
T _default;
std::string _typeDesc;
Constraint<T>* _constraint;
void _extractValue( const std::string& val );
public:
ValueArg( const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T value,
const std::string& typeDesc,
Visitor* v = NULL);
ValueArg( const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T value,
const std::string& typeDesc,
CmdLineInterface& parser,
Visitor* v = NULL );
ValueArg( const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T value,
Constraint<T>* constraint,
CmdLineInterface& parser,
Visitor* v = NULL );
ValueArg( const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T value,
Constraint<T>* constraint,
Visitor* v = NULL );
virtual bool processArg(int* i, std::vector<std::string>& args);
const T& getValue() const { return _value; }
// TODO(macbishop): Non-const variant is deprecated, don't
// use. Remove in next major.
T& getValue() { return _value; }
operator const T&() const { return getValue(); }
virtual std::string shortID(const std::string& val = "val") const;
virtual std::string longID(const std::string& val = "val") const;
virtual void reset() ;
private:
ValueArg(const ValueArg<T>& rhs);
ValueArg& operator=(const ValueArg<T>& rhs);
};
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,
Visitor* v)
: Arg(flag, name, desc, req, true, v),
_value( val ),
_default( val ),
_typeDesc( typeDesc ),
_constraint( NULL )
{ }
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,
CmdLineInterface& parser,
Visitor* v)
: Arg(flag, name, desc, req, true, v),
_value( val ),
_default( val ),
_typeDesc( typeDesc ),
_constraint( NULL )
{
parser.add( this );
}
template<class T>
ValueArg<T>::ValueArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T val,
Constraint<T>* constraint,
Visitor* v)
: Arg(flag, name, desc, req, true, v),
_value( val ),
_default( val ),
_typeDesc( Constraint<T>::shortID(constraint) ),
_constraint( constraint )
{ }
template<class T>
ValueArg<T>::ValueArg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
T val,
Constraint<T>* constraint,
CmdLineInterface& parser,
Visitor* v)
: Arg(flag, name, desc, req, true, v),
_value( val ),
_default( val ),
_typeDesc( Constraint<T>::shortID(constraint) ), // TODO(macbishop): Will crash
// if constraint is NULL
_constraint( constraint )
{
parser.add( this );
}
template<class T>
bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
{
if ( _ignoreable && Arg::ignoreRest() )
return false;
if ( _hasBlanks( args[*i] ) )
return false;
std::string flag = args[*i];
std::string value = "";
trimFlag( flag, value );
if ( argMatches( flag ) )
{
if ( _alreadySet )
{
if ( _xorSet )
throw( CmdLineParseException("Mutually exclusive argument"
" already set!", toString()));
else
throw( CmdLineParseException("Argument already set!",
toString()) );
}
if ( Arg::delimiter() != ' ' && value == "" )
throw( ArgParseException("Couldn't find delimiter for this argument!",
toString() ) );
if ( value == "" )
{
(*i)++;
if ( static_cast<unsigned int>(*i) < args.size() )
_extractValue( args[*i] );
else
throw( ArgParseException("Missing a value for this argument!",
toString() ) );
}
else
_extractValue( value );
_alreadySet = true;
_checkWithVisitor();
return true;
}
else
return false;
}
template<class T>
std::string ValueArg<T>::shortID(const std::string& val) const
{
static_cast<void>(val); // Ignore input, don't warn
return Arg::shortID( _typeDesc );
}
template<class T>
std::string ValueArg<T>::longID(const std::string& val) const
{
static_cast<void>(val); // Ignore input, don't warn
return Arg::longID( _typeDesc );
}
template<class T>
void ValueArg<T>::_extractValue( const std::string& val )
{
try {
ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
} catch( ArgParseException &e) {
throw ArgParseException(e.error(), toString());
}
if ( _constraint != NULL )
if ( ! _constraint->check( _value ) )
throw( CmdLineParseException( "Value '" + val +
+ "' does not meet constraint: "
+ _constraint->description(),
toString() ) );
}
template<class T>
void ValueArg<T>::reset()
{
Arg::reset();
_value = _default;
}
} // namespace TCLAP
#endif
Arg.h
Constraint.h
TCLAP::ArgException::error
std::string error() const
Returns the error text.
Definition: ArgException.h:65
TCLAP::ArgParseException
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:123
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:56
TCLAP::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:514
TCLAP::Arg::ignoreRest
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:196
TCLAP::Arg::delimiter
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:202
TCLAP::Arg::reset
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:670
TCLAP::Arg::shortID
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:496
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:47
TCLAP::CmdLineInterface::add
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
TCLAP::CmdLineParseException
Thrown from CmdLine when the arguments on the command line are not properly specified,...
Definition: ArgException.h:145
TCLAP::Constraint
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:43
TCLAP::ValueArg
The basic labeled argument that parses a value.
Definition: ValueArg.h:47
TCLAP::ValueArg::longID
virtual std::string longID(const std::string &val="val") const
Specialization of longID.
Definition: ValueArg.h:398
TCLAP::ValueArg::_default
T _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
Definition: ValueArg.h:61
TCLAP::ValueArg::reset
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: ValueArg.h:422
TCLAP::ValueArg::getValue
T & getValue()
Definition: ValueArg.h:230
TCLAP::ValueArg::_value
T _value
The value parsed from the command line.
Definition: ValueArg.h:55
TCLAP::ValueArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: ValueArg.h:335
TCLAP::ValueArg::_typeDesc
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:70
TCLAP::ValueArg::_constraint
Constraint< T > * _constraint
A Constraint this Arg must conform to.
Definition: ValueArg.h:75
TCLAP::ValueArg::ValueArg
ValueArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, T value, const std::string &typeDesc, Visitor *v=NULL)
Labeled ValueArg constructor.
Definition: ValueArg.h:265
TCLAP::ValueArg::getValue
const T & getValue() const
Returns the value of the argument.
Definition: ValueArg.h:226
TCLAP::ValueArg::_extractValue
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:405
TCLAP::ValueArg::shortID
virtual std::string shortID(const std::string &val="val") const
Specialization of shortID.
Definition: ValueArg.h:388
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:35
TCLAP
Definition: Arg.h:48
TCLAP::ExtractValue
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:406
TCLAP::ValueLike
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:39