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),
369  _acceptsMultipleValues(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() ||
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 {
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 {
571 }
572 
574 //END Arg.cpp
576 
577 }; // namespace ecl
578 
579 #endif
580 
ecl::Arg::setDelimiter
static void setDelimiter(char c)
Definition: arg.hpp:214
arg_exception.hpp
TCLAP command line argument parser classes.
ecl::Arg::isSet
bool isSet() const
Definition: arg.hpp:471
cmd_line_interface.hpp
TCLAP command line argument parser classes.
ecl::Arg::acceptsMultipleValues
virtual bool acceptsMultipleValues()
Definition: arg.hpp:570
ecl::Arg::_hasBlanks
bool _hasBlanks(const std::string &s) const
Definition: arg.hpp:537
ecl::Arg::getName
const std::string & getName() const
Definition: arg.hpp:465
ecl::ArgListIterator
std::list< Arg * >::iterator ArgListIterator
Definition: arg.hpp:338
ecl::Arg::xorSet
void xorSet()
Definition: arg.hpp:551
ecl::Arg::_checkWithVisitor
void _checkWithVisitor() const
Definition: arg.hpp:507
ecl::Visitor::visit
virtual void visit()
Definition: visitor.hpp:46
ecl::VisitorListIterator
std::list< Visitor * >::iterator VisitorListIterator
Definition: arg.hpp:348
ecl::Arg::shortID
virtual std::string shortID(const std::string &valueId="val") const
Definition: arg.hpp:398
visitor.hpp
TCLAP command line argument parser classes.
ecl::ArgVectorIterator
std::vector< Arg * >::iterator ArgVectorIterator
Definition: arg.hpp:343
ecl::Arg::_requireLabel
std::string _requireLabel
Definition: arg.hpp:91
ecl::Arg::delimiterRef
static char & delimiterRef()
Definition: arg.hpp:54
ecl::Arg::_flag
std::string _flag
Definition: arg.hpp:66
ecl::Arg::Arg
Arg(const std::string &flag, const std::string &name, const std::string &desc, bool req, bool valreq, Visitor *v=NULL)
Definition: arg.hpp:355
ecl::Arg::_description
std::string _description
Definition: arg.hpp:80
ecl::Arg::delimiter
static char delimiter()
Definition: arg.hpp:179
ecl::Arg::getDescription
std::string getDescription() const
Definition: arg.hpp:450
ecl::Arg
Virtual parent for all the different argument classes.
Definition: arg.hpp:39
ecl::Arg::_required
bool _required
Definition: arg.hpp:85
ecl::SpecificationException
Defines the exception that is thrown whenever a conflict in arguments occurs.
Definition: arg_exception.hpp:169
ecl::Arg::argMatches
virtual bool argMatches(const std::string &s) const
Definition: arg.hpp:486
ecl::Arg::allowMore
virtual bool allowMore()
Definition: arg.hpp:565
ecl::Arg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)=0
ecl::Arg::_valueRequired
bool _valueRequired
Definition: arg.hpp:98
ecl::Arg::setRequireLabel
void setRequireLabel(const std::string &s)
Definition: arg.hpp:481
ecl::Arg::beginIgnoring
static void beginIgnoring()
Definition: arg.hpp:168
ecl::Arg::addToList
virtual void addToList(std::list< Arg * > &argList) const
Definition: arg.hpp:560
ecl::Arg::flagStartChar
static char flagStartChar()
Definition: arg.hpp:191
ecl::Arg::blankChar
static char blankChar()
Definition: arg.hpp:186
ecl::Arg::_alreadySet
bool _alreadySet
Definition: arg.hpp:105
ecl::Arg::_visitor
Visitor * _visitor
Definition: arg.hpp:113
ecl::Visitor
TClap class indirectly used to define the interface for visitors.
Definition: visitor.hpp:27
ecl::Arg::isIgnoreable
bool isIgnoreable() const
Definition: arg.hpp:479
ecl::Arg::getFlag
const std::string & getFlag() const
Definition: arg.hpp:463
ecl::Arg::isValueRequired
bool isValueRequired() const
Definition: arg.hpp:469
ecl::Arg::nameStartString
static std::string nameStartString()
Definition: arg.hpp:203
ecl::Arg::ignoreRest
static bool ignoreRest()
Definition: arg.hpp:173
ecl::Arg::trimFlag
virtual void trimFlag(std::string &flag, std::string &value) const
Definition: arg.hpp:516
ecl::Arg::toString
virtual std::string toString() const
Definition: arg.hpp:495
ecl::Arg::flagStartString
static std::string flagStartString()
Definition: arg.hpp:197
ecl::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Definition: arg.hpp:419
ecl::Arg::ignoreNameString
static std::string ignoreNameString()
Definition: arg.hpp:208
ecl::Arg::_ignoreable
bool _ignoreable
Definition: arg.hpp:118
ecl::Arg::operator==
virtual bool operator==(const Arg &a) const
Definition: arg.hpp:442
ecl::Arg::_xorSet
bool _xorSet
Definition: arg.hpp:124
ecl::Arg::isRequired
virtual bool isRequired() const
Definition: arg.hpp:467
ecl::Arg::~Arg
virtual ~Arg()
Definition: arg.hpp:396
ecl::Arg::ignoreRestRef
static bool & ignoreRestRef()
Definition: arg.hpp:48
ecl::Arg::forceRequired
void forceRequired()
Definition: arg.hpp:546
ecl
ecl::Arg::_acceptsMultipleValues
bool _acceptsMultipleValues
Definition: arg.hpp:126
ecl::Arg::_name
std::string _name
Definition: arg.hpp:75


ecl_command_line
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:13