MultiArg.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 TCLAP_MULTIPLE_ARGUMENT_H
24 #define TCLAP_MULTIPLE_ARGUMENT_H
25 
26 #include <string>
27 #include <vector>
28 
29 #include <tclap/Arg.h>
30 #include <tclap/Constraint.h>
31 
32 namespace TCLAP {
38 template<class T>
39 class MultiArg : public Arg
40 {
41 public:
42  typedef std::vector<T> container_type;
43  typedef typename container_type::iterator iterator;
44  typedef typename container_type::const_iterator const_iterator;
45 
46 protected:
47 
51  std::vector<T> _values;
52 
57 
62 
69  void _extractValue( const std::string& val );
70 
74  bool _allowMore;
75 
76 public:
77 
95  MultiArg( const std::string& flag,
96  const std::string& name,
97  const std::string& desc,
98  bool req,
99  const std::string& typeDesc,
100  Visitor* v = NULL);
101 
120  MultiArg( const std::string& flag,
121  const std::string& name,
122  const std::string& desc,
123  bool req,
124  const std::string& typeDesc,
126  Visitor* v = NULL );
127 
143  MultiArg( const std::string& flag,
144  const std::string& name,
145  const std::string& desc,
146  bool req,
147  Constraint<T>* constraint,
148  Visitor* v = NULL );
149 
166  MultiArg( const std::string& flag,
167  const std::string& name,
168  const std::string& desc,
169  bool req,
170  Constraint<T>* constraint,
172  Visitor* v = NULL );
173 
182  virtual bool processArg(int* i, std::vector<std::string>& args);
183 
188  const std::vector<T>& getValue();
189 
194  const_iterator begin() const { return _values.begin(); }
195 
200  const_iterator end() const { return _values.end(); }
201 
206  virtual std::string shortID(const std::string& val="val") const;
207 
212  virtual std::string longID(const std::string& val="val") const;
213 
218  virtual bool isRequired() const;
219 
220  virtual bool allowMore();
221 
222  virtual void reset();
223 
224 private:
228  MultiArg<T>(const MultiArg<T>& rhs);
229  MultiArg<T>& operator=(const MultiArg<T>& rhs);
230 
231 };
232 
233 template<class T>
235  const std::string& name,
236  const std::string& desc,
237  bool req,
238  const std::string& typeDesc,
239  Visitor* v) :
240  Arg( flag, name, desc, req, true, v ),
241  _values(std::vector<T>()),
242  _typeDesc( typeDesc ),
243  _constraint( NULL ),
244  _allowMore(false)
245 {
246  _acceptsMultipleValues = true;
247 }
248 
249 template<class T>
251  const std::string& name,
252  const std::string& desc,
253  bool req,
254  const std::string& typeDesc,
256  Visitor* v)
257 : Arg( flag, name, desc, req, true, v ),
258  _values(std::vector<T>()),
259  _typeDesc( typeDesc ),
260  _constraint( NULL ),
261  _allowMore(false)
262 {
263  parser.add( this );
264  _acceptsMultipleValues = true;
265 }
266 
270 template<class T>
272  const std::string& name,
273  const std::string& desc,
274  bool req,
275  Constraint<T>* constraint,
276  Visitor* v)
277 : Arg( flag, name, desc, req, true, v ),
278  _values(std::vector<T>()),
279  _typeDesc( constraint->shortID() ),
280  _constraint( constraint ),
281  _allowMore(false)
282 {
283  _acceptsMultipleValues = true;
284 }
285 
286 template<class T>
288  const std::string& name,
289  const std::string& desc,
290  bool req,
291  Constraint<T>* constraint,
293  Visitor* v)
294 : Arg( flag, name, desc, req, true, v ),
295  _values(std::vector<T>()),
296  _typeDesc( constraint->shortID() ),
297  _constraint( constraint ),
298  _allowMore(false)
299 {
300  parser.add( this );
301  _acceptsMultipleValues = true;
302 }
303 
304 template<class T>
305 const std::vector<T>& MultiArg<T>::getValue() { return _values; }
306 
307 template<class T>
308 bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
309 {
310  if ( _ignoreable && Arg::ignoreRest() )
311  return false;
312 
313  if ( _hasBlanks( args[*i] ) )
314  return false;
315 
316  std::string flag = args[*i];
317  std::string value = "";
318 
319  trimFlag( flag, value );
320 
321  if ( argMatches( flag ) )
322  {
323  if ( Arg::delimiter() != ' ' && value == "" )
324  throw( ArgParseException(
325  "Couldn't find delimiter for this argument!",
326  toString() ) );
327 
328  // always take the first one, regardless of start string
329  if ( value == "" )
330  {
331  (*i)++;
332  if ( static_cast<unsigned int>(*i) < args.size() )
333  _extractValue( args[*i] );
334  else
335  throw( ArgParseException("Missing a value for this argument!",
336  toString() ) );
337  }
338  else
339  _extractValue( value );
340 
341  /*
342  // continuing taking the args until we hit one with a start string
343  while ( (unsigned int)(*i)+1 < args.size() &&
344  args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
345  args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
346  _extractValue( args[++(*i)] );
347  */
348 
349  _alreadySet = true;
351 
352  return true;
353  }
354  else
355  return false;
356 }
357 
361 template<class T>
363 {
364  static_cast<void>(val); // Ignore input, don't warn
365  return Arg::shortID(_typeDesc) + " ... ";
366 }
367 
371 template<class T>
373 {
374  static_cast<void>(val); // Ignore input, don't warn
375  return Arg::longID(_typeDesc) + " (accepted multiple times)";
376 }
377 
382 template<class T>
384 {
385  if ( _required )
386  {
387  if ( _values.size() > 1 )
388  return false;
389  else
390  return true;
391  }
392  else
393  return false;
394 
395 }
396 
397 template<class T>
399 {
400  try {
401  T tmp;
402  ExtractValue(tmp, val, typename ArgTraits<T>::ValueCategory());
403  _values.push_back(tmp);
404  } catch( ArgParseException &e) {
405  throw ArgParseException(e.error(), toString());
406  }
407 
408  if ( _constraint != NULL )
409  if ( ! _constraint->check( _values.back() ) )
410  throw( CmdLineParseException( "Value '" + val +
411  "' does not meet constraint: " +
412  _constraint->description(),
413  toString() ) );
414 }
415 
416 template<class T>
418 {
419  bool am = _allowMore;
420  _allowMore = true;
421  return am;
422 }
423 
424 template<class T>
426 {
427  Arg::reset();
428  _values.clear();
429 }
430 
431 } // namespace TCLAP
432 
433 #endif
bool _alreadySet
Definition: Arg.h:137
bool _required
Definition: Arg.h:117
Definition: Arg.h:64
GLuint const GLchar * name
virtual void trimFlag(std::string &flag, std::string &value) const
Definition: Arg.h:620
virtual std::string longID(const std::string &valueId="val") const
Definition: Arg.h:523
bool _hasBlanks(const std::string &s) const
Definition: Arg.h:641
virtual bool processArg(int *i, std::vector< std::string > &args)
Definition: MultiArg.h:308
GLfloat value
const std::vector< T > & getValue()
Definition: MultiArg.h:305
parser
Definition: enums.py:61
GLsizei const GLchar *const * string
void _checkWithVisitor() const
Definition: Arg.h:611
std::vector< T > _values
Definition: MultiArg.h:51
virtual void reset()
Definition: Arg.h:679
e
Definition: rmse.py:177
const_iterator end() const
Definition: MultiArg.h:200
virtual void reset()
Definition: MultiArg.h:425
static char delimiter()
Definition: Arg.h:211
GLuint GLfloat * val
virtual std::string longID(const std::string &val="val") const
Definition: MultiArg.h:372
std::string _typeDesc
Definition: MultiArg.h:56
std::string error() const
Definition: ArgException.h:64
void _extractValue(const std::string &val)
Definition: MultiArg.h:398
Constraint< T > * _constraint
Definition: MultiArg.h:61
virtual std::string shortID(const std::string &val="val") const
Definition: MultiArg.h:362
virtual std::string shortID(const std::string &valueId="val") const
Definition: Arg.h:505
virtual bool allowMore()
Definition: MultiArg.h:417
std::vector< T > container_type
Definition: MultiArg.h:42
bool _allowMore
Definition: MultiArg.h:74
container_type::const_iterator const_iterator
Definition: MultiArg.h:44
MultiArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, Visitor *v=NULL)
Definition: MultiArg.h:234
virtual void add(Arg &a)=0
bool _ignoreable
Definition: Arg.h:150
container_type::iterator iterator
Definition: MultiArg.h:43
static bool ignoreRest()
Definition: Arg.h:205
T::ValueCategory ValueCategory
Definition: ArgTraits.h:80
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:415
const_iterator begin() const
Definition: MultiArg.h:194
virtual bool argMatches(const std::string &s) const
Definition: Arg.h:590
#define NULL
Definition: tinycthread.c:47
int i
MultiArg< T > & operator=(const MultiArg< T > &rhs)
bool _acceptsMultipleValues
Definition: Arg.h:158
Definition: Arg.h:57
virtual std::string toString() const
Definition: Arg.h:599
virtual bool isRequired() const
Definition: MultiArg.h:383
GLdouble v


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