2
/******************************************************************************
6
* Copyright (c) 2003, Michael E. Smoot .
7
* Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
10
* See the file COPYING in the top directory of this distribution for
13
* THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
* DEALINGS IN THE SOFTWARE.
21
*****************************************************************************/
23
#ifndef TCLAP_CMDLINE_H
24
#define TCLAP_CMDLINE_H
26
#include <tclap/SwitchArg.h>
27
#include <tclap/MultiSwitchArg.h>
28
#include <tclap/UnlabeledValueArg.h>
29
#include <tclap/UnlabeledMultiArg.h>
31
#include <tclap/XorHandler.h>
32
#include <tclap/HelpVisitor.h>
33
#include <tclap/VersionVisitor.h>
34
#include <tclap/IgnoreRestVisitor.h>
36
#include <tclap/CmdLineOutput.h>
37
#include <tclap/StdOutput.h>
39
#include <tclap/Constraint.h>
40
#include <tclap/ValuesConstraint.h>
52
* The base class that manages the command line definition and passes
53
* along the parsing to the appropriate Arg classes.
55
class CmdLine : public CmdLineInterface
60
* The list of arguments that will be tested against the
63
std::list<Arg*> _argList;
66
* The name of the program. Set to argv[0].
68
std::string _progName;
71
* A message used to describe the program. Used in the usage output.
76
* The version to be displayed with the --version switch.
81
* The number of arguments that are required to be present on
82
* the command line. This is set dynamically, based on the
83
* Args added to the CmdLine object.
88
* The character that is used to separate the argument flag/name
89
* from the value. Defaults to ' ' (space).
94
* The handler that manages xoring lists of args.
96
XorHandler _xorHandler;
99
* A list of Args to be explicitly deleted when the destructor
100
* is called. At the moment, this only includes the three default
103
std::list<Arg*> _argDeleteOnExitList;
106
* A list of Visitors to be explicitly deleted when the destructor
107
* is called. At the moment, these are the Vistors created for the
110
std::list<Visitor*> _visitorDeleteOnExitList;
113
* Object that handles all output for the CmdLine.
115
CmdLineOutput* _output;
118
* Checks whether a name/flag string matches entirely matches
119
* the Arg::blankChar. Used when multiple switches are combined
120
* into a single argument.
121
* \param s - The message to be used in the usage.
123
bool _emptyCombined(const std::string& s);
126
* Perform a delete ptr; operation on ptr when this object is deleted.
128
void deleteOnExit(Arg* ptr);
131
* Perform a delete ptr; operation on ptr when this object is deleted.
133
void deleteOnExit(Visitor* ptr);
138
* Encapsulates the code common to the constructors (which is all
144
* Is set to true when a user sets the output object. We use this so
145
* that we don't delete objects that are created outside of this lib.
150
* Whether or not to automatically create help and version switches.
152
bool _helpAndVersion;
157
* Command line constructor. Defines how the arguments will be
159
* \param message - The message to be used in the usage
161
* \param delimiter - The character that is used to separate
162
* the argument flag/name from the value. Defaults to ' ' (space).
163
* \param version - The version number to be used in the
165
* \param helpAndVersion - Whether or not to create the Help and
166
* Version switches. Defaults to true.
168
CmdLine(const std::string& message,
169
const char delimiter = ' ',
170
const std::string& version = "none",
171
bool helpAndVersion = true);
174
* Deletes any resources allocated by a CmdLine object.
179
* Adds an argument to the list of arguments to be parsed.
180
* \param a - Argument to be added.
185
* An alternative add. Functionally identical.
186
* \param a - Argument to be added.
191
* Add two Args that will be xor'd. If this method is used, add does
192
* not need to be called.
193
* \param a - Argument to be added and xor'd.
194
* \param b - Argument to be added and xor'd.
196
void xorAdd( Arg& a, Arg& b );
199
* Add a list of Args that will be xor'd. If this method is used,
200
* add does not need to be called.
201
* \param xors - List of Args to be added and xor'd.
203
void xorAdd( std::vector<Arg*>& xors );
206
* Parses the command line.
207
* \param argc - Number of arguments.
208
* \param argv - Array of arguments.
210
void parse(int argc, char** argv);
215
CmdLineOutput* getOutput();
220
void setOutput(CmdLineOutput* co);
225
std::string& getVersion();
230
std::string& getProgramName();
235
std::list<Arg*>& getArgList();
240
XorHandler& getXorHandler();
250
std::string& getMessage();
255
bool hasHelpAndVersion();
259
///////////////////////////////////////////////////////////////////////////////
261
///////////////////////////////////////////////////////////////////////////////
263
inline CmdLine::CmdLine(const std::string& m,
265
const std::string& v,
267
: _progName("not_set_yet"),
272
_userSetOutput(false),
273
_helpAndVersion(help)
278
inline CmdLine::~CmdLine()
280
ArgListIterator argIter;
281
VisitorListIterator visIter;
283
for( argIter = _argDeleteOnExitList.begin();
284
argIter != _argDeleteOnExitList.end();
288
for( visIter = _visitorDeleteOnExitList.begin();
289
visIter != _visitorDeleteOnExitList.end();
293
if ( !_userSetOutput )
297
inline void CmdLine::_constructor()
299
_output = new StdOutput;
301
Arg::setDelimiter( _delimiter );
305
if ( _helpAndVersion )
307
v = new HelpVisitor( this, &_output );
308
SwitchArg* help = new SwitchArg("h","help",
309
"Displays usage information and exits.",
315
v = new VersionVisitor( this, &_output );
316
SwitchArg* vers = new SwitchArg("","version",
317
"Displays version information and exits.",
324
v = new IgnoreRestVisitor();
325
SwitchArg* ignore = new SwitchArg(Arg::flagStartString(),
326
Arg::ignoreNameString(),
327
"Ignores the rest of the labeled arguments following this flag.",
330
deleteOnExit(ignore);
334
inline void CmdLine::xorAdd( std::vector<Arg*>& ors )
336
_xorHandler.add( ors );
338
for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++)
340
(*it)->forceRequired();
341
(*it)->setRequireLabel( "OR required" );
347
inline void CmdLine::xorAdd( Arg& a, Arg& b )
349
std::vector<Arg*> ors;
355
inline void CmdLine::add( Arg& a )
360
inline void CmdLine::add( Arg* a )
362
for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
364
throw( SpecificationException(
365
"Argument with same flag/name already exists!",
368
a->addToList( _argList );
370
if ( a->isRequired() )
374
inline void CmdLine::parse(int argc, char** argv)
380
// this step is necessary so that we have easy access to mutable strings.
381
std::vector<std::string> args;
382
for (int i = 1; i < argc; i++)
383
args.push_back(argv[i]);
385
int requiredCount = 0;
387
for (int i = 0; static_cast<unsigned int>(i) < args.size(); i++)
389
bool matched = false;
390
for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++)
392
if ( (*it)->processArg( &i, args ) )
394
requiredCount += _xorHandler.check( *it );
400
// checks to see if the argument is an empty combined switch ...
401
// and if so, then we've actually matched it
402
if ( !matched && _emptyCombined( args[i] ) )
405
if ( !matched && !Arg::ignoreRest() )
406
throw(CmdLineParseException("Couldn't find match for argument",
410
if ( requiredCount < _numRequired )
411
throw(CmdLineParseException("One or more required arguments missing!"));
413
if ( requiredCount > _numRequired )
414
throw(CmdLineParseException("Too many arguments!"));
416
} catch ( ArgException e ) { _output->failure(*this,e); exit(1); }
419
inline bool CmdLine::_emptyCombined(const std::string& s)
421
if ( s[0] != Arg::flagStartChar() )
424
for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
425
if ( s[i] != Arg::blankChar() )
431
inline void CmdLine::deleteOnExit(Arg* ptr)
433
_argDeleteOnExitList.push_back(ptr);
436
inline void CmdLine::deleteOnExit(Visitor* ptr)
438
_visitorDeleteOnExitList.push_back(ptr);
441
inline CmdLineOutput* CmdLine::getOutput()
446
inline void CmdLine::setOutput(CmdLineOutput* co)
448
_userSetOutput = true;
452
inline std::string& CmdLine::getVersion()
457
inline std::string& CmdLine::getProgramName()
462
inline std::list<Arg*>& CmdLine::getArgList()
467
inline XorHandler& CmdLine::getXorHandler()
472
inline char CmdLine::getDelimiter()
477
inline std::string& CmdLine::getMessage()
482
inline bool CmdLine::hasHelpAndVersion()
484
return _helpAndVersion;
487
///////////////////////////////////////////////////////////////////////////////
489
///////////////////////////////////////////////////////////////////////////////