tclap  1.2.1
Arg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: Arg.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 
25 #ifndef TCLAP_ARGUMENT_H
26 #define TCLAP_ARGUMENT_H
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #else
31 #define HAVE_SSTREAM
32 #endif
33 
34 #include <string>
35 #include <vector>
36 #include <list>
37 #include <iostream>
38 #include <iomanip>
39 #include <cstdio>
40 
41 #if defined(HAVE_SSTREAM)
42 #include <sstream>
44 #elif defined(HAVE_STRSTREAM)
45 #include <strstream>
46 typedef std::istrstream istringstream;
47 #else
48 #error "Need a stringstream (sstream or strstream) to compile!"
49 #endif
50 
51 #include <tclap/ArgException.h>
52 #include <tclap/Visitor.h>
53 #include <tclap/CmdLineInterface.h>
54 #include <tclap/ArgTraits.h>
55 #include <tclap/StandardTraits.h>
56 
57 namespace TCLAP {
58 
64 class Arg
65 {
66  private:
70  Arg(const Arg& rhs);
71 
75  Arg& operator=(const Arg& rhs);
76 
80  static bool& ignoreRestRef() { static bool ign = false; return ign; }
81 
86  static char& delimiterRef() { static char delim = ' '; return delim; }
87 
88  protected:
89 
98  std::string _flag;
99 
107  std::string _name;
108 
112  std::string _description;
113 
117  bool _required;
118 
123  std::string _requireLabel;
124 
131 
138 
146 
151 
156  bool _xorSet;
157 
159 
163  void _checkWithVisitor() const;
164 
178  Arg( const std::string& flag,
179  const std::string& name,
180  const std::string& desc,
181  bool req,
182  bool valreq,
183  Visitor* v = NULL );
184 
185  public:
189  virtual ~Arg();
190 
195  virtual void addToList( std::list<Arg*>& argList ) const;
196 
200  static void beginIgnoring() { ignoreRestRef() = true; }
201 
205  static bool ignoreRest() { return ignoreRestRef(); }
206 
211  static char delimiter() { return delimiterRef(); }
212 
217  static char blankChar() { return (char)7; }
218 
223 #ifndef TCLAP_FLAGSTARTCHAR
224 #define TCLAP_FLAGSTARTCHAR '-'
225 #endif
226  static char flagStartChar() { return TCLAP_FLAGSTARTCHAR; }
227 
233 #ifndef TCLAP_FLAGSTARTSTRING
234 #define TCLAP_FLAGSTARTSTRING "-"
235 #endif
236  static const std::string flagStartString() { return TCLAP_FLAGSTARTSTRING; }
237 
242 #ifndef TCLAP_NAMESTARTSTRING
243 #define TCLAP_NAMESTARTSTRING "--"
244 #endif
245  static const std::string nameStartString() { return TCLAP_NAMESTARTSTRING; }
246 
250  static const std::string ignoreNameString() { return "ignore_rest"; }
251 
256  static void setDelimiter( char c ) { delimiterRef() = c; }
257 
265  virtual bool processArg(int *i, std::vector<std::string>& args) = 0;
266 
272  virtual bool operator==(const Arg& a) const;
273 
277  const std::string& getFlag() const;
278 
282  const std::string& getName() const;
283 
287  std::string getDescription() const;
288 
292  virtual bool isRequired() const;
293 
298  void forceRequired();
299 
304  void xorSet();
305 
309  bool isValueRequired() const;
310 
315  bool isSet() const;
316 
320  bool isIgnoreable() const;
321 
330  virtual bool argMatches( const std::string& s ) const;
331 
336  virtual std::string toString() const;
337 
342  virtual std::string shortID( const std::string& valueId = "val" ) const;
343 
348  virtual std::string longID( const std::string& valueId = "val" ) const;
349 
357  virtual void trimFlag( std::string& flag, std::string& value ) const;
358 
365  bool _hasBlanks( const std::string& s ) const;
366 
372  void setRequireLabel( const std::string& s );
373 
378  virtual bool allowMore();
379 
384  virtual bool acceptsMultipleValues();
385 
390  virtual void reset();
391 };
392 
396 typedef std::list<Arg*>::iterator ArgListIterator;
397 
401 typedef std::vector<Arg*>::iterator ArgVectorIterator;
402 
406 typedef std::list<Visitor*>::iterator VisitorListIterator;
407 
408 /*
409  * Extract a value of type T from it's string representation contained
410  * in strVal. The ValueLike parameter used to select the correct
411  * specialization of ExtractValue depending on the value traits of T.
412  * ValueLike traits use operator>> to assign the value from strVal.
413  */
414 template<typename T> void
415 ExtractValue(T &destVal, const std::string& strVal, ValueLike vl)
416 {
417  static_cast<void>(vl); // Avoid warning about unused vl
418  std::istringstream is(strVal);
419 
420  int valuesRead = 0;
421  while ( is.good() ) {
422  if ( is.peek() != EOF )
423 #ifdef TCLAP_SETBASE_ZERO
424  is >> std::setbase(0) >> destVal;
425 #else
426  is >> destVal;
427 #endif
428  else
429  break;
430 
431  valuesRead++;
432  }
433 
434  if ( is.fail() )
435  throw( ArgParseException("Couldn't read argument value "
436  "from string '" + strVal + "'"));
437 
438 
439  if ( valuesRead > 1 )
440  throw( ArgParseException("More than one valid value parsed from "
441  "string '" + strVal + "'"));
442 
443 }
444 
445 /*
446  * Extract a value of type T from it's string representation contained
447  * in strVal. The ValueLike parameter used to select the correct
448  * specialization of ExtractValue depending on the value traits of T.
449  * StringLike uses assignment (operator=) to assign from strVal.
450  */
451 template<typename T> void
452 ExtractValue(T &destVal, const std::string& strVal, StringLike sl)
453 {
454  static_cast<void>(sl); // Avoid warning about unused sl
455  SetString(destVal, strVal);
456 }
457 
459 //BEGIN Arg.cpp
461 
462 inline Arg::Arg(const std::string& flag,
463  const std::string& name,
464  const std::string& desc,
465  bool req,
466  bool valreq,
467  Visitor* v) :
468  _flag(flag),
469  _name(name),
470  _description(desc),
471  _required(req),
472  _requireLabel("required"),
473  _valueRequired(valreq),
474  _alreadySet(false),
475  _visitor( v ),
476  _ignoreable(true),
477  _xorSet(false),
478  _acceptsMultipleValues(false)
479 {
480  if ( _flag.length() > 1 )
482  "Argument flag can only be one character long", toString() ) );
483 
484  if ( _name != ignoreNameString() &&
485  ( _flag == Arg::flagStartString() ||
487  _flag == " " ) )
488  throw(SpecificationException("Argument flag cannot be either '" +
489  Arg::flagStartString() + "' or '" +
490  Arg::nameStartString() + "' or a space.",
491  toString() ) );
492 
493  if ( ( _name.substr( 0, Arg::flagStartString().length() ) == Arg::flagStartString() ) ||
494  ( _name.substr( 0, Arg::nameStartString().length() ) == Arg::nameStartString() ) ||
495  ( _name.find( " ", 0 ) != std::string::npos ) )
496  throw(SpecificationException("Argument name begin with either '" +
497  Arg::flagStartString() + "' or '" +
498  Arg::nameStartString() + "' or space.",
499  toString() ) );
500 
501 }
502 
503 inline Arg::~Arg() { }
504 
505 inline std::string Arg::shortID( const std::string& valueId ) const
506 {
507  std::string id = "";
508 
509  if ( _flag != "" )
510  id = Arg::flagStartString() + _flag;
511  else
512  id = Arg::nameStartString() + _name;
513 
514  if ( _valueRequired )
515  id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
516 
517  if ( !_required )
518  id = "[" + id + "]";
519 
520  return id;
521 }
522 
523 inline std::string Arg::longID( const std::string& valueId ) const
524 {
525  std::string id = "";
526 
527  if ( _flag != "" )
528  {
529  id += Arg::flagStartString() + _flag;
530 
531  if ( _valueRequired )
532  id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
533 
534  id += ", ";
535  }
536 
537  id += Arg::nameStartString() + _name;
538 
539  if ( _valueRequired )
540  id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">";
541 
542  return id;
543 
544 }
545 
546 inline bool Arg::operator==(const Arg& a) const
547 {
548  if ( ( _flag != "" && _flag == a._flag ) || _name == a._name)
549  return true;
550  else
551  return false;
552 }
553 
554 inline std::string Arg::getDescription() const
555 {
556  std::string desc = "";
557  if ( _required )
558  desc = "(" + _requireLabel + ") ";
559 
560 // if ( _valueRequired )
561 // desc += "(value required) ";
562 
563  desc += _description;
564  return desc;
565 }
566 
567 inline const std::string& Arg::getFlag() const { return _flag; }
568 
569 inline const std::string& Arg::getName() const { return _name; }
570 
571 inline bool Arg::isRequired() const { return _required; }
572 
573 inline bool Arg::isValueRequired() const { return _valueRequired; }
574 
575 inline bool Arg::isSet() const
576 {
577  if ( _alreadySet && !_xorSet )
578  return true;
579  else
580  return false;
581 }
582 
583 inline bool Arg::isIgnoreable() const { return _ignoreable; }
584 
585 inline void Arg::setRequireLabel( const std::string& s)
586 {
587  _requireLabel = s;
588 }
589 
590 inline bool Arg::argMatches( const std::string& argFlag ) const
591 {
592  if ( ( argFlag == Arg::flagStartString() + _flag && _flag != "" ) ||
593  argFlag == Arg::nameStartString() + _name )
594  return true;
595  else
596  return false;
597 }
598 
599 inline std::string Arg::toString() const
600 {
601  std::string s = "";
602 
603  if ( _flag != "" )
604  s += Arg::flagStartString() + _flag + " ";
605 
606  s += "(" + Arg::nameStartString() + _name + ")";
607 
608  return s;
609 }
610 
611 inline void Arg::_checkWithVisitor() const
612 {
613  if ( _visitor != NULL )
614  _visitor->visit();
615 }
616 
620 inline void Arg::trimFlag(std::string& flag, std::string& value) const
621 {
622  int stop = 0;
623  for ( int i = 0; static_cast<unsigned int>(i) < flag.length(); i++ )
624  if ( flag[i] == Arg::delimiter() )
625  {
626  stop = i;
627  break;
628  }
629 
630  if ( stop > 1 )
631  {
632  value = flag.substr(stop+1);
633  flag = flag.substr(0,stop);
634  }
635 
636 }
637 
641 inline bool Arg::_hasBlanks( const std::string& s ) const
642 {
643  for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
644  if ( s[i] == Arg::blankChar() )
645  return true;
646 
647  return false;
648 }
649 
650 inline void Arg::forceRequired()
651 {
652  _required = true;
653 }
654 
655 inline void Arg::xorSet()
656 {
657  _alreadySet = true;
658  _xorSet = true;
659 }
660 
664 inline void Arg::addToList( std::list<Arg*>& argList ) const
665 {
666  argList.push_front( const_cast<Arg*>(this) );
667 }
668 
669 inline bool Arg::allowMore()
670 {
671  return false;
672 }
673 
675 {
676  return _acceptsMultipleValues;
677 }
678 
679 inline void Arg::reset()
680 {
681  _xorSet = false;
682  _alreadySet = false;
683 }
684 
686 //END Arg.cpp
688 
689 } //namespace TCLAP
690 
691 #endif
692 
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:590
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:137
bool _required
Indicating whether the argument is required.
Definition: Arg.h:117
bool _valueRequired
Indicates whether a value is required for the argument.
Definition: Arg.h:130
std::istringstream istringstream
Definition: Arg.h:43
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:64
void xorSet()
Sets the _alreadySet value to true.
Definition: Arg.h:655
void SetString(T &dst, const std::string &src)
virtual void addToList(std::list< Arg *> &argList) const
Adds this to the specified list of Args.
Definition: Arg.h:664
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:38
void setRequireLabel(const std::string &s)
Sets the requireLabel.
Definition: Arg.h:585
static char flagStartChar()
Definition: Arg.h:226
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:679
bool _xorSet
Indicates that the arg was set as part of an XOR and not on the command line.
Definition: Arg.h:156
bool isSet() const
Indicates whether the argument has already been set.
Definition: Arg.h:575
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
Definition: ArgException.h:167
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:523
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:211
virtual ~Arg()
Destructor.
Definition: Arg.h:503
std::list< Visitor * >::iterator VisitorListIterator
Typedef of a Visitor list iterator.
Definition: Arg.h:406
#define TCLAP_FLAGSTARTCHAR
The char that indicates the beginning of a flag.
Definition: Arg.h:224
A string like argument value type is a value that can be set using operator=(string).
Definition: ArgTraits.h:48
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: Arg.h:571
Visitor * _visitor
A pointer to a vistitor object.
Definition: Arg.h:145
void _checkWithVisitor() const
Performs the special handling described by the Vistitor.
Definition: Arg.h:611
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
Definition: Arg.h:250
static const std::string nameStartString()
Definition: Arg.h:245
#define TCLAP_NAMESTARTSTRING
The sting that indicates the beginning of a name.
Definition: Arg.h:243
A base class that defines the interface for visitors.
Definition: Visitor.h:31
const std::string & getName() const
Returns the argument name.
Definition: Arg.h:569
std::string _description
Description of the argument.
Definition: Arg.h:112
std::list< Arg * >::iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: Arg.h:396
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:505
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:599
bool isValueRequired() const
Indicates whether a value must be specified for argument.
Definition: Arg.h:573
virtual bool acceptsMultipleValues()
Use by output classes to determine whether an Arg accepts multiple values.
Definition: Arg.h:674
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:150
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:401
virtual bool processArg(int *i, std::vector< std::string > &args)=0
Pure virtual method meant to handle the parsing and value assignment of the string on the command lin...
static void beginIgnoring()
Begin ignoring arguments since the "--" argument was specified.
Definition: Arg.h:200
virtual bool operator==(const Arg &a) const
Operator ==.
Definition: Arg.h:546
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:205
bool isIgnoreable() const
Indicates whether the argument can be ignored, if desired.
Definition: Arg.h:583
std::string getDescription() const
Returns the argument description.
Definition: Arg.h:554
const std::string & getFlag() const
Returns the argument flag.
Definition: Arg.h:567
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:415
virtual void visit()
Does nothing.
Definition: Visitor.h:48
std::string _name
A single work namd indentifying the argument.
Definition: Arg.h:107
static char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition: Arg.h:217
std::string _flag
The single char flag used to identify the argument.
Definition: Arg.h:98
bool _acceptsMultipleValues
Definition: Arg.h:158
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:121
void forceRequired()
Sets _required to true.
Definition: Arg.h:650
Definition: Arg.h:57
virtual bool allowMore()
Used for MultiArgs and XorHandler to determine whether args can still be set.
Definition: Arg.h:669
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
Definition: Arg.h:641
#define TCLAP_FLAGSTARTSTRING
The sting that indicates the beginning of a flag.
Definition: Arg.h:234
std::string _requireLabel
Label to be used in usage description.
Definition: Arg.h:123
static const std::string flagStartString()
Definition: Arg.h:236
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition: Arg.h:620
static void setDelimiter(char c)
Sets the delimiter for all arguments.
Definition: Arg.h:256