CmdLine.h
Go to the documentation of this file.
// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
/******************************************************************************
*
* file: CmdLine.h
*
* Copyright (c) 2003, Michael E. Smoot .
* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
* 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_CMDLINE_H
#define TCLAP_CMDLINE_H
#include <tclap/SwitchArg.h>
#include <tclap/MultiSwitchArg.h>
#include <tclap/UnlabeledValueArg.h>
#include <tclap/UnlabeledMultiArg.h>
#include <tclap/XorHandler.h>
#include <tclap/HelpVisitor.h>
#include <tclap/VersionVisitor.h>
#include <tclap/IgnoreRestVisitor.h>
#include <tclap/CmdLineOutput.h>
#include <tclap/StdOutput.h>
#include <tclap/Constraint.h>
#include <tclap/ValuesConstraint.h>
#include <string>
#include <vector>
#include <list>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdlib.h> // Needed for exit(), which isn't defined in some envs.
namespace TCLAP {
template<typename T> void DelPtr(T ptr)
{
delete ptr;
}
template<typename C> void ClearContainer(C &c)
{
typedef typename C::value_type value_type;
std::for_each(c.begin(), c.end(), DelPtr<value_type>);
c.clear();
}
class CmdLine : public CmdLineInterface
{
protected:
std::list<Arg*> _argList;
std::string _progName;
std::string _message;
std::string _version;
int _numRequired;
char _delimiter;
XorHandler _xorHandler;
std::list<Arg*> _argDeleteOnExitList;
std::list<Visitor*> _visitorDeleteOnExitList;
CmdLineOutput* _output;
bool _handleExceptions;
void missingArgsException();
bool _emptyCombined(const std::string& s);
void deleteOnExit(Arg* ptr);
void deleteOnExit(Visitor* ptr);
private:
CmdLine(const CmdLine& rhs);
CmdLine& operator=(const CmdLine& rhs);
void _constructor();
bool _userSetOutput;
bool _helpAndVersion;
bool _ignoreUnmatched;
public:
CmdLine(const std::string& message,
const char delimiter = ' ',
const std::string& version = "none",
bool helpAndVersion = true);
virtual ~CmdLine();
void add( Arg& a );
void add( Arg* a );
void xorAdd( Arg& a, Arg& b );
void xorAdd( const std::vector<Arg*>& xors );
void parse(int argc, const char * const * argv);
void parse(std::vector<std::string>& args);
CmdLineOutput* getOutput();
void setOutput(CmdLineOutput* co);
std::string& getVersion();
std::string& getProgramName();
std::list<Arg*>& getArgList();
XorHandler& getXorHandler();
char getDelimiter();
std::string& getMessage();
bool hasHelpAndVersion();
void setExceptionHandling(const bool state);
bool getExceptionHandling() const;
void reset();
void ignoreUnmatched(const bool ignore);
};
//Begin CmdLine.cpp
inline CmdLine::CmdLine(const std::string& m,
char delim,
const std::string& v,
bool help )
:
_argList(std::list<Arg*>()),
_progName("not_set_yet"),
_message(m),
_version(v),
_numRequired(0),
_delimiter(delim),
_xorHandler(XorHandler()),
_argDeleteOnExitList(std::list<Arg*>()),
_visitorDeleteOnExitList(std::list<Visitor*>()),
_output(0),
_handleExceptions(true),
_userSetOutput(false),
_helpAndVersion(help),
_ignoreUnmatched(false)
{
_constructor();
}
inline CmdLine::~CmdLine()
{
ClearContainer(_argDeleteOnExitList);
ClearContainer(_visitorDeleteOnExitList);
if ( !_userSetOutput ) {
delete _output;
_output = 0;
}
}
inline void CmdLine::_constructor()
{
_output = new StdOutput;
Arg::setDelimiter( _delimiter );
Visitor* v;
if ( _helpAndVersion )
{
v = new HelpVisitor( this, &_output );
SwitchArg* help = new SwitchArg("h","help",
"Displays usage information and exits.",
false, v);
add( help );
deleteOnExit(help);
deleteOnExit(v);
v = new VersionVisitor( this, &_output );
SwitchArg* vers = new SwitchArg("","version",
"Displays version information and exits.",
false, v);
add( vers );
deleteOnExit(vers);
deleteOnExit(v);
}
v = new IgnoreRestVisitor();
SwitchArg* ignore = new SwitchArg(Arg::flagStartString(),
Arg::ignoreNameString(),
"Ignores the rest of the labeled arguments following this flag.",
false, v);
add( ignore );
deleteOnExit(ignore);
deleteOnExit(v);
}
inline void CmdLine::xorAdd( const std::vector<Arg*>& ors )
{
_xorHandler.add( ors );
for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++)
{
(*it)->forceRequired();
(*it)->setRequireLabel( "OR required" );
add( *it );
}
}
inline void CmdLine::xorAdd( Arg& a, Arg& b )
{
std::vector<Arg*> ors;
ors.push_back( &a );
ors.push_back( &b );
xorAdd( ors );
}
inline void CmdLine::add( Arg& a )
{
add( &a );
}
inline void CmdLine::add( Arg* a )
{
for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
if ( *a == *(*it) )
throw( SpecificationException(
"Argument with same flag/name already exists!",
a->longID() ) );
a->addToList( _argList );
if ( a->isRequired() )
_numRequired++;
}
inline void CmdLine::parse(int argc, const char * const * argv)
{
// this step is necessary so that we have easy access to
// mutable strings.
std::vector<std::string> args;
for (int i = 0; i < argc; i++)
args.push_back(argv[i]);
parse(args);
}
inline void CmdLine::parse(std::vector<std::string>& args)
{
bool shouldExit = false;
int estat = 0;
try {
if (args.empty()) {
// https://sourceforge.net/p/tclap/bugs/30/
throw CmdLineParseException("The args vector must not be empty, "
"the first entry should contain the "
"program's name.");
}
_progName = args.front();
args.erase(args.begin());
int requiredCount = 0;
for (int i = 0; static_cast<unsigned int>(i) < args.size(); i++)
{
bool matched = false;
for (ArgListIterator it = _argList.begin();
it != _argList.end(); it++) {
if ( (*it)->processArg( &i, args ) )
{
requiredCount += _xorHandler.check( *it );
matched = true;
break;
}
}
// checks to see if the argument is an empty combined
// switch and if so, then we've actually matched it
if ( !matched && _emptyCombined( args[i] ) )
matched = true;
if ( !matched && !Arg::ignoreRest() && !_ignoreUnmatched)
throw(CmdLineParseException("Couldn't find match "
"for argument",
args[i]));
}
if ( requiredCount < _numRequired )
missingArgsException();
if ( requiredCount > _numRequired )
throw(CmdLineParseException("Too many arguments!"));
} catch ( ArgException& e ) {
// If we're not handling the exceptions, rethrow.
if ( !_handleExceptions) {
throw;
}
try {
_output->failure(*this,e);
} catch ( ExitException &ee ) {
estat = ee.getExitStatus();
shouldExit = true;
}
} catch (ExitException &ee) {
// If we're not handling the exceptions, rethrow.
if ( !_handleExceptions) {
throw;
}
estat = ee.getExitStatus();
shouldExit = true;
}
if (shouldExit)
exit(estat);
}
inline bool CmdLine::_emptyCombined(const std::string& s)
{
if ( s.length() > 0 && s[0] != Arg::flagStartChar() )
return false;
for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
if ( s[i] != Arg::blankChar() )
return false;
return true;
}
inline void CmdLine::missingArgsException()
{
int count = 0;
std::string missingArgList;
for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++)
{
if ( (*it)->isRequired() && !(*it)->isSet() )
{
missingArgList += (*it)->getName();
missingArgList += ", ";
count++;
}
}
missingArgList = missingArgList.substr(0,missingArgList.length()-2);
std::string msg;
if ( count > 1 )
msg = "Required arguments missing: ";
else
msg = "Required argument missing: ";
msg += missingArgList;
throw(CmdLineParseException(msg));
}
inline void CmdLine::deleteOnExit(Arg* ptr)
{
_argDeleteOnExitList.push_back(ptr);
}
inline void CmdLine::deleteOnExit(Visitor* ptr)
{
_visitorDeleteOnExitList.push_back(ptr);
}
inline CmdLineOutput* CmdLine::getOutput()
{
return _output;
}
inline void CmdLine::setOutput(CmdLineOutput* co)
{
if ( !_userSetOutput )
delete _output;
_userSetOutput = true;
_output = co;
}
inline std::string& CmdLine::getVersion()
{
return _version;
}
inline std::string& CmdLine::getProgramName()
{
return _progName;
}
inline std::list<Arg*>& CmdLine::getArgList()
{
return _argList;
}
inline XorHandler& CmdLine::getXorHandler()
{
return _xorHandler;
}
inline char CmdLine::getDelimiter()
{
return _delimiter;
}
inline std::string& CmdLine::getMessage()
{
return _message;
}
inline bool CmdLine::hasHelpAndVersion()
{
return _helpAndVersion;
}
inline void CmdLine::setExceptionHandling(const bool state)
{
_handleExceptions = state;
}
inline bool CmdLine::getExceptionHandling() const
{
return _handleExceptions;
}
inline void CmdLine::reset()
{
for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
(*it)->reset();
_progName.clear();
}
inline void CmdLine::ignoreUnmatched(const bool ignore)
{
_ignoreUnmatched = ignore;
}
//End CmdLine.cpp
} //namespace TCLAP
#endif
CmdLineOutput.h
Constraint.h
HelpVisitor.h
IgnoreRestVisitor.h
MultiSwitchArg.h
StdOutput.h
SwitchArg.h
UnlabeledMultiArg.h
UnlabeledValueArg.h
ValuesConstraint.h
VersionVisitor.h
XorHandler.h
TCLAP::ArgException
A simple class that defines and argument exception.
Definition: ArgException.h:38
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::longID
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:514
TCLAP::Arg::ignoreRest
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:196
TCLAP::Arg::ignoreNameString
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
Definition: Arg.h:241
TCLAP::Arg::flagStartChar
static char flagStartChar()
Definition: Arg.h:217
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::setDelimiter
static void setDelimiter(char c)
Sets the delimiter for all arguments.
Definition: Arg.h:247
TCLAP::Arg::isRequired
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: Arg.h:562
TCLAP::Arg::flagStartString
static const std::string flagStartString()
Definition: Arg.h:227
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:47
TCLAP::CmdLineOutput
The interface that any output object must implement.
Definition: CmdLineOutput.h:45
TCLAP::CmdLineOutput::failure
virtual void failure(CmdLineInterface &c, ArgException &e)=0
Generates some sort of output for a failure.
TCLAP::CmdLineParseException
Thrown from CmdLine when the arguments on the command line are not properly specified,...
Definition: ArgException.h:145
TCLAP::CmdLine
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLine.h:71
TCLAP::CmdLine::_emptyCombined
bool _emptyCombined(const std::string &s)
Checks whether a name/flag string matches entirely matches the Arg::blankChar.
Definition: CmdLine.h:530
TCLAP::CmdLine::reset
void reset()
Allows the CmdLine object to be reused.
Definition: CmdLine.h:637
TCLAP::CmdLine::_numRequired
int _numRequired
The number of arguments that are required to be present on the command line.
Definition: CmdLine.h:100
TCLAP::CmdLine::_version
std::string _version
The version to be displayed with the –version switch.
Definition: CmdLine.h:93
TCLAP::CmdLine::_xorHandler
XorHandler _xorHandler
The handler that manages xoring lists of args.
Definition: CmdLine.h:111
TCLAP::CmdLine::_handleExceptions
bool _handleExceptions
Should CmdLine handle parsing exceptions internally?
Definition: CmdLine.h:135
TCLAP::CmdLine::getArgList
std::list< Arg * > & getArgList()
Returns the argList.
Definition: CmdLine.h:602
TCLAP::CmdLine::_message
std::string _message
A message used to describe the program.
Definition: CmdLine.h:88
TCLAP::CmdLine::getDelimiter
char getDelimiter()
Returns the delimiter string.
Definition: CmdLine.h:612
TCLAP::CmdLine::deleteOnExit
void deleteOnExit(Arg *ptr)
Perform a delete ptr; operation on ptr when this object is deleted.
Definition: CmdLine.h:569
TCLAP::CmdLine::setOutput
void setOutput(CmdLineOutput *co)
Definition: CmdLine.h:584
TCLAP::CmdLine::getProgramName
std::string & getProgramName()
Returns the program name string.
Definition: CmdLine.h:597
TCLAP::CmdLine::_argList
std::list< Arg * > _argList
The list of arguments that will be tested against the command line.
Definition: CmdLine.h:78
TCLAP::CmdLine::_output
CmdLineOutput * _output
Object that handles all output for the CmdLine.
Definition: CmdLine.h:130
TCLAP::CmdLine::hasHelpAndVersion
bool hasHelpAndVersion()
Indicates whether or not the help and version switches were created automatically.
Definition: CmdLine.h:622
TCLAP::CmdLine::_delimiter
char _delimiter
The character that is used to separate the argument flag/name from the value.
Definition: CmdLine.h:106
TCLAP::CmdLine::ignoreUnmatched
void ignoreUnmatched(const bool ignore)
Allows unmatched args to be ignored.
Definition: CmdLine.h:645
TCLAP::CmdLine::missingArgsException
void missingArgsException()
Throws an exception listing the missing args.
Definition: CmdLine.h:542
TCLAP::CmdLine::_visitorDeleteOnExitList
std::list< Visitor * > _visitorDeleteOnExitList
A list of Visitors to be explicitly deleted when the destructor is called.
Definition: CmdLine.h:125
TCLAP::CmdLine::getXorHandler
XorHandler & getXorHandler()
Returns the XorHandler.
Definition: CmdLine.h:607
TCLAP::CmdLine::getVersion
std::string & getVersion()
Returns the version string.
Definition: CmdLine.h:592
TCLAP::CmdLine::~CmdLine
virtual ~CmdLine()
Deletes any resources allocated by a CmdLine object.
Definition: CmdLine.h:358
TCLAP::CmdLine::_progName
std::string _progName
The name of the program.
Definition: CmdLine.h:83
TCLAP::CmdLine::getMessage
std::string & getMessage()
Returns the message string.
Definition: CmdLine.h:617
TCLAP::CmdLine::add
void add(Arg &a)
Adds an argument to the list of arguments to be parsed.
Definition: CmdLine.h:426
TCLAP::CmdLine::setExceptionHandling
void setExceptionHandling(const bool state)
Disables or enables CmdLine's internal parsing exception handling.
Definition: CmdLine.h:627
TCLAP::CmdLine::getExceptionHandling
bool getExceptionHandling() const
Returns the current state of the internal exception handling.
Definition: CmdLine.h:632
TCLAP::CmdLine::parse
void parse(int argc, const char *const *argv)
Parses the command line.
Definition: CmdLine.h:446
TCLAP::CmdLine::getOutput
CmdLineOutput * getOutput()
Returns the CmdLineOutput object.
Definition: CmdLine.h:579
TCLAP::CmdLine::_argDeleteOnExitList
std::list< Arg * > _argDeleteOnExitList
A list of Args to be explicitly deleted when the destructor is called.
Definition: CmdLine.h:118
TCLAP::CmdLine::xorAdd
void xorAdd(Arg &a, Arg &b)
Add two Args that will be xor'd.
Definition: CmdLine.h:418
TCLAP::ExitException
Thrown when TCLAP thinks the program should exit.
Definition: ArgException.h:200
TCLAP::ExitException::getExitStatus
int getExitStatus() const
Definition: ArgException.h:204
TCLAP::HelpVisitor
A Visitor object that calls the usage method of the given CmdLineOutput object for the specified CmdL...
Definition: HelpVisitor.h:38
TCLAP::SpecificationException
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
Definition: ArgException.h:169
TCLAP::StdOutput
A class that isolates any output from the CmdLine object so that it may be easily modified.
Definition: StdOutput.h:45
TCLAP::SwitchArg
A simple switch argument.
Definition: SwitchArg.h:42
TCLAP::VersionVisitor
A Visitor that will call the version method of the given CmdLineOutput for the specified CmdLine obje...
Definition: VersionVisitor.h:38
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:35
TCLAP::XorHandler
This class handles lists of Arg's that are to be XOR'd on the command line.
Definition: XorHandler.h:41
TCLAP::XorHandler::check
int check(const Arg *a)
Checks whether the specified Arg is in one of the xor lists and if it does match one,...
Definition: XorHandler.h:102
TCLAP::XorHandler::add
void add(const std::vector< Arg * > &ors)
Add a list of Arg*'s that will be xor'd together.
Definition: XorHandler.h:97
TCLAP
Definition: Arg.h:48
TCLAP::ArgVectorIterator
std::vector< Arg * >::const_iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:392
TCLAP::DelPtr
void DelPtr(T ptr)
Definition: CmdLine.h:53
TCLAP::ArgListIterator
std::list< Arg * >::const_iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: Arg.h:387
TCLAP::ClearContainer
void ClearContainer(C &c)
Definition: CmdLine.h:58