VR-Link API Documentation for HLA 1516
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmdArg.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * file: Arg.h
4  *
5  * Copyright (c) 2003, Michael E. Smoot .
6  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno .
7  * All rights reverved.
8  *
9  * See the file COPYING in the top directory of this distribution for
10  * more information.
11  *
12  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
13  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18  * DEALINGS IN THE SOFTWARE.
19  *
20  *****************************************************************************/
24 
25 #ifndef DtCmdLine_ARGUMENT_H
26 #define DtCmdLine_ARGUMENT_H
27 
28 #include <string>
29 #include <vector>
30 #include <list>
31 #include <iostream>
32 
33 #include "cmdArgException.h"
34 #include "cmdVisitor.h"
35 #include "cmdLineInterface.h"
36 
37 #ifdef DtUSE_UTILITIES_NAMESPACE
38 namespace DtUSE_UTILITIES_NAMESPACE
39 {
40 #endif
41 
42 namespace DtCmdLine {
43 
48  class Arg
49  {
50  private:
51 
55  static bool& ignoreRestRef() { static bool ign = false; return ign; }
56 
61  static char& delimiterRef() { static char delim = ' '; return delim; }
62 
63  protected:
64 
73  std::string _flag;
74 
82  std::string _name;
83 
87  std::string _description;
88 
92  bool _required;
93 
98  std::string _requireLabel;
99 
106 
113 
121 
126 
131  bool _xorSet;
132 
136  void _checkWithVisitor() const;
137 
151  Arg( const std::string& flag,
152  const std::string& name,
153  const std::string& desc,
154  bool req,
155  bool valreq,
156  Visitor* v = NULL );
157 
158  public:
162  virtual ~Arg();
163 
168  virtual void addToList( std::list<Arg*>& argList ) const;
169 
173  static void beginIgnoring() { ignoreRestRef() = true; }
174 
178  static bool ignoreRest() { return ignoreRestRef(); }
179 
184  static char delimiter() { return delimiterRef(); }
185 
191  static const char blankChar() { return '*'; }
192 
196  static const char flagStartChar() { return '-'; }
197 
202  static const std::string flagStartString() { return "-"; }
203 
208  static const std::string nameStartString() { return "--"; }
209 
213  static const std::string ignoreNameString() { return "ignore_rest"; }
214 
219  static void setDelimiter( char c ) { delimiterRef() = c; }
220 
228  virtual bool processArg(int *i, std::vector<std::string>& args) = 0;
229 
235  virtual bool operator==(const Arg& a);
236 
242  virtual bool operator!=(const Arg& a);
243 
247  const std::string& getFlag() const;
248 
252  const std::string& getName() const;
253 
257  std::string getDescription() const;
258 
262  virtual bool isRequired() const;
263 
268  void forceRequired();
269 
274  void xorSet();
275 
279  bool isValueRequired() const;
280 
285  bool isSet() const;
286 
287  bool isAlreadySet() const;
288  bool isXorSet() const;
289 
293  bool isIgnoreable() const;
294 
303  virtual bool argMatches( const std::string& s ) const;
304 
309  virtual std::string toString() const;
310 
315  virtual std::string shortID( const std::string& valueId = "val" ) const;
316 
321  virtual std::string longID( const std::string& valueId = "val" ) const;
322 
330  virtual void trimFlag( std::string& flag, std::string& value ) const;
331 
338  bool _hasBlanks( const std::string& s ) const;
339 
345  void setRequireLabel( const std::string& s );
346 
347  };
348 
352  typedef std::list<Arg*>::iterator ArgListIterator;
353 
357  typedef std::vector<Arg*>::iterator ArgVectorIterator;
358 
362  typedef std::list<Visitor*>::iterator VisitorListIterator;
363 
364 
366  //BEGIN Arg.cpp
368 
369  inline Arg::Arg(const std::string& flag,
370  const std::string& name,
371  const std::string& desc,
372  bool req,
373  bool valreq,
374  Visitor* v) :
375  _flag(flag),
376  _name(name),
377  _description(desc),
378  _required(req),
379  _requireLabel("required"),
380  _valueRequired(valreq),
381  _alreadySet(false),
382  _visitor( v ),
383  _ignoreable(true),
384  _xorSet(false)
385  {
386  if ( _flag.length() > 1 )
388  "Argument flag can only be one character long", toString() ) );
389 
390  if ( _name != ignoreNameString() &&
391  ( _flag == Arg::flagStartString() ||
392  _flag == Arg::nameStartString() ||
393  _flag == " " ) )
394  throw(SpecificationException("Argument flag cannot be either '" +
395  Arg::flagStartString() + "' or '" +
396  Arg::nameStartString() + "' or a space.",
397  toString() ) );
398 
399  if ( ( _name.find( Arg::flagStartString(), 0 ) != std::string::npos ) ||
400  ( _name.find( Arg::nameStartString(), 0 ) != std::string::npos ) ||
401  ( _name.find( " ", 0 ) != std::string::npos ) )
402  throw(SpecificationException("Argument name cannot contain either '" +
403  Arg::flagStartString() + "' or '" +
404  Arg::nameStartString() + "' or space.",
405  toString() ) );
406 
407  }
408 
409  inline Arg::~Arg() { }
410 
411  inline std::string Arg::shortID( const std::string& valueId ) const
412  {
413  std::string id = "";
414 
415  if ( _flag != "" )
416  id = Arg::flagStartString() + _flag;
417  else
418  id = Arg::nameStartString() + _name;
419 
420  std::string delim = " ";
421  delim[0] = Arg::delimiter();
422 
423  if ( _valueRequired )
424  id += delim + "<" + valueId + ">";
425 
426  if ( !_required )
427  id = "[" + id + "]";
428 
429  return id;
430  }
431 
432  inline std::string Arg::longID( const std::string& valueId ) const
433  {
434  std::string id = "";
435 
436  if ( _flag != "" )
437  {
438  id += Arg::flagStartString() + _flag;
439 
440  if ( _valueRequired )
441  id += " <" + valueId + ">";
442 
443  id += ", ";
444  }
445 
446  id += Arg::nameStartString() + _name;
447 
448  if ( _valueRequired )
449  id += " <" + valueId + ">";
450 
451  return id;
452 
453  }
454 
455  inline bool Arg::operator==(const Arg& a)
456  {
457  if ( ( _flag != "" && _flag == a._flag ) ||
458  (_name == a._name))
459  {
460  return true;
461  }
462  else
463  {
464  return false;
465  }
466  }
467 
468  inline bool Arg::operator!=(const Arg& a)
469  {
470  return !(*this == a);
471  }
472 
473  inline std::string Arg::getDescription() const
474  {
475  std::string desc = "";
476  if ( _required )
477  desc = "(" + _requireLabel + ") ";
478 
479  //if ( _valueRequired )
481 
482  desc += _description;
483  return desc;
484  }
485 
486  inline const std::string& Arg::getFlag() const { return _flag; }
487 
488  inline const std::string& Arg::getName() const { return _name; }
489 
490  inline bool Arg::isRequired() const { return _required; }
491 
492  inline bool Arg::isValueRequired() const { return _valueRequired; }
493 
494  inline bool Arg::isSet() const
495  {
496  if ( _alreadySet && !_xorSet )
497  return true;
498  else
499  return false;
500  }
501 
502  inline bool Arg::isAlreadySet() const { return _alreadySet; }
503  inline bool Arg::isXorSet() const { return _xorSet; }
504 
505 
506  inline bool Arg::isIgnoreable() const { return _ignoreable; }
507 
508  inline void Arg::setRequireLabel( const std::string& s)
509  {
510  _requireLabel = s;
511  }
512 
513  inline bool Arg::argMatches( const std::string& argFlag ) const
514  {
515  if ( ( argFlag == Arg::flagStartString() + _flag && _flag != "" ) ||
516  argFlag == Arg::nameStartString() + _name )
517  return true;
518  else
519  return false;
520  }
521 
522  inline std::string Arg::toString() const
523  {
524  std::string s = "";
525 
526  if ( _flag != "" )
527  s += Arg::flagStartString() + _flag + " ";
528 
529  s += "(" + Arg::nameStartString() + _name + ")";
530 
531  return s;
532 }
533 
534 inline void Arg::_checkWithVisitor() const
535 {
536  if ( _visitor != NULL )
537  _visitor->visit();
538 }
539 
543 inline void Arg::trimFlag(std::string& flag, std::string& value) const
544 {
545  int stop = 0;
546  for ( int i = 0; (unsigned int)i < flag.length(); i++ )
547  if ( flag[i] == Arg::delimiter() )
548  {
549  stop = i;
550  break;
551  }
552 
553  if ( stop > 1 )
554  {
555  value = flag.substr(stop+1);
556  flag = flag.substr(0,stop);
557  }
558 
559 }
560 
564 inline bool Arg::_hasBlanks( const std::string& s ) const
565 {
566  for ( int i = 1; (unsigned int)i < s.length(); i++ )
567  if ( s[i] == Arg::blankChar() )
568  return true;
569 
570  return false;
571 }
572 
573 inline void Arg::forceRequired()
574 {
575  _required = true;
576 }
577 
578 inline void Arg::xorSet()
579 {
580  _alreadySet = true;
581  _xorSet = true;
582 }
583 
587 inline void Arg::addToList( std::list<Arg*>& argList ) const
588 {
589  argList.push_front( (Arg*)this );
590 }
591 
593 //END Arg.cpp
595 
596 } //namespace DtCmdLine
597 
598 #ifdef DtUSE_UTILITIES_NAMESPACE
599 }
600 #endif
601 
602 #endif
603 
virtual bool operator==(const Arg &a)
Operator ==.
Definition: cmdArg.h:455
bool isXorSet() const
Definition: cmdArg.h:503
virtual ~Arg()
Destructor.
Definition: cmdArg.h:409
bool isSet() const
Indicates whether the argument has already been set.
Definition: cmdArg.h:494
A base class that defines the interface for visitors.
Definition: cmdVisitor.h:41
const bool operator==(const DtU128 &lhs, const DtU128 &rhs)
bool _valueRequired
Indicates whether a value is required for the argument.
Definition: cmdArg.h:105
static void setDelimiter(char c)
Sets the delimiter for all arguments.
Definition: cmdArg.h:219
static const char blankChar()
The char used as a place holder when SwitchArgs are combined.
Definition: cmdArg.h:191
Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
Definition: cmdArgException.h:177
This file declares the class DtCmdLine::CmdLineInterface.
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: cmdArg.h:125
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: cmdArg.h:490
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
Definition: cmdArg.h:564
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: cmdArg.h:432
bool _alreadySet
Indicates whether the argument has been set.
Definition: cmdArg.h:112
static bool & ignoreRestRef()
Indicates whether the rest of the arguments should be ignored.
Definition: cmdArg.h:55
static const std::string flagStartString()
The sting that indicates the beginning of a flag.
Definition: cmdArg.h:202
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: cmdArg.h:184
static const char flagStartChar()
The char that indicates the beginning of a flag.
Definition: cmdArg.h:196
void forceRequired()
Sets _required to true.
Definition: cmdArg.h:573
std::string getDescription() const
Returns the argument description.
Definition: cmdArg.h:473
const std::string & getName() const
Returns the argument name.
Definition: cmdArg.h:488
bool _xorSet
Indicates that the arg was set as part of an XOR and not on the command line.
Definition: cmdArg.h:131
virtual void addToList(std::list< Arg * > &argList) const
Adds this to the specified list of Args.
Definition: cmdArg.h:587
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: cmdArg.h:522
std::list< Visitor * >::iterator VisitorListIterator
Typedef of a Visitor list iterator.
Definition: cmdArg.h:362
bool _required
Indicating whether the argument is required.
Definition: cmdArg.h:92
void xorSet()
Sets the _alreadySet value to true.
Definition: cmdArg.h:578
static const std::string nameStartString()
The sting that indicates the beginning of a name.
Definition: cmdArg.h:208
static bool ignoreRest()
Whether to ignore the rest.
Definition: cmdArg.h:178
std::string _description
Description of the argument.
Definition: cmdArg.h:87
std::string _name
A single work namd indentifying the argument.
Definition: cmdArg.h:82
void setRequireLabel(const std::string &s)
Sets the requireLabel.
Definition: cmdArg.h:508
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: cmdArg.h:411
This file declares the class DtCmdLine::Visitor.
A virtual base class that defines the essential data for all arguments.
Definition: cmdArg.h:48
static const std::string ignoreNameString()
The name used to identify the ignore rest argument.
Definition: cmdArg.h:213
static void beginIgnoring()
Begin ignoring arguments since the &quot;--&quot; argument was specified.
Definition: cmdArg.h:173
virtual bool operator!=(const Arg &a)
Operator !=.
Definition: cmdArg.h:468
This file declares the class DtCmdLine::ArgException.
const std::string & getFlag() const
Returns the argument flag.
Definition: cmdArg.h:486
std::list< Arg * >::iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: cmdArg.h:352
const bool operator!=(const DtU128 &lhs, const DtU128 &rhs)
virtual void visit()
Does nothing.
Definition: cmdVisitor.h:55
bool isValueRequired() const
Indicates whether a value must be specified for argument.
Definition: cmdArg.h:492
std::string _flag
The single char flag used to identify the argument.
Definition: cmdArg.h:73
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition: cmdArg.h:543
Visitor * _visitor
A pointer to a vistitor object.
Definition: cmdArg.h:120
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: cmdArg.h:357
bool isAlreadySet() const
Definition: cmdArg.h:502
static char & delimiterRef()
The delimiter that separates an argument flag/name from the value.
Definition: cmdArg.h:61
std::string _requireLabel
Label to be used in usage description.
Definition: cmdArg.h:98
void _checkWithVisitor() const
Performs the special handling described by the Vistitor.
Definition: cmdArg.h:534
bool isIgnoreable() const
Indicates whether the argument can be ignored, if desired.
Definition: cmdArg.h:506
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: cmdArg.h:513

Document ID: Generated on Thu Oct 16 00:12:25 EDT 2025 from SVN revision 280738
Copyright © 1992-2025 MAK Technologies. All Rights Reserved (www.mak.com)