arg.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 
16 #ifndef TCLAP_ARGUMENT_H
17 #define TCLAP_ARGUMENT_H
18 
19 #include <string>
20 #include <vector>
21 #include <list>
22 #include <iostream>
23 
24 #include "arg_exception.hpp"
25 #include "visitor.hpp"
26 #include "cmd_line_interface.hpp"
27 
28 namespace ecl {
29 
37 class Arg
38 {
39  private:
40 
44  static bool& ignoreRestRef() { static bool ign = false; return ign; }
45 
50  static char& delimiterRef() { static char delim = ' '; return delim; }
51 
52  protected:
53 
62  std::string _flag;
63 
71  std::string _name;
72 
76  std::string _description;
77 
81  bool _required;
82 
87  std::string _requireLabel;
88 
94  bool _valueRequired;
95 
101  bool _alreadySet;
102 
109  Visitor* _visitor;
110 
114  bool _ignoreable;
115 
120  bool _xorSet;
121 
123 
127  void _checkWithVisitor() const;
128 
142  Arg( const std::string& flag,
143  const std::string& name,
144  const std::string& desc,
145  bool req,
146  bool valreq,
147  Visitor* v = NULL );
148 
149  public:
153  virtual ~Arg();
154 
159  virtual void addToList( std::list<Arg*>& argList ) const;
160 
164  static void beginIgnoring() { ignoreRestRef() = true; }
165 
169  static bool ignoreRest() { return ignoreRestRef(); }
170 
175  static char delimiter() { return delimiterRef(); }
176 
182  static char blankChar() { return '*'; }
183 
187  static char flagStartChar() { return '-'; }
188 
193  static std::string flagStartString() { return "-"; }
194 
199  static std::string nameStartString() { return "--"; }
200 
204  static std::string ignoreNameString() { return "ignore_rest"; }
205 
210  static void setDelimiter( char c ) { delimiterRef() = c; }
211 
219  virtual bool processArg(int *i, std::vector<std::string>& args) = 0;
220 
226  virtual bool operator==(const Arg& a) const;
227 
231  const std::string& getFlag() const;
232 
236  const std::string& getName() const;
237 
241  std::string getDescription() const;
242 
246  virtual bool isRequired() const;
247 
252  void forceRequired();
253 
258  void xorSet();
259 
263  bool isValueRequired() const;
264 
269  bool isSet() const;
270 
274  bool isIgnoreable() const;
275 
284  virtual bool argMatches( const std::string& s ) const;
285 
290  virtual std::string toString() const;
291 
296  virtual std::string shortID( const std::string& valueId = "val" ) const;
297 
302  virtual std::string longID( const std::string& valueId = "val" ) const;
303 
311  virtual void trimFlag( std::string& flag, std::string& value ) const;
312 
319  bool _hasBlanks( const std::string& s ) const;
320 
326  void setRequireLabel( const std::string& s );
327 
328  virtual bool allowMore();
329  virtual bool acceptsMultipleValues();
330 
331 };
332 
336 typedef std::list<Arg*>::iterator ArgListIterator;
337 
341 typedef std::vector<Arg*>::iterator ArgVectorIterator;
342 
346 typedef std::list<Visitor*>::iterator VisitorListIterator;
347 
348 
350 //BEGIN Arg.cpp
352 
353 inline Arg::Arg(const std::string& flag,
354  const std::string& name,
355  const std::string& desc,
356  bool req,
357  bool valreq,
358  Visitor* v) :
359  _flag(flag),
360  _name(name),
361  _description(desc),
362  _required(req),
363  _requireLabel("required"),
364  _valueRequired(valreq),
365  _alreadySet(false),
366  _visitor( v ),
367  _ignoreable(true),
368  _xorSet(false),
370 {
371  if ( _flag.length() > 1 )
373  "Argument flag can only be one character long", toString() ) );
374 
375  if ( _name != ignoreNameString() &&
376  ( _flag == Arg::flagStartString() ||
377  _flag == Arg::nameStartString() ||
378  _flag == " " ) )
379  throw(SpecificationException("Argument flag cannot be either '" +
380  Arg::flagStartString() + "' or '" +
381  Arg::nameStartString() + "' or a space.",
382  toString() ) );
383 
384  if ( ( _name.find( Arg::flagStartString(), 0 ) != std::string::npos ) ||
385  ( _name.find( Arg::nameStartString(), 0 ) != std::string::npos ) ||
386  ( _name.find( " ", 0 ) != std::string::npos ) )
387  throw(SpecificationException("Argument name cannot contain either '" +
388  Arg::flagStartString() + "' or '" +
389  Arg::nameStartString() + "' or space.",
390  toString() ) );
391 
392 }
393 
394 inline Arg::~Arg() { }
395 
396 inline std::string Arg::shortID( const std::string& valueId ) const
397 {
398  std::string id = "";
399 
400  if ( _flag != "" )
401  id = Arg::flagStartString() + _flag;
402  else
403  id = Arg::nameStartString() + _name;
404 
405  std::string delim = " ";
406  delim[0] = Arg::delimiter(); // ugly!!!
407 
408  if ( _valueRequired )
409  id += delim + "<" + valueId + ">";
410 
411  if ( !_required )
412  id = "[" + id + "]";
413 
414  return id;
415 }
416 
417 inline std::string Arg::longID( const std::string& valueId ) const
418 {
419  std::string id = "";
420 
421  if ( _flag != "" )
422  {
423  id += Arg::flagStartString() + _flag;
424 
425  if ( _valueRequired )
426  id += " <" + valueId + ">";
427 
428  id += ", ";
429  }
430 
431  id += Arg::nameStartString() + _name;
432 
433  if ( _valueRequired )
434  id += " <" + valueId + ">";
435 
436  return id;
437 
438 }
439 
440 inline bool Arg::operator==(const Arg& a) const
441 {
442  if ( ( _flag != "" && _flag == a._flag ) || _name == a._name)
443  return true;
444  else
445  return false;
446 }
447 
448 inline std::string Arg::getDescription() const
449 {
450  std::string desc = "";
451  if ( _required )
452  desc = "(" + _requireLabel + ") ";
453 
454 // if ( _valueRequired )
455 // desc += "(value required) ";
456 
457  desc += _description;
458  return desc;
459 }
460 
461 inline const std::string& Arg::getFlag() const { return _flag; }
462 
463 inline const std::string& Arg::getName() const { return _name; }
464 
465 inline bool Arg::isRequired() const { return _required; }
466 
467 inline bool Arg::isValueRequired() const { return _valueRequired; }
468 
469 inline bool Arg::isSet() const
470 {
471  if ( _alreadySet && !_xorSet )
472  return true;
473  else
474  return false;
475 }
476 
477 inline bool Arg::isIgnoreable() const { return _ignoreable; }
478 
479 inline void Arg::setRequireLabel( const std::string& s)
480 {
481  _requireLabel = s;
482 }
483 
484 inline bool Arg::argMatches( const std::string& argFlag ) const
485 {
486  if ( ( argFlag == Arg::flagStartString() + _flag && _flag != "" ) ||
487  argFlag == Arg::nameStartString() + _name )
488  return true;
489  else
490  return false;
491 }
492 
493 inline std::string Arg::toString() const
494 {
495  std::string s = "";
496 
497  if ( _flag != "" )
498  s += Arg::flagStartString() + _flag + " ";
499 
500  s += "(" + Arg::nameStartString() + _name + ")";
501 
502  return s;
503 }
504 
505 inline void Arg::_checkWithVisitor() const
506 {
507  if ( _visitor != NULL )
508  _visitor->visit();
509 }
510 
514 inline void Arg::trimFlag(std::string& flag, std::string& value) const
515 {
516  int stop = 0;
517  for ( int i = 0; static_cast<unsigned int>(i) < flag.length(); i++ )
518  if ( flag[i] == Arg::delimiter() )
519  {
520  stop = i;
521  break;
522  }
523 
524  if ( stop > 1 )
525  {
526  value = flag.substr(stop+1);
527  flag = flag.substr(0,stop);
528  }
529 
530 }
531 
535 inline bool Arg::_hasBlanks( const std::string& s ) const
536 {
537  for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ )
538  if ( s[i] == Arg::blankChar() )
539  return true;
540 
541  return false;
542 }
543 
544 inline void Arg::forceRequired()
545 {
546  _required = true;
547 }
548 
549 inline void Arg::xorSet()
550 {
551  _alreadySet = true;
552  _xorSet = true;
553 }
554 
558 inline void Arg::addToList( std::list<Arg*>& argList ) const
559 {
560  argList.push_front( const_cast<Arg*>(this) );
561 }
562 
563 inline bool Arg::allowMore()
564 {
565  return false;
566 }
567 
568 inline bool Arg::acceptsMultipleValues()
569 {
570  return _acceptsMultipleValues;
571 }
572 
574 //END Arg.cpp
576 
577 }; // namespace ecl
578 
579 #endif
580 
bool _alreadySet
Definition: arg.hpp:101
static char flagStartChar()
static char blankChar()
Visitor * _visitor
Definition: arg.hpp:109
std::list< Visitor * >::iterator VisitorListIterator
virtual std::string shortID(const std::string &valueId="val") const
Definition: arg.hpp:396
static bool ignoreRest()
virtual bool isRequired() const
Definition: arg.hpp:465
static void beginIgnoring()
TClap class indirectly used to define the interface for visitors.
Definition: visitor.hpp:25
Virtual parent for all the different argument classes.
Definition: arg.hpp:37
XmlRpcServer s
std::list< Arg * >::iterator ArgListIterator
static std::string nameStartString()
static std::string ignoreNameString()
bool _ignoreable
Definition: arg.hpp:114
const std::string & getName() const
Definition: arg.hpp:463
static std::string flagStartString()
virtual ~Arg()
Definition: arg.hpp:394
bool isSet() const
Definition: arg.hpp:469
static bool & ignoreRestRef()
bool _xorSet
Definition: arg.hpp:120
bool _acceptsMultipleValues
Definition: arg.hpp:122
virtual std::string toString() const
Definition: arg.hpp:493
std::vector< Arg * >::iterator ArgVectorIterator
void forceRequired()
Definition: arg.hpp:544
const std::string & getFlag() const
Definition: arg.hpp:461
void _checkWithVisitor() const
Definition: arg.hpp:505
std::string _name
Definition: arg.hpp:71
virtual std::string longID(const std::string &valueId="val") const
Definition: arg.hpp:417
static void setDelimiter(char c)
virtual bool argMatches(const std::string &s) const
Definition: arg.hpp:484
std::string getDescription() const
Definition: arg.hpp:448
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
virtual bool acceptsMultipleValues()
Definition: arg.hpp:568
bool isIgnoreable() const
Definition: arg.hpp:477
void xorSet()
Definition: arg.hpp:549
const GenericPointer< typename T::ValueType > T2 value
virtual bool operator==(const Arg &a) const
Definition: arg.hpp:440
bool isValueRequired() const
Definition: arg.hpp:467
virtual void addToList(std::list< Arg * > &argList) const
Definition: arg.hpp:558
std::string _requireLabel
Definition: arg.hpp:87
bool _hasBlanks(const std::string &s) const
Definition: arg.hpp:535
static char & delimiterRef()
std::string _flag
Definition: arg.hpp:62
Arg(const std::string &flag, const std::string &name, const std::string &desc, bool req, bool valreq, Visitor *v=NULL)
Definition: arg.hpp:353
std::string _description
Definition: arg.hpp:76
bool _required
Definition: arg.hpp:81
Defines the exception that is thrown whenever a conflict in arguments occurs.
virtual bool allowMore()
Definition: arg.hpp:563
virtual void trimFlag(std::string &flag, std::string &value) const
Definition: arg.hpp:514
static char delimiter()
bool _valueRequired
Definition: arg.hpp:94
virtual void visit()
virtual bool processArg(int *i, std::vector< std::string > &args)=0
void setRequireLabel(const std::string &s)
Definition: arg.hpp:479


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13