28#ifdef HAVE_TCLAP_CONFIG_H
29#include <tclap/TCLAPConfig.h>
34#ifndef TCLAP_ALLOW_NUL_TRUNCATION
35#define TCLAP_ALLOW_NUL_TRUNCATION 0
61 :
flag(std::move(f)),
name(std::move(n)) {}
80 const char *_parserDelimiter;
81 std::vector<ArgAlias> _aliases;
83 void _validateAliases(
char delimiter,
const std::string &flagPrefix,
84 const std::string &namePrefix)
const;
86 const std::string &flagPrefix,
87 const std::string &namePrefix)
const;
88 void _validateAliasToken(
const std::string &label,
89 const std::string &prefix,
90 std::vector<std::string> &tokens)
const;
91 void _validateAliasLabels(
const std::string &flag,
92 const std::string &name)
const;
93 bool _matchesDeclaredToken(
const std::string &token,
94 const std::string &flagPrefix,
95 const std::string &namePrefix)
const;
96 bool _aliasConflictsWith(
const Arg &other,
const std::string &flagPrefix,
97 const std::string &namePrefix)
const;
103 static char &delimiterRef() {
104 static char delim =
' ';
203 Arg(std::string flag, std::string name, std::string desc,
bool req,
204 bool valreq,
Visitor *v =
nullptr);
214 Arg &
addAlias(std::string flag, std::string name = {});
217 [[nodiscard]]
const std::vector<ArgAlias> &
aliases() const noexcept {
return _aliases; }
228 virtual void addToList(std::list<Arg *> &argList)
const;
240 return _parserDelimiter ? *_parserDelimiter :
delimiter();
247 static constexpr char blankChar() noexcept {
return '\a'; }
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 [[nodiscard]]
const std::string &
getFlag() const noexcept;
323 [[nodiscard]] const std::
string &
getName() const noexcept;
350 [[nodiscard]]
virtual bool isRequired()
const;
361 [[nodiscard]]
bool isSet() const noexcept;
366 [[nodiscard]] const std::
string &
setBy() const noexcept {
return _setBy; }
381 [[nodiscard]] virtual
bool argMatches(const std::
string &s) const;
387 [[nodiscard]] virtual std::
string toString() const;
393 [[nodiscard]] virtual std::
string shortID(
394 const std::
string &valueId = "val") const;
400 [[nodiscard]] virtual std::
string longID(
401 const std::
string &valueId = "val") const;
410 virtual
void trimFlag(std::
string &flag, std::
string &value) const;
418 [[nodiscard]]
bool _hasBlanks(const std::
string &s) const;
436 virtual
void reset();
449 [[nodiscard]]
virtual bool hasLabel()
const {
return true; }
484 std::same_as<typename ArgTraits<T>::ValueCategory, StringLike>;
495 static_cast<void>(vl);
496#if !TCLAP_ALLOW_NUL_TRUNCATION
497 if (strVal.find(
'\0') != std::string::npos)
505 if (strVal.empty()) {
510 if (is.peek() != EOF)
511#ifdef TCLAP_SETBASE_ZERO
512 is >> std::setbase(0) >> destVal;
543 static_cast<void>(sl);
551inline Arg::Arg(std::string flag, std::string name, std::string desc,
552 bool req,
bool valreq,
Visitor *v)
553 : _parserDelimiter(nullptr),
555 _flag(std::move(flag)),
556 _name(std::move(name)),
557 _description(std::move(desc)),
559 _requireLabel(
"required"),
560 _valueRequired(valreq),
565 _acceptsMultipleValues(false),
566 _visibleInHelp(true) {
567 if (
_flag.length() > 1)
569 "Argument flag can only be one character long",
Arg::toString()));
583 (
_name.find(
" ", 0) != std::string::npos))
606 if (!
_flag.empty()) {
618 for (
const auto &alias : _aliases) {
619 if (!alias.flag.empty()) {
623 if (!alias.name.empty()) {
631inline bool Arg::_matchesDeclaredToken(
const std::string &token,
632 const std::string &flagPrefix,
633 const std::string &namePrefix)
const {
634 if ((!
_flag.empty() && token == flagPrefix +
_flag) ||
635 token == namePrefix +
_name)
637 for (
const auto &alias : _aliases)
638 if ((!alias.flag.empty() && token == flagPrefix + alias.flag) ||
639 (!alias.name.empty() && token == namePrefix + alias.name))
644inline bool Arg::_aliasConflictsWith(
const Arg &other,
645 const std::string &flagPrefix,
646 const std::string &namePrefix)
const {
647 for (
const auto &alias : _aliases)
648 if ((!alias.flag.empty() &&
649 other._matchesDeclaredToken(flagPrefix + alias.flag, flagPrefix,
651 (!alias.name.empty() &&
652 other._matchesDeclaredToken(namePrefix + alias.name, flagPrefix,
658inline void Arg::_validateAliases(
char delimiter,
const std::string &flagPrefix,
659 const std::string &namePrefix)
const {
660 if (_aliases.empty())
return;
661 std::vector<std::string> tokens;
662 if (!
_flag.empty()) tokens.push_back(flagPrefix +
_flag);
663 tokens.push_back(namePrefix +
_name);
664 for (
const auto &alias : _aliases) {
665 if (_aliasConflictsWithDialect(alias,
delimiter, flagPrefix,
667 throw SpecificationException(
"Alias conflicts with parser dialect",
669 _validateAliasToken(alias.flag, flagPrefix, tokens);
670 _validateAliasToken(alias.name, namePrefix, tokens);
675 if (flag.empty())
return false;
676 if (flag ==
_flag)
return true;
677 for (
const auto &alias : _aliases)
678 if (flag == alias.flag)
return true;
683 if (name.empty())
return false;
684 if (name ==
_name)
return true;
685 for (
const auto &alias : _aliases)
686 if (name == alias.name)
return true;
691 if (_parserDelimiter)
695 _validateAliasLabels(flag, name);
698 _aliases.emplace_back(std::move(flag), std::move(name));
702inline bool Arg::_aliasConflictsWithDialect(
703 const ArgAlias &alias,
char delimiter,
const std::string &flagPrefix,
704 const std::string &namePrefix)
const {
705 return (!alias.
flag.empty() &&
706 (alias.
flag == flagPrefix || alias.
flag == namePrefix)) ||
707 (!alias.
name.empty() &&
708 ((!flagPrefix.empty() && alias.
name.find(flagPrefix) == 0) ||
709 (!namePrefix.empty() && alias.
name.find(namePrefix) == 0))) ||
715inline void Arg::_validateAliasToken(
const std::string &label,
716 const std::string &prefix,
717 std::vector<std::string> &tokens)
const {
718 if (label.empty())
return;
719 const std::string token = prefix + label;
720 for (
const auto &existing : tokens)
721 if (existing == token)
722 throw SpecificationException(
"Duplicate argument alias token",
724 tokens.push_back(token);
727inline void Arg::_validateAliasLabels(
const std::string &flag,
728 const std::string &name)
const {
729 if ((flag.empty() && name.empty()) || flag.size() > 1 ||
731 flag.find_first_of(
" \t\r\n\v\f\a") != std::string::npos ||
732 flag.find(
'\0') != std::string::npos ||
733 name.find_first_of(
" \t\r\n\v\f") != std::string::npos ||
734 name.find(
'\0') != std::string::npos ||
737 throw SpecificationException(
"Invalid argument alias",
toString());
738 const std::string labels = flag + name;
739 for (
unsigned char c : labels)
740 if (c < 0x20 || c ==
'\x7f')
741 throw SpecificationException(
"Control byte in argument alias",
746 if (
this == &a || _name == a._name || matchesLongName(a._name) || matchesShortFlag(a._flag))
748 for (
const auto &alias : a._aliases)
749 if (matchesShortFlag(alias.
flag) || matchesLongName(alias.
name))
return true;
772 return argFlag == longPrefix +
_name ||
773 (argFlag.find(shortPrefix) == 0 &&
775 (argFlag.find(longPrefix) == 0 &&
797 std::string::size_type stop = flag.find(
getDelimiter());
799 if (stop != std::string::npos && stop > 1) {
800 value = flag.substr(stop + 1);
801 flag = flag.substr(0, stop);
816 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 std::string flagStartString()
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
bool _acceptsMultipleValues
bool isValueRequired() const noexcept
Indicates whether a value must be specified for argument.
bool _visibleInHelp
Indicates if the argument is visible in the help output (e.g., when specifying –help).
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
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
virtual ~Arg()=default
Destructor.
bool conflictsWith(const Arg &a) const noexcept
Checks declared registration conflicts: the same object, the same name, or the same nonempty short fl...
static constexpr char blankChar() noexcept
The char used as a place holder when SwitchArgs are combined.
static constexpr char flagStartChar() noexcept
bool isIgnoreable() const noexcept
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...
Arg(const Arg &rhs)=delete
Prevent accidental copying.
bool _valueRequired
Indicates whether a value is required for the argument.
bool _alreadySet
Indicates whether the argument has been set.
const std::string & setBy() const noexcept
Returns the value specified to set this flag (like -a or –all).
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.
Arg & addAlias(std::string flag, std::string name={})
Add equivalent spellings before registration.
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.
const std::string & getName() const noexcept
Returns the argument name.
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
static std::string nameStartString()
virtual std::string toString() const
Returns a simple string representation of the argument.
bool isSet() const noexcept
Indicates whether the argument has already been set.
char getDelimiter() const
Returns this argument's parser delimiter, or the legacy default.
static std::string ignoreNameString()
The name used to identify the ignore rest argument.
const std::string & getFlag() const noexcept
Returns the argument flag.
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.
const std::vector< ArgAlias > & aliases() const noexcept
Alias spellings in insertion order; no prefixes are stored.
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 ==.
Arg & operator=(const Arg &rhs)=delete
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::istringstream istringstream
std::vector< Arg * >::const_iterator ArgVectorIterator
Typedef of an Arg vector iterator.
std::list< Visitor * >::const_iterator VisitorListIterator
Typedef of a Visitor list iterator.
void SetString(T &dst, const std::string &src)
std::list< Arg * >::const_iterator ArgListIterator
Typedef of an Arg list iterator.
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
An additional short flag and/or long name for the same argument.
ArgAlias(std::string f, 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>>.