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 
99 
108 
113 
117  bool _required;
118 
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
237 
242 #ifndef TCLAP_NAMESTARTSTRING
243 #define TCLAP_NAMESTARTSTRING "--"
244 #endif
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),
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 
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 
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 
bool _alreadySet
Definition: Arg.h:137
bool _required
Definition: Arg.h:117
bool _valueRequired
Definition: Arg.h:130
std::istringstream istringstream
Definition: Arg.h:43
Definition: Arg.h:64
GLuint const GLchar * name
virtual void trimFlag(std::string &flag, std::string &value) const
Definition: Arg.h:620
GLdouble s
virtual std::string longID(const std::string &valueId="val") const
Definition: Arg.h:523
bool isIgnoreable() const
Definition: Arg.h:583
bool _hasBlanks(const std::string &s) const
Definition: Arg.h:641
std::string getDescription() const
Definition: Arg.h:554
void xorSet()
Definition: Arg.h:655
void SetString(T &dst, const std::string &src)
GLfloat value
void setRequireLabel(const std::string &s)
Definition: Arg.h:585
static char flagStartChar()
Definition: Arg.h:226
virtual void addToList(std::list< Arg * > &argList) const
Definition: Arg.h:664
GLsizei const GLchar *const * string
void _checkWithVisitor() const
Definition: Arg.h:611
virtual void reset()
Definition: Arg.h:679
static char & delimiterRef()
Definition: Arg.h:86
bool _xorSet
Definition: Arg.h:156
static const textual_icon stop
Definition: model-views.h:225
GLenum GLuint id
const std::string & getName() const
Definition: Arg.h:569
static char delimiter()
Definition: Arg.h:211
bool isValueRequired() const
Definition: Arg.h:573
GLboolean GLboolean GLboolean GLboolean a
virtual ~Arg()
Definition: Arg.h:503
std::list< Visitor * >::iterator VisitorListIterator
Definition: Arg.h:406
#define TCLAP_FLAGSTARTCHAR
Definition: Arg.h:224
const GLubyte * c
Definition: glext.h:12690
const std::string & getFlag() const
Definition: Arg.h:567
Visitor * _visitor
Definition: Arg.h:145
Arg & operator=(const Arg &rhs)
bool isSet() const
Definition: Arg.h:575
static const std::string ignoreNameString()
Definition: Arg.h:250
static const std::string nameStartString()
Definition: Arg.h:245
#define TCLAP_NAMESTARTSTRING
Definition: Arg.h:243
virtual std::string shortID(const std::string &valueId="val") const
Definition: Arg.h:505
virtual bool operator==(const Arg &a) const
Definition: Arg.h:546
std::string _description
Definition: Arg.h:112
std::list< Arg * >::iterator ArgListIterator
Definition: Arg.h:396
virtual bool acceptsMultipleValues()
Definition: Arg.h:674
bool _ignoreable
Definition: Arg.h:150
std::vector< Arg * >::iterator ArgVectorIterator
Definition: Arg.h:401
virtual bool processArg(int *i, std::vector< std::string > &args)=0
static void beginIgnoring()
Definition: Arg.h:200
static bool ignoreRest()
Definition: Arg.h:205
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:415
virtual bool argMatches(const std::string &s) const
Definition: Arg.h:590
virtual void visit()
Definition: Visitor.h:48
std::string _name
Definition: Arg.h:107
static char blankChar()
Definition: Arg.h:217
#define NULL
Definition: tinycthread.c:47
int i
std::string _flag
Definition: Arg.h:98
bool _acceptsMultipleValues
Definition: Arg.h:158
static bool & ignoreRestRef()
Definition: Arg.h:80
void forceRequired()
Definition: Arg.h:650
Definition: Arg.h:57
virtual bool allowMore()
Definition: Arg.h:669
virtual std::string toString() const
Definition: Arg.h:599
#define TCLAP_FLAGSTARTSTRING
Definition: Arg.h:234
Arg(const Arg &rhs)
std::string _requireLabel
Definition: Arg.h:123
GLdouble v
static const std::string flagStartString()
Definition: Arg.h:236
static void setDelimiter(char c)
Definition: Arg.h:256
virtual bool isRequired() const
Definition: Arg.h:571


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:45:06