Arg.h
Go to the documentation of this file.
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/******************************************************************************
*
* file: Arg.h
*
* Copyright (c) 2003, Michael E. Smoot .
* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno .
* Copyright (c) 2017 Google Inc.
* 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_ARGUMENT_H
#define TCLAP_ARGUMENT_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <vector>
#include <list>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <tclap/sstream.h>
#include <tclap/ArgException.h>
#include <tclap/Visitor.h>
#include <tclap/CmdLineInterface.h>
#include <tclap/ArgTraits.h>
#include <tclap/StandardTraits.h>
namespace TCLAP {
class Arg
{
private:
Arg(const Arg& rhs);
Arg& operator=(const Arg& rhs);
static bool& ignoreRestRef() { static bool ign = false; return ign; }
static char& delimiterRef() { static char delim = ' '; return delim; }
protected:
std::string _flag;
std::string _name;
std::string _description;
bool _required;
std::string _requireLabel;
bool _valueRequired;
bool _alreadySet;
Visitor* _visitor;
bool _ignoreable;
bool _xorSet;
bool _acceptsMultipleValues;
void _checkWithVisitor() const;
Arg( const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
bool valreq,
Visitor* v = NULL );
public:
virtual ~Arg();
virtual void addToList( std::list<Arg*>& argList ) const;
static void beginIgnoring() { ignoreRestRef() = true; }
static bool ignoreRest() { return ignoreRestRef(); }
static char delimiter() { return delimiterRef(); }
static char blankChar() { return (char)7; }
#ifndef TCLAP_FLAGSTARTCHAR
#define TCLAP_FLAGSTARTCHAR '-'
#endif
static char flagStartChar() { return TCLAP_FLAGSTARTCHAR; }
#ifndef TCLAP_FLAGSTARTSTRING
#define TCLAP_FLAGSTARTSTRING "-"
#endif
static const std::string flagStartString() { return TCLAP_FLAGSTARTSTRING; }
#ifndef TCLAP_NAMESTARTSTRING
#define TCLAP_NAMESTARTSTRING "--"
#endif
static const std::string nameStartString() { return TCLAP_NAMESTARTSTRING; }
static const std::string ignoreNameString() { return "ignore_rest"; }
static void setDelimiter( char c ) { delimiterRef() = c; }
virtual bool processArg(int *i, std::vector<std::string>& args) = 0;
virtual bool operator==(const Arg& a) const;
const std::string& getFlag() const;
const std::string& getName() const;
std::string getDescription() const;
virtual bool isRequired() const;
void forceRequired();
void xorSet();
bool isValueRequired() const;
bool isSet() const;
bool isIgnoreable() const;
virtual bool argMatches( const std::string& s ) const;
virtual std::string toString() const;
virtual std::string shortID( const std::string& valueId = "val" ) const;
virtual std::string longID( const std::string& valueId = "val" ) const;
virtual void trimFlag( std::string& flag, std::string& value ) const;
bool _hasBlanks( const std::string& s ) const;
void setRequireLabel( const std::string& s );
virtual bool allowMore();
virtual bool acceptsMultipleValues();
virtual void reset();
};
typedef std::list<Arg*>::const_iterator ArgListIterator;
typedef std::vector<Arg*>::const_iterator ArgVectorIterator;
typedef std::list<Visitor*>::const_iterator VisitorListIterator;
/*
* Extract a value of type T from it's string representation contained
* in strVal. The ValueLike parameter used to select the correct
* specialization of ExtractValue depending on the value traits of T.
* ValueLike traits use operator>> to assign the value from strVal.
*/
template<typename T> void
ExtractValue(T &destVal, const std::string& strVal, ValueLike vl)
{
static_cast<void>(vl); // Avoid warning about unused vl
istringstream is(strVal.c_str());
int valuesRead = 0;
while ( is.good() ) {
if ( is.peek() != EOF )
#ifdef TCLAP_SETBASE_ZERO
is >> std::setbase(0) >> destVal;
#else
is >> destVal;
#endif
else
break;
valuesRead++;
}
if ( is.fail() )
throw( ArgParseException("Couldn't read argument value "
"from string '" + strVal + "'"));
if ( valuesRead > 1 )
throw( ArgParseException("More than one valid value parsed from "
"string '" + strVal + "'"));
}
/*
* Extract a value of type T from it's string representation contained
* in strVal. The ValueLike parameter used to select the correct
* specialization of ExtractValue depending on the value traits of T.
* StringLike uses assignment (operator=) to assign from strVal.
*/
template<typename T> void
ExtractValue(T &destVal, const std::string& strVal, StringLike sl)
{
static_cast<void>(sl); // Avoid warning about unused sl
SetString(destVal, strVal);
}
//BEGIN Arg.cpp
inline Arg::Arg(const std::string& flag,
const std::string& name,
const std::string& desc,
bool req,
bool valreq,
Visitor* v) :
_flag(flag),
_name(name),
_description(desc),
_required(req),
_requireLabel("required"),
_valueRequired(valreq),
_alreadySet(false),
_visitor( v ),
_ignoreable(true),
_xorSet(false),
_acceptsMultipleValues(false)
{
if ( _flag.length() > 1 )
throw(SpecificationException(
"Argument flag can only be one character long", toString() ) );
if ( _name != ignoreNameString() &&
( _flag == Arg::flagStartString() ||
_flag == Arg::nameStartString() ||
_flag == " " ) )
throw(SpecificationException("Argument flag cannot be either '" +
Arg::flagStartString() + "' or '" +
Arg::nameStartString() + "' or a space.",
toString() ) );
if ( ( _name.substr( 0, Arg::flagStartString().length() ) == Arg::flagStartString() ) ||
( _name.substr( 0, Arg::nameStartString().length() ) == Arg::nameStartString() ) ||
( _name.find( " ", 0 ) != std::string::npos ) )
throw(SpecificationException("Argument name begin with either '" +
Arg::flagStartString() + "' or '" +
Arg::nameStartString() + "' or space.",
toString() ) );
}
inline Arg::~Arg() { }
inline std::string Arg::shortID( const std::string& valueId ) const
{
std::string id = "";
if ( _flag != "" )
id = Arg::flagStartString() + _flag;
else
id = Arg::nameStartString() + _name;
if ( _valueRequired )
id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
if ( !_required )
id = "[" + id + "]";
return id;
}
inline std::string Arg::longID( const std::string& valueId ) const
{
std::string id = "";
if ( _flag != "" )
{
id += Arg::flagStartString() + _flag;
if ( _valueRequired )
id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
id += ", ";
}
id += Arg::nameStartString() + _name;
if ( _valueRequired )
id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
return id;
}
inline bool Arg::operator==(const Arg& a) const
{
if ( ( _flag != "" && _flag == a._flag ) || _name == a._name)
return true;
else
return false;
}
inline std::string Arg::getDescription() const
{
std::string desc = "";
if ( _required )
desc = "(" + _requireLabel + ") ";
// if ( _valueRequired )
// desc += "(value required) ";
desc += _description;
return desc;
}
inline const std::string& Arg::getFlag() const { return _flag; }
inline const std::string& Arg::getName() const { return _name; }
inline bool Arg::isRequired() const { return _required; }
inline bool Arg::isValueRequired() const { return _valueRequired; }
inline bool Arg::isSet() const
{
if ( _alreadySet && !_xorSet )
return true;
else
return false;
}
inline bool Arg::isIgnoreable() const { return _ignoreable; }
inline void Arg::setRequireLabel( const std::string& s)
{
_requireLabel = s;
}
inline bool Arg::argMatches( const std::string& argFlag ) const
{
if ( ( argFlag == Arg::flagStartString() + _flag && _flag != "" ) ||
argFlag == Arg::nameStartString() + _name )
return true;
else
return false;
}
inline std::string Arg::toString() const
{
std::string s = "";
if ( _flag != "" )
s += Arg::flagStartString() + _flag + " ";
s += "(" + Arg::nameStartString() + _name + ")";
return s;
}
inline void Arg::_checkWithVisitor() const
{
if ( _visitor != NULL )
_visitor->visit();
}
inline void Arg::trimFlag(std::string& flag, std::string& value) const
{
int stop = 0;
for ( int i = 0; static_cast<unsigned int>(i) < flag.length(); i++ )
if ( flag[i] == Arg::delimiter() )
{
stop = i;
break;
}
if ( stop > 1 )
{
value = flag.substr(stop+1);
flag = flag.substr(0,stop);
}
}
inline bool Arg::_hasBlanks( const std::string& s ) const
{
for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
if ( s[i] == Arg::blankChar() )
return true;
return false;
}
inline void Arg::forceRequired()
{
_required = true;
}
inline void Arg::xorSet()
{
_alreadySet = true;
_xorSet = true;
}
inline void Arg::addToList( std::list<Arg*>& argList ) const
{
argList.push_front( const_cast<Arg*>(this) );
}
inline bool Arg::allowMore()
{
return false;
}
inline bool Arg::acceptsMultipleValues()
{
return _acceptsMultipleValues;
}
inline void Arg::reset()
{
_xorSet = false;
_alreadySet = false;
}
//END Arg.cpp
} //namespace TCLAP
#endif
ArgException.h
ArgTraits.h
TCLAP_FLAGSTARTSTRING
#define TCLAP_FLAGSTARTSTRING
The sting that indicates the beginning of a flag.
Definition: Arg.h:225
TCLAP_FLAGSTARTCHAR
#define TCLAP_FLAGSTARTCHAR
The char that indicates the beginning of a flag.
Definition: Arg.h:215
TCLAP_NAMESTARTSTRING
#define TCLAP_NAMESTARTSTRING
The sting that indicates the beginning of a name.
Definition: Arg.h:234
CmdLineInterface.h
StandardTraits.h
Visitor.h
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::blankChar
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition: Arg.h:208
TCLAP::Arg::_checkWithVisitor
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:602
TCLAP::Arg::_acceptsMultipleValues
bool _acceptsMultipleValues
Definition: Arg.h:149
TCLAP::Arg::~Arg
virtual ~Arg()
Destructor.
Definition: Arg.h:494
TCLAP::Arg::isSet
bool isSet() const
Indicates whether the argument has already been set.
Definition: Arg.h:566
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::nameStartString
static const std::string nameStartString()
Definition: Arg.h:236
TCLAP::Arg::beginIgnoring
static void beginIgnoring()
Begin ignoring arguments since the "--" argument was specified.
Definition: Arg.h:191
TCLAP::Arg::_requireLabel
std::string _requireLabel
Label to be used in usage description.
Definition: Arg.h:114
TCLAP::Arg::_hasBlanks
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
Definition: Arg.h:632
TCLAP::Arg::ignoreRest
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:196
TCLAP::Arg::isIgnoreable
bool isIgnoreable() const
Indicates whether the argument can be ignored, if desired.
Definition: Arg.h:574
TCLAP::Arg::forceRequired
void forceRequired()
Sets _required to true.
Definition: Arg.h:641
TCLAP::Arg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)=0
Pure virtual method meant to handle the parsing and value assignment of the string on the command lin...
TCLAP::Arg::getFlag
const std::string & getFlag() const
Returns the argument flag.
Definition: Arg.h:558
TCLAP::Arg::ignoreNameString
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
Definition: Arg.h:241
TCLAP::Arg::_valueRequired
bool _valueRequired
Indicates whether a value is required for the argument.
Definition: Arg.h:121
TCLAP::Arg::isValueRequired
bool isValueRequired() const
Indicates whether a value must be specified for argument.
Definition: Arg.h:564
TCLAP::Arg::flagStartChar
static char flagStartChar()
Definition: Arg.h:217
TCLAP::Arg::_alreadySet
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:128
TCLAP::Arg::_ignoreable
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:141
TCLAP::Arg::_description
std::string _description
Description of the argument.
Definition: Arg.h:103
TCLAP::Arg::addToList
virtual void addToList(std::list< Arg * > &argList) const
Adds this to the specified list of Args.
Definition: Arg.h:655
TCLAP::Arg::allowMore
virtual bool allowMore()
Used for MultiArgs and XorHandler to determine whether args can still be set.
Definition: Arg.h:660
TCLAP::Arg::getName
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:560
TCLAP::Arg::_visitor
Visitor * _visitor
A pointer to a visitor object.
Definition: Arg.h:136
TCLAP::Arg::delimiter
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:202
TCLAP::Arg::setRequireLabel
void setRequireLabel(const std::string &s)
Sets the requireLabel.
Definition: Arg.h:576
TCLAP::Arg::getDescription
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:545
TCLAP::Arg::_xorSet
bool _xorSet
Indicates that the arg was set as part of an XOR and not on the command line.
Definition: Arg.h:147
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::_name
std::string _name
A single word namd identifying the argument.
Definition: Arg.h:98
TCLAP::Arg::argMatches
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:581
TCLAP::Arg::setDelimiter
static void setDelimiter(char c)
Sets the delimiter for all arguments.
Definition: Arg.h:247
TCLAP::Arg::_required
bool _required
Indicating whether the argument is required.
Definition: Arg.h:108
TCLAP::Arg::acceptsMultipleValues
virtual bool acceptsMultipleValues()
Use by output classes to determine whether an Arg accepts multiple values.
Definition: Arg.h:665
TCLAP::Arg::trimFlag
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition: Arg.h:611
TCLAP::Arg::toString
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:590
TCLAP::Arg::_flag
std::string _flag
The single char flag used to identify the argument.
Definition: Arg.h:89
TCLAP::Arg::xorSet
void xorSet()
Sets the _alreadySet value to true.
Definition: Arg.h:646
TCLAP::Arg::isRequired
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: Arg.h:562
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::Arg::operator==
virtual bool operator==(const Arg &a) const
Operator ==.
Definition: Arg.h:537
TCLAP::Arg::flagStartString
static const std::string flagStartString()
Definition: Arg.h:227
TCLAP::SpecificationException
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
Definition: ArgException.h:169
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:35
TCLAP::Visitor::visit
virtual void visit()=0
This method (to implemented by children) will be called when the visitor is visited.
TCLAP
Definition: Arg.h:48
TCLAP::ArgVectorIterator
std::vector< Arg * >::const_iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:392
TCLAP::istringstream
std::istringstream istringstream
Definition: sstream.h:37
TCLAP::ArgListIterator
std::list< Arg * >::const_iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: Arg.h:387
TCLAP::VisitorListIterator
std::list< Visitor * >::const_iterator VisitorListIterator
Typedef of a Visitor list iterator.
Definition: Arg.h:397
TCLAP::SetString
void SetString(T &dst, const std::string &src)
Definition: StandardTraits.h:55
TCLAP::ExtractValue
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:406
sstream.h
TCLAP::StringLike
A string like argument value type is a value that can be set using operator=(string).
Definition: ArgTraits.h:49
TCLAP::ValueLike
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:39