tclap 1.2.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

CmdLine.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3 /******************************************************************************
4 *
5 * file: CmdLine.h
6 *
7 * Copyright (c) 2003, Michael E. Smoot .
8 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
9 * All rights reverved.
10 *
11 * See the file COPYING in the top directory of this distribution for
12 * more information.
13 *
14 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *
22 *****************************************************************************/
23
24 #ifndef TCLAP_CMDLINE_H
25 #define TCLAP_CMDLINE_H
26
27 #include <tclap/SwitchArg.h>
28 #include <tclap/MultiSwitchArg.h>
29 #include <tclap/UnlabeledValueArg.h>
30 #include <tclap/UnlabeledMultiArg.h>
31
32 #include <tclap/XorHandler.h>
33 #include <tclap/HelpVisitor.h>
34 #include <tclap/VersionVisitor.h>
35 #include <tclap/IgnoreRestVisitor.h>
36
37 #include <tclap/CmdLineOutput.h>
38 #include <tclap/StdOutput.h>
39
40 #include <tclap/Constraint.h>
41 #include <tclap/ValuesConstraint.h>
42
43 #include <string>
44 #include <vector>
45 #include <list>
46 #include <iostream>
47 #include <iomanip>
48 #include <algorithm>
49 #include <stdlib.h> // Needed for exit(), which isn't defined in some envs.
50
51 namespace TCLAP {
52
53 template<typename T> void DelPtr(T ptr)
54 {
55 delete ptr;
56 }
57
58 template<typename C> void ClearContainer(C &c)
59 {
60 typedef typename C::value_type value_type;
61 std::for_each(c.begin(), c.end(), DelPtr<value_type>);
62 c.clear();
63 }
64
65
70 class CmdLine : public CmdLineInterface
71 {
72 protected:
73
78 std::list<Arg*> _argList;
79
83 std::string _progName;
84
88 std::string _message;
89
93 std::string _version;
94
100 int _numRequired;
101
106 char _delimiter;
107
111 XorHandler _xorHandler;
112
118 std::list<Arg*> _argDeleteOnExitList;
119
125 std::list<Visitor*> _visitorDeleteOnExitList;
126
130 CmdLineOutput* _output;
131
135 bool _handleExceptions;
136
140 void missingArgsException();
141
148 bool _emptyCombined(const std::string& s);
149
153 void deleteOnExit(Arg* ptr);
154
158 void deleteOnExit(Visitor* ptr);
159
160 private:
161
165 CmdLine(const CmdLine& rhs);
166 CmdLine& operator=(const CmdLine& rhs);
167
172 void _constructor();
173
174
179 bool _userSetOutput;
180
184 bool _helpAndVersion;
185
186 public:
187
200 CmdLine(const std::string& message,
201 const char delimiter = ' ',
202 const std::string& version = "none",
203 bool helpAndVersion = true);
204
208 virtual ~CmdLine();
209
214 void add( Arg& a );
215
220 void add( Arg* a );
221
228 void xorAdd( Arg& a, Arg& b );
229
235 void xorAdd( std::vector<Arg*>& xors );
236
242 void parse(int argc, const char * const * argv);
243
249 void parse(std::vector<std::string>& args);
250
254 CmdLineOutput* getOutput();
255
259 void setOutput(CmdLineOutput* co);
260
264 std::string& getVersion();
265
269 std::string& getProgramName();
270
274 std::list<Arg*>& getArgList();
275
279 XorHandler& getXorHandler();
280
284 char getDelimiter();
285
289 std::string& getMessage();
290
294 bool hasHelpAndVersion();
295
301 void setExceptionHandling(const bool state);
302
309 bool getExceptionHandling() const;
310
314 void reset();
315
316 };
317
318
320 //Begin CmdLine.cpp
322
323 inline CmdLine::CmdLine(const std::string& m,
324 char delim,
325 const std::string& v,
326 bool help )
327 :
328 _argList(std::list<Arg*>()),
329 _progName("not_set_yet"),
330 _message(m),
331 _version(v),
332 _numRequired(0),
333 _delimiter(delim),
334 _xorHandler(XorHandler()),
335 _argDeleteOnExitList(std::list<Arg*>()),
336 _visitorDeleteOnExitList(std::list<Visitor*>()),
337 _output(0),
338 _handleExceptions(true),
339 _userSetOutput(false),
340 _helpAndVersion(help)
341 {
342 _constructor();
343 }
344
345 inline CmdLine::~CmdLine()
346 {
347 ClearContainer(_argDeleteOnExitList);
348 ClearContainer(_visitorDeleteOnExitList);
349
350 if ( !_userSetOutput ) {
351 delete _output;
352 _output = 0;
353 }
354 }
355
356 inline void CmdLine::_constructor()
357 {
358 _output = new StdOutput;
359
360 Arg::setDelimiter( _delimiter );
361
362 Visitor* v;
363
364 if ( _helpAndVersion )
365 {
366 v = new HelpVisitor( this, &_output );
367 SwitchArg* help = new SwitchArg("h","help",
368 "Displays usage information and exits.",
369 false, v);
370 add( help );
371 deleteOnExit(help);
372 deleteOnExit(v);
373
374 v = new VersionVisitor( this, &_output );
375 SwitchArg* vers = new SwitchArg("","version",
376 "Displays version information and exits.",
377 false, v);
378 add( vers );
379 deleteOnExit(vers);
380 deleteOnExit(v);
381 }
382
383 v = new IgnoreRestVisitor();
384 SwitchArg* ignore = new SwitchArg(Arg::flagStartString(),
385 Arg::ignoreNameString(),
386 "Ignores the rest of the labeled arguments following this flag.",
387 false, v);
388 add( ignore );
389 deleteOnExit(ignore);
390 deleteOnExit(v);
391 }
392
393 inline void CmdLine::xorAdd( std::vector<Arg*>& ors )
394 {
395 _xorHandler.add( ors );
396
397 for (ArgVectorIterator it = ors.begin(); it != ors.end(); it++)
398 {
399 (*it)->forceRequired();
400 (*it)->setRequireLabel( "OR required" );
401 add( *it );
402 }
403 }
404
405 inline void CmdLine::xorAdd( Arg& a, Arg& b )
406 {
407 std::vector<Arg*> ors;
408 ors.push_back( &a );
409 ors.push_back( &b );
410 xorAdd( ors );
411 }
412
413 inline void CmdLine::add( Arg& a )
414 {
415 add( &a );
416 }
417
418 inline void CmdLine::add( Arg* a )
419 {
420 for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
421 if ( *a == *(*it) )
422 throw( SpecificationException(
423 "Argument with same flag/name already exists!",
424 a->longID() ) );
425
426 a->addToList( _argList );
427
428 if ( a->isRequired() )
429 _numRequired++;
430 }
431
432
433 inline void CmdLine::parse(int argc, const char * const * argv)
434 {
435 // this step is necessary so that we have easy access to
436 // mutable strings.
437 std::vector<std::string> args;
438 for (int i = 0; i < argc; i++)
439 args.push_back(argv[i]);
440
441 parse(args);
442 }
443
444 inline void CmdLine::parse(std::vector<std::string>& args)
445 {
446 bool shouldExit = false;
447 int estat = 0;
448
449 try {
450 _progName = args.front();
451 args.erase(args.begin());
452
453 int requiredCount = 0;
454
455 for (int i = 0; static_cast<unsigned int>(i) < args.size(); i++)
456 {
457 bool matched = false;
458 for (ArgListIterator it = _argList.begin();
459 it != _argList.end(); it++) {
460 if ( (*it)->processArg( &i, args ) )
461 {
462 requiredCount += _xorHandler.check( *it );
463 matched = true;
464 break;
465 }
466 }
467
468 // checks to see if the argument is an empty combined
469 // switch and if so, then we've actually matched it
470 if ( !matched && _emptyCombined( args[i] ) )
471 matched = true;
472
473 if ( !matched && !Arg::ignoreRest() )
474 throw(CmdLineParseException("Couldn't find match "
475 "for argument",
476 args[i]));
477 }
478
479 if ( requiredCount < _numRequired )
480 missingArgsException();
481
482 if ( requiredCount > _numRequired )
483 throw(CmdLineParseException("Too many arguments!"));
484
485 } catch ( ArgException& e ) {
486 // If we're not handling the exceptions, rethrow.
487 if ( !_handleExceptions) {
488 throw;
489 }
490
491 try {
492 _output->failure(*this,e);
493 } catch ( ExitException &ee ) {
494 estat = ee.getExitStatus();
495 shouldExit = true;
496 }
497 } catch (ExitException &ee) {
498 // If we're not handling the exceptions, rethrow.
499 if ( !_handleExceptions) {
500 throw;
501 }
502
503 estat = ee.getExitStatus();
504 shouldExit = true;
505 }
506
507 if (shouldExit)
508 exit(estat);
509 }
510
511 inline bool CmdLine::_emptyCombined(const std::string& s)
512 {
513 if ( s.length() > 0 && s[0] != Arg::flagStartChar() )
514 return false;
515
516 for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
517 if ( s[i] != Arg::blankChar() )
518 return false;
519
520 return true;
521 }
522
523 inline void CmdLine::missingArgsException()
524 {
525 int count = 0;
526
527 std::string missingArgList;
528 for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++)
529 {
530 if ( (*it)->isRequired() && !(*it)->isSet() )
531 {
532 missingArgList += (*it)->getName();
533 missingArgList += ", ";
534 count++;
535 }
536 }
537 missingArgList = missingArgList.substr(0,missingArgList.length()-2);
538
539 std::string msg;
540 if ( count > 1 )
541 msg = "Required arguments missing: ";
542 else
543 msg = "Required argument missing: ";
544
545 msg += missingArgList;
546
547 throw(CmdLineParseException(msg));
548 }
549
550 inline void CmdLine::deleteOnExit(Arg* ptr)
551 {
552 _argDeleteOnExitList.push_back(ptr);
553 }
554
555 inline void CmdLine::deleteOnExit(Visitor* ptr)
556 {
557 _visitorDeleteOnExitList.push_back(ptr);
558 }
559
560 inline CmdLineOutput* CmdLine::getOutput()
561 {
562 return _output;
563 }
564
565 inline void CmdLine::setOutput(CmdLineOutput* co)
566 {
567 if ( !_userSetOutput )
568 delete _output;
569 _userSetOutput = true;
570 _output = co;
571 }
572
573 inline std::string& CmdLine::getVersion()
574 {
575 return _version;
576 }
577
578 inline std::string& CmdLine::getProgramName()
579 {
580 return _progName;
581 }
582
583 inline std::list<Arg*>& CmdLine::getArgList()
584 {
585 return _argList;
586 }
587
588 inline XorHandler& CmdLine::getXorHandler()
589 {
590 return _xorHandler;
591 }
592
593 inline char CmdLine::getDelimiter()
594 {
595 return _delimiter;
596 }
597
598 inline std::string& CmdLine::getMessage()
599 {
600 return _message;
601 }
602
603 inline bool CmdLine::hasHelpAndVersion()
604 {
605 return _helpAndVersion;
606 }
607
608 inline void CmdLine::setExceptionHandling(const bool state)
609 {
610 _handleExceptions = state;
611 }
612
613 inline bool CmdLine::getExceptionHandling() const
614 {
615 return _handleExceptions;
616 }
617
618 inline void CmdLine::reset()
619 {
620 for( ArgListIterator it = _argList.begin(); it != _argList.end(); it++ )
621 (*it)->reset();
622
623 _progName.clear();
624 }
625
627 //End CmdLine.cpp
629
630
631
632 } //namespace TCLAP
633 #endif
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::_output
CmdLineOutput * _output
Object that handles all output for the CmdLine.
Definition: CmdLine.h:130
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:64
HelpVisitor.h
TCLAP::CmdLine::getExceptionHandling
bool getExceptionHandling() const
Returns the current state of the internal exception handling.
Definition: CmdLine.h:613
TCLAP::CmdLine::_message
std::string _message
A message used to describe the program.
Definition: CmdLine.h:88
TCLAP::CmdLine::xorAdd
void xorAdd(Arg &a, Arg &b)
Add two Args that will be xor'd.
Definition: CmdLine.h:405
TCLAP::CmdLine::_argList
std::list< Arg * > _argList
The list of arguments that will be tested against the command line.
Definition: CmdLine.h:78
TCLAP::ExitException::getExitStatus
int getExitStatus() const
Definition: ArgException.h:191
TCLAP::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:523
TCLAP::ArgException
A simple class that defines and argument exception.
Definition: ArgException.h:36
TCLAP::CmdLineOutput::failure
virtual void failure(CmdLineInterface &c, ArgException &e)=0
Generates some sort of output for a failure.
TCLAP::CmdLine::_progName
std::string _progName
The name of the program.
Definition: CmdLine.h:83
TCLAP::CmdLineParseException
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: ArgException.h:143
CmdLineOutput.h
TCLAP::Arg::flagStartChar
static char flagStartChar()
Definition: Arg.h:226
VersionVisitor.h
TCLAP::Arg::addToList
virtual void addToList(std::list< Arg * > &argList) const
Adds this to the specified list of Args.
Definition: Arg.h:664
TCLAP::DelPtr
void DelPtr(T ptr)
Definition: CmdLine.h:53
ValuesConstraint.h
TCLAP::CmdLine::getProgramName
std::string & getProgramName()
Returns the program name string.
Definition: CmdLine.h:578
TCLAP::CmdLine::_version
std::string _version
The version to be displayed with the –version switch.
Definition: CmdLine.h:93
TCLAP::ExitException
Definition: ArgException.h:187
TCLAP::SwitchArg
A simple switch argument.
Definition: SwitchArg.h:39
XorHandler.h
TCLAP::CmdLine::deleteOnExit
void deleteOnExit(Arg *ptr)
Perform a delete ptr; operation on ptr when this object is deleted.
Definition: CmdLine.h:550
UnlabeledMultiArg.h
TCLAP::SpecificationException
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
Definition: ArgException.h:167
TCLAP::CmdLine::reset
void reset()
Allows the CmdLine object to be reused.
Definition: CmdLine.h:618
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, returns the size of the xor list that the Arg matched.
Definition: XorHandler.h:100
MultiSwitchArg.h
TCLAP::CmdLine::_delimiter
char _delimiter
The character that is used to separate the argument flag/name from the value.
Definition: CmdLine.h:106
IgnoreRestVisitor.h
SwitchArg.h
TCLAP::VersionVisitor
A Vistor that will call the version method of the given CmdLineOutput for the specified CmdLine objec...
Definition: VersionVisitor.h:37
TCLAP::Arg::ignoreNameString
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
Definition: Arg.h:250
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::~CmdLine
virtual ~CmdLine()
Deletes any resources allocated by a CmdLine object.
Definition: CmdLine.h:345
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::_emptyCombined
bool _emptyCombined(const std::string &s)
Checks whether a name/flag string matches entirely matches the Arg::blankChar.
Definition: CmdLine.h:511
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:31
TCLAP::ArgListIterator
std::list< Arg * >::iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: Arg.h:396
StdOutput.h
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:43
Constraint.h
TCLAP::CmdLine::getOutput
CmdLineOutput * getOutput()
Returns the CmdLineOutput object.
Definition: CmdLine.h:560
UnlabeledValueArg.h
TCLAP::CmdLine::setExceptionHandling
void setExceptionHandling(const bool state)
Disables or enables CmdLine's internal parsing exception handling.
Definition: CmdLine.h:608
TCLAP::ArgVectorIterator
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:401
TCLAP::StdOutput
A class that isolates any output from the CmdLine object so that it may be easily modified...
Definition: StdOutput.h:43
TCLAP::CmdLine::getMessage
std::string & getMessage()
Returns the message string.
Definition: CmdLine.h:598
TCLAP::HelpVisitor
A Visitor object that calls the usage method of the given CmdLineOutput object for the specified CmdL...
Definition: HelpVisitor.h:35
TCLAP::CmdLine::parse
void parse(int argc, const char *const *argv)
Parses the command line.
Definition: CmdLine.h:433
TCLAP::Arg::ignoreRest
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:205
TCLAP::CmdLine::hasHelpAndVersion
bool hasHelpAndVersion()
Indicates whether or not the help and version switches were created automatically.
Definition: CmdLine.h:603
TCLAP::CmdLine::_xorHandler
XorHandler _xorHandler
The handler that manages xoring lists of args.
Definition: CmdLine.h:111
TCLAP::CmdLine::getVersion
std::string & getVersion()
Returns the version string.
Definition: CmdLine.h:573
TCLAP::CmdLine::add
void add(Arg &a)
Adds an argument to the list of arguments to be parsed.
Definition: CmdLine.h:413
TCLAP::CmdLine::setOutput
void setOutput(CmdLineOutput *co)
Definition: CmdLine.h:565
TCLAP::Arg::blankChar
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition: Arg.h:217
TCLAP::CmdLine::getDelimiter
char getDelimiter()
Returns the delimiter string.
Definition: CmdLine.h:593
TCLAP::CmdLine::getArgList
std::list< Arg * > & getArgList()
Returns the argList.
Definition: CmdLine.h:583
TCLAP
Definition: Arg.h:57
TCLAP::CmdLine
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLine.h:70
TCLAP::CmdLine::missingArgsException
void missingArgsException()
Throws an exception listing the missing args.
Definition: CmdLine.h:523
TCLAP::CmdLine::getXorHandler
XorHandler & getXorHandler()
Returns the XorHandler.
Definition: CmdLine.h:588
TCLAP::CmdLine::_handleExceptions
bool _handleExceptions
Should CmdLine handle parsing exceptions internally?
Definition: CmdLine.h:135
TCLAP::Arg::flagStartString
static const std::string flagStartString()
Definition: Arg.h:236
TCLAP::XorHandler
This class handles lists of Arg's that are to be XOR'd on the command line.
Definition: XorHandler.h:38
TCLAP::ClearContainer
void ClearContainer(C &c)
Definition: CmdLine.h:58
TCLAP::Arg::setDelimiter
static void setDelimiter(char c)
Sets the delimiter for all arguments.
Definition: Arg.h:256
TCLAP::CmdLineOutput
The interface that any output object must implement.
Definition: CmdLineOutput.h:41
TCLAP::Arg::isRequired
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: Arg.h:571
TCLAP::XorHandler::add
void add(std::vector< Arg * > &ors)
Add a list of Arg*'s that will be orred together.
Definition: XorHandler.h:95