multi_arg.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef TCLAP_MULTIPLE_ARGUMENT_H
16 #define TCLAP_MULTIPLE_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 MultiArg;
37 
38 namespace MultiArgHelper {
39 
41 
53 template<class T>
55 {
56  friend class MultiArg<T>;
57 
58  private:
59 
64  std::vector<T> &_values;
65 
70  ValueExtractor(std::vector<T> &values) : _values(values) {}
71 
77  int extractValue( const std::string& val )
78  {
79  T temp;
80 
81 #if defined(HAVE_SSTREAM)
82  std::istringstream is(val);
83 #elif defined(HAVE_STRSTREAM)
84  std::istrstream is(val.c_str());
85 #else
86 #error "Need a stringstream (sstream or strstream) to compile!"
87 #endif
88 
89  int valuesRead = 0;
90 
91  while ( is.good() )
92  {
93  if ( is.peek() != EOF )
94  is >> temp;
95  else
96  break;
97 
98  valuesRead++;
99  }
100 
101  if ( is.fail() )
102  return EXTRACT_FAILURE;
103 
104  if ( valuesRead > 1 )
105  return EXTRACT_TOO_MANY;
106 
107  _values.push_back(temp);
108 
109  return 0;
110  }
111 };
112 
120 template<>
121 class ValueExtractor<std::string>
122 {
123  friend class MultiArg<std::string>;
124 
125  private:
126 
131  std::vector<std::string> &_values;
132 
137  ValueExtractor(std::vector<std::string> &values) : _values(values) {}
138 
144  int extractValue( const std::string& val )
145  {
146  _values.push_back( val );
147  return 0;
148  }
149 };
150 
151 } //namespace MultiArgHelper
152 
158 template<class T>
159 class MultiArg : public Arg
160 {
161  protected:
162 
166  std::vector<T> _values;
167 
171  std::string _typeDesc;
172 
177 
184  void _extractValue( const std::string& val );
185 
187 
188  public:
189 
207  MultiArg( const std::string& flag,
208  const std::string& name,
209  const std::string& desc,
210  bool req,
211  const std::string& typeDesc,
212  Visitor* v = NULL);
213 
232  MultiArg( const std::string& flag,
233  const std::string& name,
234  const std::string& desc,
235  bool req,
236  const std::string& typeDesc,
237  CmdLineInterface& parser,
238  Visitor* v = NULL );
239 
255  MultiArg( const std::string& flag,
256  const std::string& name,
257  const std::string& desc,
258  bool req,
259  Constraint<T>* constraint,
260  Visitor* v = NULL );
261 
278  MultiArg( const std::string& flag,
279  const std::string& name,
280  const std::string& desc,
281  bool req,
282  Constraint<T>* constraint,
283  CmdLineInterface& parser,
284  Visitor* v = NULL );
285 
294  virtual bool processArg(int* i, std::vector<std::string>& args);
295 
300  const std::vector<T>& getValue();
301 
306  virtual std::string shortID(const std::string& val="val") const;
307 
312  virtual std::string longID(const std::string& val="val") const;
313 
318  virtual bool isRequired() const;
319 
320  virtual bool allowMore();
321 
322 };
323 
324 template<class T>
325 MultiArg<T>::MultiArg(const std::string& flag,
326  const std::string& name,
327  const std::string& desc,
328  bool req,
329  const std::string& typeDesc,
330  Visitor* v)
331 : Arg( flag, name, desc, req, true, v ),
332  _typeDesc( typeDesc ),
333  _constraint( NULL ),
334  _allowMore(false)
335 {
336  _acceptsMultipleValues = true;
337 }
338 
339 template<class T>
340 MultiArg<T>::MultiArg(const std::string& flag,
341  const std::string& name,
342  const std::string& desc,
343  bool req,
344  const std::string& typeDesc,
345  CmdLineInterface& parser,
346  Visitor* v)
347 : Arg( flag, name, desc, req, true, v ),
348  _typeDesc( typeDesc ),
349  _constraint( NULL ),
350  _allowMore(false)
351 {
352  parser.add( this );
353  _acceptsMultipleValues = true;
354 }
355 
359 template<class T>
360 MultiArg<T>::MultiArg(const std::string& flag,
361  const std::string& name,
362  const std::string& desc,
363  bool req,
364  Constraint<T>* constraint,
365  Visitor* v)
366 : Arg( flag, name, desc, req, true, v ),
367  _typeDesc( constraint->shortID() ),
368  _constraint( constraint ),
369  _allowMore(false)
370 {
371  _acceptsMultipleValues = true;
372 }
373 
374 template<class T>
375 MultiArg<T>::MultiArg(const std::string& flag,
376  const std::string& name,
377  const std::string& desc,
378  bool req,
379  Constraint<T>* constraint,
380  CmdLineInterface& parser,
381  Visitor* v)
382 : Arg( flag, name, desc, req, true, v ),
383  _typeDesc( constraint->shortID() ),
384  _constraint( constraint ),
385  _allowMore(false)
386 {
387  parser.add( this );
388  _acceptsMultipleValues = true;
389 }
390 
391 template<class T>
392 const std::vector<T>& MultiArg<T>::getValue() { return _values; }
393 
394 template<class T>
395 bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
396 {
397  if ( _ignoreable && Arg::ignoreRest() )
398  return false;
399 
400  if ( _hasBlanks( args[*i] ) )
401  return false;
402 
403  std::string flag = args[*i];
404  std::string value = "";
405 
406  trimFlag( flag, value );
407 
408  if ( argMatches( flag ) )
409  {
410  if ( Arg::delimiter() != ' ' && value == "" )
411  throw( ArgParseException(
412  "Couldn't find delimiter for this argument!",
413  toString() ) );
414 
415  // always take the first one, regardless of start string
416  if ( value == "" )
417  {
418  (*i)++;
419  if ( static_cast<unsigned int>(*i) < args.size() )
420  _extractValue( args[*i] );
421  else
422  throw( ArgParseException("Missing a value for this argument!",
423  toString() ) );
424  }
425  else
426  _extractValue( value );
427 
428  /*
429  // continuing taking the args until we hit one with a start string
430  while ( (unsigned int)(*i)+1 < args.size() &&
431  args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
432  args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
433  _extractValue( args[++(*i)] );
434  */
435 
436  _alreadySet = true;
437  _checkWithVisitor();
438 
439  return true;
440  }
441  else
442  return false;
443 }
444 
448 template<class T>
449 std::string MultiArg<T>::shortID(const std::string& val) const
450 {
451  std::string id = Arg::shortID(_typeDesc) + " ... ";
452 
453  return id;
454 }
455 
459 template<class T>
460 std::string MultiArg<T>::longID(const std::string& val) const
461 {
462  std::string id = Arg::longID(_typeDesc) + " (accepted multiple times)";
463 
464  return id;
465 }
466 
471 template<class T>
473 {
474  if ( _required )
475  {
476  if ( _values.size() > 1 )
477  return false;
478  else
479  return true;
480  }
481  else
482  return false;
483 
484 }
485 
486 template<class T>
487 void MultiArg<T>::_extractValue( const std::string& val )
488 {
490 
491  int err = ve.extractValue(val);
492 
493  if ( err == MultiArgHelper::EXTRACT_FAILURE )
494  throw( ArgParseException("Couldn't read argument value "
495  "from string '" + val + "'", toString() ) );
496 
498  throw( ArgParseException("More than one valid value "
499  "parsed from string '" + val + "'",
500  toString() ) );
501  if ( _constraint != NULL )
502  if ( ! _constraint->check( _values.back() ) )
503  throw( CmdLineParseException( "Value '" + val +
504  "' does not meet constraint: " +
505  _constraint->description(),
506  toString() ) );
507 }
508 
509 template<class T>
511 {
512  bool am = _allowMore;
513  _allowMore = true;
514  return am;
515 }
516 
517 }; // namespace ecl
518 
519 
520 #endif
arg.hpp
TCLAP command line argument parser classes.
ecl::MultiArg::_values
std::vector< T > _values
Definition: multi_arg.hpp:166
ecl::MultiArgHelper::ValueExtractor::extractValue
int extractValue(const std::string &val)
Definition: multi_arg.hpp:77
ecl::MultiArgHelper::EXTRACT_TOO_MANY
@ EXTRACT_TOO_MANY
Definition: multi_arg.hpp:40
ecl::MultiArgHelper::ValueExtractor< std::string >::extractValue
int extractValue(const std::string &val)
Definition: multi_arg.hpp:144
ecl::Arg::shortID
virtual std::string shortID(const std::string &valueId="val") const
Definition: arg.hpp:398
ecl::MultiArg
Definition: multi_arg.hpp:36
ecl::MultiArgHelper::ValueExtractor::ValueExtractor
ValueExtractor(std::vector< T > &values)
Definition: multi_arg.hpp:70
ecl::MultiArgHelper::ValueExtractor::_values
std::vector< T > & _values
Definition: multi_arg.hpp:64
ecl::Arg::delimiter
static char delimiter()
Definition: arg.hpp:179
ecl::CmdLineInterface::add
virtual void add(Arg &a)=0
ecl::MultiArgHelper::Error_e
Error_e
Definition: multi_arg.hpp:40
ecl::Arg
Virtual parent for all the different argument classes.
Definition: arg.hpp:39
ecl::MultiArg::_extractValue
void _extractValue(const std::string &val)
Definition: multi_arg.hpp:487
ecl::MultiArg::_allowMore
bool _allowMore
Definition: multi_arg.hpp:186
ecl::ArgParseException
Defines the exception that is thrown when an argument is improperly parsed.
Definition: arg_exception.hpp:119
ecl::MultiArg::isRequired
virtual bool isRequired() const
Definition: multi_arg.hpp:472
ecl::MultiArg::_typeDesc
std::string _typeDesc
Definition: multi_arg.hpp:171
ecl::MultiArg::_constraint
Constraint< T > * _constraint
Definition: multi_arg.hpp:176
ecl::Visitor
TClap class indirectly used to define the interface for visitors.
Definition: visitor.hpp:27
ecl::MultiArg::allowMore
virtual bool allowMore()
Definition: multi_arg.hpp:510
ecl::MultiArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Definition: multi_arg.hpp:395
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::MultiArgHelper::ValueExtractor
TClap class internally used for extracting value args.
Definition: multi_arg.hpp:54
ecl::Constraint
Defines the interaction between an argument and a constraint.
Definition: constraint.hpp:36
ecl::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Definition: arg.hpp:419
ecl::MultiArg::longID
virtual std::string longID(const std::string &val="val") const
Definition: multi_arg.hpp:460
ecl::CmdLineParseException
Defines the exception when an argument is improperly specified.
Definition: arg_exception.hpp:143
ecl::MultiArgHelper::EXTRACT_FAILURE
@ EXTRACT_FAILURE
Definition: multi_arg.hpp:40
ecl::MultiArg::shortID
virtual std::string shortID(const std::string &val="val") const
Definition: multi_arg.hpp:449
ecl
ecl::MultiArg::getValue
const std::vector< T > & getValue()
Definition: multi_arg.hpp:392
ecl::MultiArg::MultiArg
MultiArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, Visitor *v=NULL)
Definition: multi_arg.hpp:325


ecl_command_line
Author(s): Daniel Stonier
autogenerated on Sun Aug 2 2020 03:12:03