25#ifndef TCLAP_CMD_LINE_H
26#define TCLAP_CMD_LINE_H
68 "Argument with same flag/name already exists!",
166 virtual std::string
translate(
const std::string &messageId,
167 const std::string &fallback)
const = 0;
170 template <
typename T>
174 virtual std::string
translate(
const std::string &messageId,
175 const std::string &fallback)
const {
216 void _createIgnoreRestArgument();
217 void _createHelpAndVersionArguments();
218 void _refreshBuiltinDescriptions();
220 void _setProgramName(std::vector<std::string> &args);
221 int _parseArguments(std::vector<std::string> &args);
222 bool _processArgument(
int &index, std::vector<std::string> &args,
224 void _validateRequiredArguments(
int requiredCount);
229 bool _helpAndVersion;
234 bool _ignoreUnmatched;
254 CmdLine(
const std::string &message,
const char delimiter =
' ',
255 const std::string &version =
"none",
bool helpAndVersion =
true);
301 void xorAdd(
const std::vector<Arg *> &xors);
308 void parse(
int argc,
const char *
const *argv);
315 void parse(std::vector<std::string> &args);
366 template <
typename T>
370 _refreshBuiltinDescriptions();
377 const std::string &fallback)
const {
392inline CmdLine::CmdLine(
const std::string &m,
char delim,
const std::string &v,
398 _progName(
"not_set_yet"),
403 _hasOptionalUnlabeledArg(false),
404 _registrationActive(false),
407 _output(&_defaultOutput),
408 _handleExceptions(true),
409 _messageTranslator(NULL),
413 _helpAndVersion(help),
414 _ignoreUnmatched(false),
419inline void CmdLine::_constructor() {
422 _createIgnoreRestArgument();
423 if (_helpAndVersion) _createHelpAndVersionArguments();
426inline void CmdLine::_createIgnoreRestArgument() {
427 Visitor *v =
new IgnoreRestVisitor(*
this);
428 SwitchArg *ignore =
new SwitchArg(
431 "Ignores the rest of the labeled arguments following this flag."),
440inline void CmdLine::_createHelpAndVersionArguments() {
441 Visitor *v =
new HelpVisitor(
this, &
_output);
442 SwitchArg *help =
new SwitchArg(
445 "Displays usage information and exits."),
451 v =
new VersionVisitor(
this, &
_output);
452 SwitchArg *vers =
new SwitchArg(
455 "Displays version information and exits."),
468inline void CmdLine::_refreshBuiltinDescriptions() {
471 "ignore_rest_description",
472 "Ignores the rest of the labeled arguments following this flag."));
477 "Displays usage information and exits."));
481 "version_description",
482 "Displays version information and exits."));
490 for (std::vector<Arg *>::const_iterator it = args.begin(); it != args.end();
497 std::vector<Arg *> ors;
508 : _cmd(cmd), _args(cmd.
_argList), _numRequired(cmd._numRequired),
509 _hasOptionalUnlabeledArg(cmd._hasOptionalUnlabeledArg), _bindings() {
516 for (Bindings::reverse_iterator it = _bindings.rbegin();
517 it != _bindings.rend(); ++it)
518 it->first->_parserDelimiter = it->second;
525 validateConflicts(arg);
526 validatePositionalOrder(arg);
527 _bindings.push_back(std::make_pair(&arg, arg._parserDelimiter));
541 void validateConflicts(
const Arg &arg)
const {
549 "Argument with same flag/name already exists!", arg.
longID());
552 void validatePositionalOrder(
Arg &arg) {
554 if (_hasOptionalUnlabeledArg)
556 "You can't specify ANY Unlabeled Arg following an optional "
557 "Unlabeled Arg", arg.
longID());
559 _hasOptionalUnlabeledArg =
true;
565 typedef std::vector<std::pair<Arg *, const char *> > Bindings;
567 std::list<Arg *> _args;
569 bool _hasOptionalUnlabeledArg;
577 groups.push_back(&group);
580 registration.
add(**it);
591 registration.
add(*a);
597 members.push_back(a);
599 registration.
add(*a);
608 std::vector<std::string> args;
609 for (
int i = 0; i < argc; i++) args.push_back(argv[i]);
615 bool shouldExit =
false;
619 _setProgramName(args);
620 const int requiredCount = _parseArguments(args);
621 _validateRequiredArguments(requiredCount);
644 if (shouldExit) exit(estat);
647inline void CmdLine::_setProgramName(std::vector<std::string> &args) {
651 "The args vector must not be empty, "
652 "the first entry should contain the "
658 args.erase(args.begin());
661inline int CmdLine::_parseArguments(std::vector<std::string> &args) {
662 int requiredCount = 0;
663 for (
int i = 0;
static_cast<unsigned int>(i) < args.size(); i++) {
664 bool matched = _processArgument(i, args, requiredCount);
670 if (!matched && !
ignoreRest() && !_ignoreUnmatched)
672 CmdLineParseException(
"Couldn't find match "
677 return requiredCount;
680inline bool CmdLine::_processArgument(
int &index,
681 std::vector<std::string> &args,
682 int &requiredCount) {
686 bool alreadySet = arg.
isSet();
687 bool ignore = arg.isIgnoreable() &&
ignoreRest();
688 if (!ignore && arg.processArg(&index, args)) {
689 requiredCount += (!alreadySet && arg.isRequired()) ? 1 : 0;
697inline void CmdLine::_validateRequiredArguments(
int requiredCount) {
698 std::list<ArgGroup *> missingArgGroups;
701 for (std::list<ArgGroup *>::iterator it =
_argGroups.begin();
703 bool missingRequired = (*it)->validate();
704 if (missingRequired) {
705 missingArgGroups.push_back(*it);
709 if (requiredCount <
_numRequired || !missingArgGroups.empty()) {
714 throw(CmdLineParseException(
722 for (
int i = 1;
static_cast<unsigned int>(i) < s.length(); i++)
729 const std::list<ArgGroup *> &missing) {
732 std::string missingArgList;
734 if ((*it)->isRequired() && !(*it)->isSet()) {
735 missingArgList += (*it)->getName();
736 missingArgList +=
", ";
741 for (std::list<ArgGroup *>::const_iterator it = missing.begin();
742 it != missing.end(); it++) {
743 missingArgList += (*it)->getName();
744 missingArgList +=
", ";
748 missingArgList = missingArgList.substr(0, missingArgList.length() - 2);
753 "Required arguments missing: ");
756 "Required argument missing: ");
758 msg += missingArgList;
778 _ignoreUnmatched = ignore;
Groups arguments for presentation without imposing a selection rule.
Interface that allows adding an Arg to a "container".
A simple class that defines and argument exception.
ArgGroup is the base class for implementing groups of arguments that are mutually exclusive (it repla...
CmdLineInterface * _parser
std::list< Arg * > Container
Container::iterator iterator
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.
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()
bool conflictsWith(const Arg &a) const
Checks declared registration conflicts: the same object, the same name, or the same nonempty short fl...
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
static char flagStartChar()
virtual void addToList(std::list< Arg * > &argList) const
Adds this to the specified list of Args.
virtual bool acceptsMultipleValues()
Use by output classes to determine whether an Arg accepts multiple values.
virtual bool hasLabel() const
virtual bool isRequired() const
Indicates whether the argument is required.
virtual void setDescription(const std::string &desc)
Updates the argument description.
static const std::string flagStartString()
The base class that manages the command line definition and passes along the parsing to the appropria...
The interface that any output object must implement.
virtual void failure(CmdLineInterface &c, ArgException &e)=0
Generates some sort of output for a failure.
Thrown from CmdLine when the arguments on the command line are not properly specified,...
Registration(CmdLine &cmd)
The base class that manages the command line definition and passes along the parsing to the appropria...
MessageTranslator * _messageTranslator
std::list< Arg * > _argList
The list of arguments that will be tested against the command line.
void setMessageTranslator(const T &translator)
Sets an optional message translator callback.
SwitchArg * _ignoreArg
Built-in arguments whose descriptions may need refreshing when the translator changes.
bool _emptyCombined(const std::string &s)
Checks whether a name/flag string matches entirely matches the Arg::blankChar.
void reset()
Allows the CmdLine object to be reused.
int _numRequired
The number of arguments that are required to be present on the command line.
std::string _version
The version to be displayed with the –version switch.
bool _handleExceptions
Should CmdLine handle parsing exceptions internally?
std::string _message
A message used to describe the program.
void setOutput(CmdLineOutput *co)
StandaloneArgs _standaloneArgs
char getDelimiter() const
Returns the delimiter string.
CmdLineOutput * _output
Object that handles all output for the CmdLine.
char _delimiter
The character that is used to separate the argument flag/name from the value.
void ignoreUnmatched(const bool ignore)
Allows unmatched args to be ignored.
void beginIgnoring()
Begin ignoring arguments since the "--" argument was specified.
std::list< ArgGroup * > _argGroups
Some args have set constraints on them (i.e., exactly or at most one must be specified.
DeferDelete _deleteOnExit
Add pointers that should be deleted as part of cleanup when this object is destroyed.
std::string _progName
The name of the program.
void addToArgList(Arg *a)
bool hasExceptionHandling() const
Returns the current state of the internal exception handling.
void setExceptionHandling(const bool state)
Disables or enables CmdLine's internal parsing exception handling.
std::list< Arg * > getArgList() const
std::string getMessage() const
Returns the message string.
StdOutput _defaultOutput
Default output handler if nothing is specified.
bool attachGroup(ArgGroup &group)
bool hasHelpAndVersion() const
Indicates whether or not the help and version switches were created automatically.
std::string translateMessage(const std::string &messageId, const std::string &fallback) const
Translates a built-in message.
bool ignoreRest()
Whether to ignore the rest.
void missingArgsException(const std::list< ArgGroup * > &missing)
Throws an exception listing the missing args.
virtual ~CmdLine()
Deletes any resources allocated by a CmdLine object.
void parse(int argc, const char *const *argv)
Parses the command line.
std::string getVersion() const
Returns the version string.
std::string getProgramName() const
Returns the program name string.
std::list< ArgGroup * > getArgGroups()
Returns the list of ArgGroups.
ArgContainer & add(Arg &a)
Adds an argument to the list of arguments to be parsed.
bool _hasOptionalUnlabeledArg
void xorAdd(Arg &a, Arg &b)
DeferDelete can be used by objects that need to allocate arbitrary other objects to live for the dura...
ArgContainer & add(Arg &arg)
Add an argument to this arg group.
Thrown when TCLAP thinks the program should exit.
int getExitStatus() const
Implements a group of arguments where exactly one must be selected.
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
bool showAsGroup() const
If arguments in this group should show up as grouped in help.
ArgContainer & add(Arg *arg)
Add an argument to this arg group.
A class that isolates any output from the CmdLine object so that it may be easily modified.
A simple switch argument.
std::list< Arg * >::const_iterator ArgListIterator
Typedef of an Arg list iterator.
std::string basename(std::string s)
virtual std::string translate(const std::string &messageId, const std::string &fallback) const
MessageTranslatorImpl(const T &translator)
Optional callback used to localize user-facing strings.
virtual ~MessageTranslator()
virtual std::string translate(const std::string &messageId, const std::string &fallback) const =0