value_arg.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef TCLAP_VALUE_ARGUMENT_H
16 #define TCLAP_VALUE_ARGUMENT_H
17 
18 #include <string>
19 #include <vector>
20 #include <cstdio>
21 #include "arg.hpp"
22 #include "constraint.hpp"
23 
24 #define HAVE_SSTREAM
25 
26 #if defined(HAVE_SSTREAM)
27 #include <sstream>
28 #elif defined(HAVE_STRSTREAM)
29 #include <strstream>
30 #else
31 #error "Need a stringstream (sstream or strstream) to compile!"
32 #endif
33 
34 namespace ecl {
35 
36 template<class T> class ValueArg;
37 
38 namespace ValueArgHelper {
39 
41 
53 template<class T> class ValueExtractor
54 {
55  friend class ValueArg<T>;
56 
57  private:
58 
63  T &_value;
64 
69  ValueExtractor(T &value) : _value(value) { }
70 
76  int extractValue( const std::string& val )
77  {
78 
79 #if defined(HAVE_SSTREAM)
80  std::istringstream is(val);
81 #elif defined(HAVE_STRSTREAM)
82  std::istrstream is(val.c_str());
83 #else
84 #error "Need a stringstream (sstream or strstream) to compile!"
85 #endif
86 
87  int valuesRead = 0;
88  while ( is.good() )
89  {
90  if ( is.peek() != EOF )
91  is >> _value;
92  else
93  break;
94 
95  valuesRead++;
96  }
97 
98  if ( is.fail() )
99  return EXTRACT_FAILURE;
100 
101  if ( valuesRead > 1 )
102  return EXTRACT_TOO_MANY;
103 
104  return 0;
105  }
106 };
107 
115 template<> class ValueExtractor<std::string>
116 {
117  friend class ValueArg<std::string>;
118 
119  private:
120 
125  std::string &_value;
126 
131  ValueExtractor(std::string &value) : _value(value) {}
132 
138  int extractValue( const std::string& val )
139  {
140  _value = val;
141  return 0;
142  }
143 };
144 
145 } //namespace ValueArgHelper
146 
155 template<class T>
156 class ValueArg : public Arg
157 {
158  protected:
159 
166 
174  std::string _typeDesc;
175 
180 
187  void _extractValue( const std::string& val );
188 
189  public:
190 
214  ValueArg( const std::string& flag,
215  const std::string& name,
216  const std::string& desc,
217  bool req,
218  T value,
219  const std::string& typeDesc,
220  Visitor* v = NULL);
221 
222 
247  ValueArg( const std::string& flag,
248  const std::string& name,
249  const std::string& desc,
250  bool req,
251  T value,
252  const std::string& typeDesc,
253  CmdLineInterface& parser,
254  Visitor* v = NULL );
255 
278  ValueArg( const std::string& flag,
279  const std::string& name,
280  const std::string& desc,
281  bool req,
282  T value,
283  Constraint<T>* constraint,
284  CmdLineInterface& parser,
285  Visitor* v = NULL );
286 
308  ValueArg( const std::string& flag,
309  const std::string& name,
310  const std::string& desc,
311  bool req,
312  T value,
313  Constraint<T>* constraint,
314  Visitor* v = NULL );
315 
325  virtual bool processArg(int* i, std::vector<std::string>& args);
326 
330  T& getValue() ;
331 
336  virtual std::string shortID(const std::string& val = "val") const;
337 
342  virtual std::string longID(const std::string& val = "val") const;
343 
344 };
345 
346 
350 template<class T>
351 ValueArg<T>::ValueArg(const std::string& flag,
352  const std::string& name,
353  const std::string& desc,
354  bool req,
355  T value,
356  const std::string& typeDesc,
357  Visitor* v)
358 : Arg(flag, name, desc, req, true, v),
359  _value( value ),
360  _typeDesc( typeDesc ),
361  _constraint( NULL )
362 { }
363 
364 template<class T>
365 ValueArg<T>::ValueArg(const std::string& flag,
366  const std::string& name,
367  const std::string& desc,
368  bool req,
369  T val,
370  const std::string& typeDesc,
371  CmdLineInterface& parser,
372  Visitor* v)
373 : Arg(flag, name, desc, req, true, v),
374  _value( val ),
375  _typeDesc( typeDesc ),
376  _constraint( NULL )
377 {
378  parser.add( this );
379 }
380 
381 template<class T>
382 ValueArg<T>::ValueArg(const std::string& flag,
383  const std::string& name,
384  const std::string& desc,
385  bool req,
386  T val,
387  Constraint<T>* constraint,
388  Visitor* v)
389 : Arg(flag, name, desc, req, true, v),
390  _value( val ),
391  _typeDesc( constraint->shortID() ),
392  _constraint( constraint )
393 { }
394 
395 template<class T>
396 ValueArg<T>::ValueArg(const std::string& flag,
397  const std::string& name,
398  const std::string& desc,
399  bool req,
400  T val,
401  Constraint<T>* constraint,
402  CmdLineInterface& parser,
403  Visitor* v)
404 : Arg(flag, name, desc, req, true, v),
405  _value( val ),
406  _typeDesc( constraint->shortID() ),
407  _constraint( constraint )
408 {
409  parser.add( this );
410 }
411 
412 
416 template<class T>
417 T& ValueArg<T>::getValue() { return _value; }
418 
422 template<class T>
423 bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
424 {
425  if ( _ignoreable && Arg::ignoreRest() )
426  return false;
427 
428  if ( _hasBlanks( args[*i] ) )
429  return false;
430 
431  std::string flag = args[*i];
432 
433  std::string value = "";
434  trimFlag( flag, value );
435 
436  if ( argMatches( flag ) )
437  {
438  if ( _alreadySet )
439  throw( CmdLineParseException("Argument already set!", toString()) );
440 
441  if ( Arg::delimiter() != ' ' && value == "" )
442  throw( ArgParseException(
443  "Couldn't find delimiter for this argument!",
444  toString() ) );
445 
446  if ( value == "" )
447  {
448  (*i)++;
449  if ( static_cast<unsigned int>(*i) < args.size() )
450  _extractValue( args[*i] );
451  else
452  throw( ArgParseException("Missing a value for this argument!",
453  toString() ) );
454  }
455  else
456  _extractValue( value );
457 
458  _alreadySet = true;
459  _checkWithVisitor();
460  return true;
461  }
462  else
463  return false;
464 }
465 
469 template<class T>
470 std::string ValueArg<T>::shortID(const std::string& val) const
471 {
472  std::string stop_warnings = val; // Only to stop annoying warning messages, I dont think it slows things down
473  return Arg::shortID( _typeDesc );
474 }
475 
479 template<class T>
480 std::string ValueArg<T>::longID(const std::string& val) const
481 {
482  std::string stop_warnings = val; // Only to stop annoying warning messages, I dont think it slows things down
483  return Arg::longID( _typeDesc );
484 }
485 
486 template<class T> // Just to stop the warnings.
487 void ValueArg<T>::_extractValue( const std::string& val )
488 {
490 
491  int err = ve.extractValue(val);
492 
493  if ( err == ValueArgHelper::EXTRACT_FAILURE )
494  throw( ArgParseException("Couldn't read argument value from string '" +
495  val + "'", toString() ) );
496 
498  throw( ArgParseException(
499  "More than one valid value parsed from string '" +
500  val + "'", toString() ) );
501 
502  if ( _constraint != NULL )
503  if ( ! _constraint->check( _value ) )
504  throw( CmdLineParseException( "Value '" + val +
505  "' does not meet constraint: " +
506  _constraint->description(),
507  toString() ) );
508 }
509 
510 }; // namespace ecl
511 
512 #endif
arg.hpp
TCLAP command line argument parser classes.
ecl::ValueArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Definition: value_arg.hpp:423
ecl::ValueArgHelper::ValueExtractor::ValueExtractor
ValueExtractor(T &value)
Definition: value_arg.hpp:69
ecl::ValueArg::shortID
virtual std::string shortID(const std::string &val="val") const
Definition: value_arg.hpp:470
ecl::Arg::shortID
virtual std::string shortID(const std::string &valueId="val") const
Definition: arg.hpp:398
ecl::ValueArgHelper::EXTRACT_FAILURE
@ EXTRACT_FAILURE
Definition: value_arg.hpp:40
ecl::ValueArg::_constraint
Constraint< T > * _constraint
Definition: value_arg.hpp:179
ecl::Arg::delimiter
static char delimiter()
Definition: arg.hpp:179
ecl::CmdLineInterface::add
virtual void add(Arg &a)=0
ecl::ValueArg::getValue
T & getValue()
Definition: value_arg.hpp:417
ecl::Arg
Virtual parent for all the different argument classes.
Definition: arg.hpp:39
ecl::ValueArg::_value
T _value
Definition: value_arg.hpp:165
ecl::ArgParseException
Defines the exception that is thrown when an argument is improperly parsed.
Definition: arg_exception.hpp:119
ecl::ValueArg::_typeDesc
std::string _typeDesc
Definition: value_arg.hpp:174
ecl::ValueArgHelper::ValueExtractor
TClap class internally used for extracting value args.
Definition: value_arg.hpp:53
ecl::ValueArgHelper::EXTRACT_TOO_MANY
@ EXTRACT_TOO_MANY
Definition: value_arg.hpp:40
ecl::Visitor
TClap class indirectly used to define the interface for visitors.
Definition: visitor.hpp:27
ecl::ValueArgHelper::ValueExtractor::_value
T & _value
Definition: value_arg.hpp:63
ecl::CmdLineInterface
Managing interface for The base class that manages the command line definition and passes along the p...
Definition: cmd_line_interface.hpp:38
ecl::Arg::ignoreRest
static bool ignoreRest()
Definition: arg.hpp:173
constraint.hpp
TCLAP command line argument parser classes.
ecl::ValueArg
Definition: value_arg.hpp:36
ecl::ValueArg::ValueArg
ValueArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, T value, const std::string &typeDesc, Visitor *v=NULL)
Definition: value_arg.hpp:351
ecl::Constraint
Defines the interaction between an argument and a constraint.
Definition: constraint.hpp:36
ecl::ValueArg::longID
virtual std::string longID(const std::string &val="val") const
Definition: value_arg.hpp:480
ecl::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Definition: arg.hpp:419
ecl::CmdLineParseException
Defines the exception when an argument is improperly specified.
Definition: arg_exception.hpp:143
ecl::ValueArgHelper::ValueExtractor::extractValue
int extractValue(const std::string &val)
Definition: value_arg.hpp:76
ecl::ValueArgHelper::ValueExtractor< std::string >::extractValue
int extractValue(const std::string &val)
Definition: value_arg.hpp:138
ecl::ValueArg::_extractValue
void _extractValue(const std::string &val)
Definition: value_arg.hpp:487
ecl
ecl::ValueArgHelper::Error_e
Error_e
Definition: value_arg.hpp:40


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