VR-Link API Documentation for HLA 1516
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmdMultiArg.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * file: MultiArg.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  *****************************************************************************/
21 
22 
23 #ifndef DtCmdLine_MULTIPLE_ARGUMENT_H
24 #define DtCmdLine_MULTIPLE_ARGUMENT_H
25 
29 
30 #include <cstdio>
31 
32 #include <string>
33 #include <vector>
34 
35 #include "cmdArg.h"
36 
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #else
40 #define HAVE_SSTREAM
41 
42 #endif
43 
44 #if defined(HAVE_SSTREAM)
45 #include <sstream>
46 #elif defined(HAVE_STRSTREAM)
47 #include <strstream>
48 #else
49 #error "Need a stringstream (sstream or strstream) to compile!"
50 #endif
51 
52 #ifdef DtUSE_UTILITIES_NAMESPACE
53 namespace DtUSE_UTILITIES_NAMESPACE
54 {
55 #endif
56 
57 namespace DtCmdLine {
58 
59  template<class T> class MultiArg;
60 
61  namespace MULTI_ARG_HELPER {
62 
64 
74  template<class T>
77  {
78  friend class MultiArg<T>;
79 
80  private:
81 
86  std::vector<T> &_values;
87 
92  ValueExtractor(std::vector<T> &values) : _values(values) {}
93 
99  int extractValue( const std::string& val )
100  {
101  T temp;
102 
103 #if defined(HAVE_SSTREAM)
104  std::istringstream is(val);
105 #elif defined(HAVE_STRSTREAM)
106  std::istrstream is(val.c_str());
107 #else
108 #error "Need a stringstream (sstream or strstream) to compile!"
109 #endif
110 
111  int valuesRead = 0;
112 
113  while ( is.good() )
114  {
115  if ( is.peek() != EOF )
116  is >> temp;
117  else
118  break;
119 
120  valuesRead++;
121  }
122 
123  if ( is.fail() )
124  return EXTRACT_FAILURE;
125 
126  if ( valuesRead > 1 )
127  return EXTRACT_TOO_MANY;
128 
129  _values.push_back(temp);
130 
131  return 0;
132  }
133  };
134 
140  template<>
141  class ValueExtractor<std::string>
142  {
143  friend class MultiArg<std::string>;
144 
145  private:
146 
151  std::vector<std::string> &_values;
152 
157  ValueExtractor(std::vector<std::string> &values) : _values(values) {}
158 
164  int extractValue( const std::string& val )
165  {
166  _values.push_back( val );
167  return 0;
168  }
169  };
170 
176  template<>
178  {
179  friend class MultiArg<DtString>;
180 
181  private:
182 
187  std::vector<DtString>& _values;
188 
193  ValueExtractor(std::vector<DtString>& values) : _values(values) {}
194 
200  int extractValue(const DtString& val)
201  {
202  _values.push_back(val);
203  return 0;
204  }
205  };
206 
207 
208  } //namespace MULTI_ARG_HELPER
209 
215  template<class T>
217  class MultiArg : public Arg
218  {
219  protected:
220 
224  std::vector<T> _values;
225 
232  std::vector<T> _allowed;
233 
237  std::string _typeDesc;
238 
245  void _extractValue( const std::string& val );
246 
251  void _checkAllowed( const std::string& val );
252 
253  public:
254 
272  MultiArg( const std::string& flag,
273  const std::string& name,
274  const std::string& desc,
275  bool req,
276  const std::string& typeDesc,
277  Visitor* v = NULL);
278 
297  MultiArg( const std::string& flag,
298  const std::string& name,
299  const std::string& desc,
300  bool req,
301  const std::string& typeDesc,
302  CmdLineInterface& parser,
303  Visitor* v = NULL );
304 
320  MultiArg( const std::string& flag,
321  const std::string& name,
322  const std::string& desc,
323  bool req,
324  const std::vector<T>& allowed,
325  Visitor* v = NULL );
326 
343  MultiArg( const std::string& flag,
344  const std::string& name,
345  const std::string& desc,
346  bool req,
347  const std::vector<T>& allowed,
348  CmdLineInterface& parser,
349  Visitor* v = NULL );
350 
359  virtual bool processArg(int* i, std::vector<std::string>& args);
360 
365  const std::vector<T>& getValue() const;
366 
371  virtual std::string shortID(const std::string& val="val") const;
372 
377  virtual std::string longID(const std::string& val="val") const;
378 
383  virtual bool isRequired() const;
384 
385  private:
386 
390  void allowedInit();
391  };
392 
396  template<class T>
398  {
399  for ( unsigned int i = 0; i < _allowed.size(); i++ )
400  {
401 
402 #if defined(HAVE_SSTREAM)
403  std::ostringstream os;
404 #elif defined(HAVE_STRSTREAM)
405  std::ostrstream os;
406 #else
407 #error "Need a stringstream (sstream or strstream) to compile!"
408 #endif
409 
410  os << _allowed[i];
411 
412  std::string temp( os.str() );
413 
414  if ( i > 0 )
415  _typeDesc += "|";
416 
417  _typeDesc += temp;
418  }
419  }
420 
424  template<class T>
425  MultiArg<T>::MultiArg(const std::string& flag,
426  const std::string& name,
427  const std::string& desc,
428  bool req,
429  const std::string& typeDesc,
430  Visitor* v)
431  : Arg( flag, name, desc, req, true, v ),
432  _typeDesc( typeDesc )
433  { }
434 
435  template<class T>
436  MultiArg<T>::MultiArg(const std::string& flag,
437  const std::string& name,
438  const std::string& desc,
439  bool req,
440  const std::string& typeDesc,
441  CmdLineInterface& parser,
442  Visitor* v)
443  : Arg( flag, name, desc, req, true, v ),
444  _typeDesc( typeDesc )
445  {
446  parser.add( this );
447  }
448 
452  template<class T>
453  MultiArg<T>::MultiArg(const std::string& flag,
454  const std::string& name,
455  const std::string& desc,
456  bool req,
457  const std::vector<T>& allowed,
458  Visitor* v)
459  : Arg( flag, name, desc, req, true, v ),
460  _allowed( allowed )
461  {
462  allowedInit();
463  }
464 
465  template<class T>
466  MultiArg<T>::MultiArg(const std::string& flag,
467  const std::string& name,
468  const std::string& desc,
469  bool req,
470  const std::vector<T>& allowed,
471  CmdLineInterface& parser,
472  Visitor* v)
473  : Arg( flag, name, desc, req, true, v ),
474  _allowed( allowed )
475  {
476  allowedInit();
477  parser.add( this );
478  }
479 
483  template<class T>
484  const std::vector<T>& MultiArg<T>::getValue() const { return _values; }
485 
489  template<class T>
490  bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
491  {
492  if ( _ignoreable && Arg::ignoreRest() )
493  return false;
494 
495  if ( _hasBlanks( args[*i] ) )
496  return false;
497 
498  std::string flag = args[*i];
499  std::string value = "";
500 
501  trimFlag( flag, value );
502 
503  if ( argMatches( flag ) )
504  {
505  if ( Arg::delimiter() != ' ' && value == "" )
506  throw( ArgParseException(
507  "Couldn't find delimiter for this argument!",
508  toString() ) );
509 
510  if ( value == "" )
511  {
512  (*i)++;
513  if ( (unsigned int)*i < args.size() )
514  _extractValue( args[*i] );
515  else
516  throw( ArgParseException("Missing a value for this argument!",
517  toString() ) );
518  }
519  else
520  _extractValue( value );
521 
522  _checkWithVisitor();
523 
524  return true;
525  }
526  else
527  return false;
528  }
529 
533  template<class T>
534  void MultiArg<T>::_checkAllowed( const std::string& val )
535  {
536  if ( _allowed.size() > 0 )
537  if ( std::find(_allowed.begin(),_allowed.end(),_values.back())
538  == _allowed.end() )
539  throw( CmdLineParseException( "Couldn't find '" + val +
540  "' in allowed list.", toString() ) );
541  }
542 
546  template<class T>
547  std::string MultiArg<T>::shortID(const std::string& val) const
548  {
549  std::string id = Arg::shortID(_typeDesc) + " ... ";
550 
551  return id;
552  }
553 
557  template<class T>
558  std::string MultiArg<T>::longID(const std::string& val) const
559  {
560  std::string id = Arg::longID(_typeDesc) + " (accepted multiple times)";
561 
562  return id;
563  }
564 
569  template<class T>
571  {
572  if ( _required )
573  {
574  if ( _values.size() > 1 )
575  return false;
576  else
577  return true;
578  }
579  else
580  return false;
581 
582  }
583 
584  template<class T>
585  void MultiArg<T>::_extractValue( const std::string& val )
586  {
588 
589  int err = ve.extractValue(val);
590 
592  throw( ArgParseException("Couldn't read argument value "
593  "from string '" + val + "'", toString() ) );
594 
596  throw( ArgParseException("More than one valid value "
597  "parsed from string '" + val + "'",
598  toString() ) );
599  _checkAllowed( val );
600  }
601 
602 
603 }
604 
605 #ifdef DtUSE_UTILITIES_NAMESPACE
606 }
607 #endif
608 
609 #endif
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
Definition: cmdMultiArg.h:558
A base class that defines the interface for visitors.
Definition: cmdVisitor.h:41
int extractValue(const std::string &val)
Method that will attempt to parse the input stream for values of type std::string.
Definition: cmdMultiArg.h:164
int extractValue(const std::string &val)
Method that will attempt to parse the input stream for values of type T.
Definition: cmdMultiArg.h:99
This file declares classes for VR-Link command line parsing utility.
ValueExtractor(std::vector< DtString > &values)
Constructor.
Definition: cmdMultiArg.h:193
Error_e
Definition: cmdMultiArg.h:63
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition: cmdMultiArg.h:224
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: cmdArg.h:432
int extractValue(const DtString &val)
Method that will attempt to parse the input stream for values of type std::string.
Definition: cmdMultiArg.h:200
std::vector< T > _allowed
A list of allowed values.
Definition: cmdMultiArg.h:232
void _checkAllowed(const std::string &val)
Checks to see if parsed value is in allowed list.
Definition: cmdMultiArg.h:534
This class is used to extract a value from an argument.
Definition: cmdMultiArg.h:76
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: cmdArg.h:184
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: cmdArgException.h:153
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: cmdArgException.h:131
MultiArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, Visitor *v=NULL)
Constructor.
Definition: cmdMultiArg.h:425
std::string _typeDesc
The description of type T to be used in the usage.
Definition: cmdMultiArg.h:237
const std::vector< T > & getValue() const
Returns a vector of type T containing the values parsed from the command line.
Definition: cmdMultiArg.h:484
static bool ignoreRest()
Whether to ignore the rest.
Definition: cmdArg.h:178
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
Definition: cmdMultiArg.h:547
virtual bool isRequired() const
Once we&#39;ve matched the first value, then the arg is no longer required.
Definition: cmdMultiArg.h:570
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: cmdArg.h:411
A virtual base class that defines the essential data for all arguments.
Definition: cmdArg.h:48
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: cmdMultiArg.h:490
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: cmdLineInterface.h:52
An argument that allows multiple values of type T to be specified.
Definition: cmdMultiArg.h:59
void allowedInit()
Common initialization code for constructors with allowed vectors.
Definition: cmdMultiArg.h:397
Instances of DtString are used to represent strings.
Definition: vlString.h:36
ValueExtractor(std::vector< T > &values)
Constructor.
Definition: cmdMultiArg.h:92
std::vector< DtString > & _values
Reference to the vector of strings where the result of the extraction will be put.
Definition: cmdMultiArg.h:187
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: cmdMultiArg.h:585
std::vector< T > & _values
Reference to the vector of values where the result of the extraction will be put. ...
Definition: cmdMultiArg.h:86

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)