Go to the documentation of this file.
24 #ifndef TCLAP_CMDLINE_H
25 #define TCLAP_CMDLINE_H
53 template<
typename T>
void DelPtr(T ptr)
60 typedef typename C::value_type value_type;
61 std::for_each(c.begin(), c.end(), DelPtr<value_type>);
70 class CmdLine :
public CmdLineInterface
184 bool _helpAndVersion;
200 CmdLine(
const std::string& message,
201 const char delimiter =
' ',
202 const std::string& version =
"none",
203 bool helpAndVersion =
true);
235 void xorAdd( std::vector<Arg*>& xors );
242 void parse(
int argc,
const char *
const * argv);
249 void parse(std::vector<std::string>& args);
323 inline CmdLine::CmdLine(
const std::string& m,
325 const std::string& v,
328 _argList(std::list<
Arg*>()),
329 _progName(
"not_set_yet"),
335 _argDeleteOnExitList(std::list<
Arg*>()),
336 _visitorDeleteOnExitList(std::list<
Visitor*>()),
338 _handleExceptions(true),
339 _userSetOutput(false),
340 _helpAndVersion(help)
350 if ( !_userSetOutput ) {
356 inline void CmdLine::_constructor()
364 if ( _helpAndVersion )
368 "Displays usage information and exits.",
376 "Displays version information and exits.",
386 "Ignores the rest of the labeled arguments following this flag.",
399 (*it)->forceRequired();
400 (*it)->setRequireLabel(
"OR required" );
407 std::vector<Arg*> ors;
423 "Argument with same flag/name already exists!",
437 std::vector<std::string> args;
438 for (
int i = 0; i < argc; i++)
439 args.push_back(argv[i]);
446 bool shouldExit =
false;
451 args.erase(args.begin());
453 int requiredCount = 0;
455 for (
int i = 0;
static_cast<unsigned int>(i) < args.size(); i++)
457 bool matched =
false;
460 if ( (*it)->processArg( &i, args ) )
474 throw(CmdLineParseException(
"Couldn't find match "
483 throw(CmdLineParseException(
"Too many arguments!"));
485 }
catch ( ArgException& e ) {
493 }
catch ( ExitException &ee ) {
494 estat = ee.getExitStatus();
497 }
catch (ExitException &ee) {
503 estat = ee.getExitStatus();
516 for (
int i = 1;
static_cast<unsigned int>(i) < s.length(); i++ )
527 std::string missingArgList;
530 if ( (*it)->isRequired() && !(*it)->isSet() )
532 missingArgList += (*it)->getName();
533 missingArgList +=
", ";
537 missingArgList = missingArgList.substr(0,missingArgList.length()-2);
541 msg =
"Required arguments missing: ";
543 msg =
"Required argument missing: ";
545 msg += missingArgList;
567 if ( !_userSetOutput )
569 _userSetOutput =
true;
605 return _helpAndVersion;
virtual ~CmdLine()
Deletes any resources allocated by a CmdLine object.
A simple switch argument.
bool _emptyCombined(const std::string &s)
Checks whether a name/flag string matches entirely matches the Arg::blankChar.
bool getExceptionHandling() const
Returns the current state of the internal exception handling.
int _numRequired
The number of arguments that are required to be present on the command line.
std::list< Arg * > _argDeleteOnExitList
A list of Args to be explicitly deleted when the destructor is called.
The base class that manages the command line definition and passes along the parsing to the appropria...
CmdLineOutput * getOutput()
Returns the CmdLineOutput object.
A Visitor object that calls the usage method of the given CmdLineOutput object for the specified CmdL...
void parse(int argc, const char *const *argv)
Parses the command line.
std::list< Arg * >::iterator ArgListIterator
Typedef of an Arg list iterator.
XorHandler _xorHandler
The handler that manages xoring lists of args.
static const std::string flagStartString()
void setExceptionHandling(const bool state)
Disables or enables CmdLine's internal parsing exception handling.
Thrown from CmdLine when the arguments on the command line are not properly specified,...
std::string & getMessage()
Returns the message string.
void ClearContainer(C &c)
bool hasHelpAndVersion()
Indicates whether or not the help and version switches were created automatically.
static bool ignoreRest()
Whether to ignore the rest.
void add(Arg &a)
Adds an argument to the list of arguments to be parsed.
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
void missingArgsException()
Throws an exception listing the missing args.
std::string & getVersion()
Returns the version string.
void setOutput(CmdLineOutput *co)
A Visitor that tells the CmdLine to begin ignoring arguments after this one is parsed.
char getDelimiter()
Returns the delimiter string.
std::list< Arg * > & getArgList()
Returns the argList.
static void setDelimiter(char c)
Sets the delimiter for all arguments.
bool _handleExceptions
Should CmdLine handle parsing exceptions internally?
The interface that any output object must implement.
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
XorHandler & getXorHandler()
Returns the XorHandler.
CmdLineOutput * _output
Object that handles all output for the CmdLine.
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
std::string _message
A message used to describe the program.
virtual void addToList(std::list< Arg * > &argList) const
Adds this to the specified list of Args.
std::list< Arg * > _argList
The list of arguments that will be tested against the command line.
virtual void failure(CmdLineInterface &c, ArgException &e)=0
Generates some sort of output for a failure.
std::list< Visitor * > _visitorDeleteOnExitList
A list of Visitors to be explicitly deleted when the destructor is called.
std::string _progName
The name of the program.
This class handles lists of Arg's that are to be XOR'd on the command line.
void xorAdd(Arg &a, Arg &b)
Add two Args that will be xor'd.
void add(std::vector< Arg * > &ors)
Add a list of Arg*'s that will be xor'd together.
static char flagStartChar()
A base class that defines the interface for visitors.
std::string _version
The version to be displayed with the –version switch.
void deleteOnExit(Arg *ptr)
Perform a delete ptr; operation on ptr when this object is deleted.
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
std::string & getProgramName()
Returns the program name string.
A Visitor that will call the version method of the given CmdLineOutput for the specified CmdLine obje...
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
int check(const Arg *a)
Checks whether the specified Arg is in one of the xor lists and if it does match one,...
char _delimiter
The character that is used to separate the argument flag/name from the value.
void reset()
Allows the CmdLine object to be reused.
virtual bool isRequired() const
Indicates whether the argument is required.
A virtual base class that defines the essential data for all arguments.