28#ifdef HAVE_TCLAP_CONFIG_H
29#include <tclap/TCLAPConfig.h>
34#ifndef TCLAP_ALLOW_NUL_TRUNCATION
35#define TCLAP_ALLOW_NUL_TRUNCATION 0
71 const char *_parserDelimiter;
72 std::vector<ArgAlias> _aliases;
74 void _validateAliases(
char delimiter,
const std::string &flagPrefix,
75 const std::string &namePrefix)
const;
77 const std::string &flagPrefix,
78 const std::string &namePrefix)
const;
79 void _validateAliasToken(
const std::string &label,
const std::string &prefix,
80 std::vector<std::string> &tokens)
const;
81 void _validateAliasLabels(
const std::string &flag,
82 const std::string &name)
const;
83 bool _matchesDeclaredToken(
const std::string &token,
84 const std::string &flagPrefix,
85 const std::string &namePrefix)
const;
86 bool _aliasConflictsWith(
const Arg &other,
const std::string &flagPrefix,
87 const std::string &namePrefix)
const;
97 Arg &operator=(
const Arg &rhs);
103 static char &delimiterRef() {
104 static char delim =
' ';
203 Arg(
const std::string &flag,
const std::string &name,
204 const std::string &desc,
bool req,
bool valreq,
Visitor *v = NULL);
214 Arg &
addAlias(
const std::string &flag,
const std::string &name =
"");
217 const std::vector<ArgAlias> &
aliases()
const {
return _aliases; }
228 virtual void addToList(std::list<Arg *> &argList)
const;
240 return _parserDelimiter ? *_parserDelimiter :
delimiter();
253#ifndef TCLAP_FLAGSTARTCHAR
254#define TCLAP_FLAGSTARTCHAR '-'
263#ifndef TCLAP_FLAGSTARTSTRING
264#define TCLAP_FLAGSTARTSTRING "-"
272#ifndef TCLAP_NAMESTARTSTRING
273#define TCLAP_NAMESTARTSTRING "--"
296 virtual bool processArg(
int *i, std::vector<std::string> &args) = 0;
318 const std::string &
getFlag()
const;
323 const std::string &
getName()
const;
379 virtual bool argMatches(
const std::string &s)
const;
385 virtual std::string
toString()
const;
391 virtual std::string
shortID(
const std::string &valueId =
"val")
const;
397 virtual std::string
longID(
const std::string &valueId =
"val")
const;
406 virtual void trimFlag(std::string &flag, std::string &value)
const;
432 virtual void reset();
471 static_cast<void>(vl);
472#if !TCLAP_ALLOW_NUL_TRUNCATION
473 if (strVal.find(
'\0') != std::string::npos)
481 if (strVal.empty()) {
486 if (is.peek() != EOF)
487#ifdef TCLAP_SETBASE_ZERO
488 is >> std::setbase(0) >> destVal;
519 static_cast<void>(sl);
527inline Arg::Arg(
const std::string &flag,
const std::string &name,
528 const std::string &desc,
bool req,
bool valreq,
Visitor *v)
529 : _parserDelimiter(NULL),
535 _requireLabel(
"required"),
536 _valueRequired(valreq),
541 _acceptsMultipleValues(false),
542 _visibleInHelp(true) {
543 if (
_flag.length() > 1)
545 "Argument flag can only be one character long",
Arg::toString()));
559 (
_name.find(
" ", 0) != std::string::npos))
596 for (std::vector<ArgAlias>::const_iterator it = _aliases.begin();
597 it != _aliases.end(); ++it) {
598 if (!it->flag.empty()) {
602 if (!it->name.empty()) {
610inline bool Arg::_matchesDeclaredToken(
const std::string &token,
611 const std::string &flagPrefix,
612 const std::string &namePrefix)
const {
613 if ((!
_flag.empty() && token == flagPrefix +
_flag) ||
614 token == namePrefix +
_name)
616 for (std::vector<ArgAlias>::const_iterator it = _aliases.begin();
617 it != _aliases.end(); ++it)
618 if ((!it->flag.empty() && token == flagPrefix + it->flag) ||
619 (!it->name.empty() && token == namePrefix + it->name))
624inline bool Arg::_aliasConflictsWith(
const Arg &other,
625 const std::string &flagPrefix,
626 const std::string &namePrefix)
const {
627 for (std::vector<ArgAlias>::const_iterator it = _aliases.begin();
628 it != _aliases.end(); ++it)
629 if ((!it->flag.empty() &&
630 other._matchesDeclaredToken(flagPrefix + it->flag, flagPrefix,
632 (!it->name.empty() &&
633 other._matchesDeclaredToken(namePrefix + it->name, flagPrefix,
639inline void Arg::_validateAliases(
char delimiter,
const std::string &flagPrefix,
640 const std::string &namePrefix)
const {
641 if (_aliases.empty())
return;
642 std::vector<std::string> tokens;
643 if (!
_flag.empty()) tokens.push_back(flagPrefix +
_flag);
644 tokens.push_back(namePrefix +
_name);
645 for (std::vector<ArgAlias>::const_iterator it = _aliases.begin();
646 it != _aliases.end(); ++it) {
647 if (_aliasConflictsWithDialect(*it,
delimiter, flagPrefix, namePrefix))
648 throw SpecificationException(
"Alias conflicts with parser dialect",
650 _validateAliasToken(it->flag, flagPrefix, tokens);
651 _validateAliasToken(it->name, namePrefix, tokens);
655inline bool Arg::_aliasConflictsWithDialect(
656 const ArgAlias &alias,
char delimiter,
const std::string &flagPrefix,
657 const std::string &namePrefix)
const {
658 return (!alias.flag.empty() &&
659 (alias.flag == flagPrefix || alias.flag == namePrefix)) ||
660 (!alias.name.empty() &&
661 ((!flagPrefix.empty() && alias.name.find(flagPrefix) == 0) ||
662 (!namePrefix.empty() && alias.name.find(namePrefix) == 0))) ||
664 (alias.flag.find(
delimiter) != std::string::npos ||
665 alias.name.find(
delimiter) != std::string::npos));
668inline void Arg::_validateAliasToken(
const std::string &label,
669 const std::string &prefix,
670 std::vector<std::string> &tokens)
const {
671 if (label.empty())
return;
672 const std::string token = prefix + label;
673 for (std::vector<std::string>::const_iterator it = tokens.begin();
674 it != tokens.end(); ++it)
676 throw SpecificationException(
"Duplicate argument alias token",
678 tokens.push_back(token);
682 if (flag.empty())
return false;
683 if (flag ==
_flag)
return true;
684 for (std::vector<ArgAlias>::const_iterator it = _aliases.begin();
685 it != _aliases.end(); ++it)
686 if (flag == it->flag)
return true;
691 if (name.empty())
return false;
692 if (name ==
_name)
return true;
693 for (std::vector<ArgAlias>::const_iterator it = _aliases.begin();
694 it != _aliases.end(); ++it)
695 if (name == it->name)
return true;
700 if (_parserDelimiter)
704 _validateAliasLabels(flag, name);
707 _aliases.push_back(
ArgAlias(flag, name));
711inline void Arg::_validateAliasLabels(
const std::string &flag,
712 const std::string &name)
const {
713 if ((flag.empty() && name.empty()) || flag.size() > 1 ||
715 flag.find_first_of(
" \t\r\n\v\f\a") != std::string::npos ||
716 flag.find(
'\0') != std::string::npos ||
717 name.find_first_of(
" \t\r\n\v\f") != std::string::npos ||
718 name.find(
'\0') != std::string::npos ||
722 const std::string labels = flag + name;
723 for (std::string::const_iterator it = labels.begin(); it != labels.end();
725 if (
static_cast<unsigned char>(*it) < 0x20 || *it ==
'\x7f')
733 for (std::vector<ArgAlias>::const_iterator it = a._aliases.begin();
734 it != a._aliases.end(); ++it)
758 return argFlag == longPrefix +
_name ||
759 (argFlag.find(shortPrefix) == 0 &&
761 (argFlag.find(longPrefix) == 0 &&
784 for (
int i = 0;
static_cast<unsigned int>(i) < flag.length(); i++)
791 value = flag.substr(stop + 1);
792 flag = flag.substr(0, stop);
800 for (
int i = 1;
static_cast<unsigned int>(i) < s.length(); i++)
810 argList.push_front(
const_cast<Arg *
>(
this));
#define TCLAP_FLAGSTARTSTRING
The sting that indicates the beginning of a flag.
#define TCLAP_FLAGSTARTCHAR
The char that indicates the beginning of a flag.
#define TCLAP_NAMESTARTSTRING
The sting that indicates the beginning of a name.
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
A virtual base class that defines the essential data for all arguments.
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
bool _acceptsMultipleValues
const std::string & setBy() const
Returns the value specified to set this flag (like -a or –all).
Arg & addAlias(const std::string &flag, const std::string &name="")
Add equivalent spellings before registration.
virtual ~Arg()
Destructor.
bool _visibleInHelp
Indicates if the argument is visible in the help output (e.g., when specifying –help).
bool isSet() const
Indicates whether the argument has already been set.
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
static const std::string nameStartString()
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
std::string getDescription(bool required) const
Returns the argument description.
std::string _requireLabel
Label to be used in usage description.
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
bool matchesLongName(const std::string &name) const
bool conflictsWith(const Arg &a) const
Checks declared registration conflicts: the same object, the same name, or the same nonempty short fl...
bool isIgnoreable() const
Indicates whether the argument can be ignored, if desired.
const bool _required
Indicating whether the argument is required.
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...
const std::string & getFlag() const
Returns the argument flag.
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
bool _valueRequired
Indicates whether a value is required for the argument.
bool isValueRequired() const
Indicates whether a value must be specified for argument.
static char flagStartChar()
bool _alreadySet
Indicates whether the argument has been set.
bool _ignoreable
Whether this argument can be ignored, if desired.
std::string _description
Description of the argument.
virtual void addToList(std::list< Arg * > &argList) const
Adds this to the specified list of Args.
virtual bool allowMore()
Used for MultiArgs to determine whether args can still be set.
const std::string & getName() const
Returns the argument name.
Visitor * _visitor
A pointer to a visitor object.
virtual void hideFromHelp(bool hide=true)
Hide this argument from the help output (e.g., when specifying the –help flag or on error.
static char delimiter()
The delimiter that separates an argument flag/name from the value.
std::string getDescription() const
Returns the argument description.
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
bool matchesShortFlag(const std::string &flag) const
std::string _name
A single word namd identifying the argument.
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
static void setDelimiter(char c)
Sets the legacy delimiter for unregistered arguments and static callers.
virtual bool acceptsMultipleValues()
Use by output classes to determine whether an Arg accepts multiple values.
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
virtual std::string toString() const
Returns a simple string representation of the argument.
char getDelimiter() const
Returns this argument's parser delimiter, or the legacy default.
virtual bool hasLabel() const
std::string _flag
The single char flag used to identify the argument.
virtual bool visibleInHelp() const
Returns true if this Arg is visible in the help output.
virtual bool isRequired() const
Indicates whether the argument is required.
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
virtual void setDescription(const std::string &desc)
Updates the argument description.
virtual bool operator==(const Arg &a) const
Operator ==.
static const std::string flagStartString()
const std::vector< ArgAlias > & aliases() const
Alias spellings in insertion order; no prefixes are stored.
The base class that manages the command line definition and passes along the parsing to the appropria...
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
A base class that defines the interface for visitors.
virtual void visit()=0
This method (to implemented by children) will be called when the visitor is visited.
std::vector< Arg * >::const_iterator ArgVectorIterator
Typedef of an Arg vector iterator.
std::istringstream istringstream
std::list< Arg * >::const_iterator ArgListIterator
Typedef of an Arg list iterator.
std::list< Visitor * >::const_iterator VisitorListIterator
Typedef of a Visitor list iterator.
void SetString(T &dst, const std::string &src)
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
An additional short flag and/or long name for the same argument.
ArgAlias(const std::string &f, const std::string &n)
A string like argument value type is a value that can be set using operator=(string).
A value like argument value type is a value that can be set using operator>>.