tclap  1.2.2
MultiArg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: MultiArg.h
6  *
7  * Copyright (c) 2003, Michael E. Smoot .
8  * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
9  * Copyright (c) 2017, Google LLC
10  * All rights reserved.
11  *
12  * See the file COPYING in the top directory of this distribution for
13  * more information.
14  *
15  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  *****************************************************************************/
24 
25 
26 #ifndef TCLAP_MULTIPLE_ARGUMENT_H
27 #define TCLAP_MULTIPLE_ARGUMENT_H
28 
29 #include <string>
30 #include <vector>
31 
32 #include <tclap/Arg.h>
33 #include <tclap/Constraint.h>
34 
35 namespace TCLAP {
41 template<class T>
42 class MultiArg : public Arg
43 {
44 public:
45  typedef std::vector<T> container_type;
46  typedef typename container_type::iterator iterator;
47  typedef typename container_type::const_iterator const_iterator;
48 
49 protected:
50 
54  std::vector<T> _values;
55 
59  std::string _typeDesc;
60 
65 
72  void _extractValue( const std::string& val );
73 
77  bool _allowMore;
78 
79 public:
80 
98  MultiArg( const std::string& flag,
99  const std::string& name,
100  const std::string& desc,
101  bool req,
102  const std::string& typeDesc,
103  Visitor* v = NULL);
104 
123  MultiArg( const std::string& flag,
124  const std::string& name,
125  const std::string& desc,
126  bool req,
127  const std::string& typeDesc,
128  CmdLineInterface& parser,
129  Visitor* v = NULL );
130 
146  MultiArg( const std::string& flag,
147  const std::string& name,
148  const std::string& desc,
149  bool req,
150  Constraint<T>* constraint,
151  Visitor* v = NULL );
152 
169  MultiArg( const std::string& flag,
170  const std::string& name,
171  const std::string& desc,
172  bool req,
173  Constraint<T>* constraint,
174  CmdLineInterface& parser,
175  Visitor* v = NULL );
176 
185  virtual bool processArg(int* i, std::vector<std::string>& args);
186 
191  const std::vector<T>& getValue() const { return _values; }
192 
197  const_iterator begin() const { return _values.begin(); }
198 
203  const_iterator end() const { return _values.end(); }
204 
209  virtual std::string shortID(const std::string& val="val") const;
210 
215  virtual std::string longID(const std::string& val="val") const;
216 
221  virtual bool isRequired() const;
222 
223  virtual bool allowMore();
224 
225  virtual void reset();
226 
227 private:
231  MultiArg<T>(const MultiArg<T>& rhs);
232  MultiArg<T>& operator=(const MultiArg<T>& rhs);
233 
234 };
235 
236 template<class T>
237 MultiArg<T>::MultiArg(const std::string& flag,
238  const std::string& name,
239  const std::string& desc,
240  bool req,
241  const std::string& typeDesc,
242  Visitor* v) :
243  Arg( flag, name, desc, req, true, v ),
244  _values(std::vector<T>()),
245  _typeDesc( typeDesc ),
246  _constraint( NULL ),
247  _allowMore(false)
248 {
249  _acceptsMultipleValues = true;
250 }
251 
252 template<class T>
253 MultiArg<T>::MultiArg(const std::string& flag,
254  const std::string& name,
255  const std::string& desc,
256  bool req,
257  const std::string& typeDesc,
258  CmdLineInterface& parser,
259  Visitor* v)
260 : Arg( flag, name, desc, req, true, v ),
261  _values(std::vector<T>()),
262  _typeDesc( typeDesc ),
263  _constraint( NULL ),
264  _allowMore(false)
265 {
266  parser.add( this );
267  _acceptsMultipleValues = true;
268 }
269 
273 template<class T>
274 MultiArg<T>::MultiArg(const std::string& flag,
275  const std::string& name,
276  const std::string& desc,
277  bool req,
278  Constraint<T>* constraint,
279  Visitor* v)
280 : Arg( flag, name, desc, req, true, v ),
281  _values(std::vector<T>()),
282  _typeDesc( Constraint<T>::shortID(constraint) ),
283  _constraint( constraint ),
284  _allowMore(false)
285 {
286  _acceptsMultipleValues = true;
287 }
288 
289 template<class T>
290 MultiArg<T>::MultiArg(const std::string& flag,
291  const std::string& name,
292  const std::string& desc,
293  bool req,
294  Constraint<T>* constraint,
295  CmdLineInterface& parser,
296  Visitor* v)
297 : Arg( flag, name, desc, req, true, v ),
298  _values(std::vector<T>()),
299  _typeDesc( Constraint<T>::shortID(constraint) ),
300  _constraint( constraint ),
301  _allowMore(false)
302 {
303  parser.add( this );
304  _acceptsMultipleValues = true;
305 }
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>
362 std::string MultiArg<T>::shortID(const std::string& val) const
363 {
364  static_cast<void>(val); // Ignore input, don't warn
365  return Arg::shortID(_typeDesc) + " ...";
366 }
367 
371 template<class T>
372 std::string MultiArg<T>::longID(const std::string& val) const
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>
398 void MultiArg<T>::_extractValue( const std::string& val )
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
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
Definition: MultiArg.h:372
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:581
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:128
bool _required
Indicating whether the argument is required.
Definition: Arg.h:108
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:55
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: MultiArg.h:308
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: ArgException.h:144
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:39
const_iterator end() const
Returns the end of the values parsed from the command line.
Definition: MultiArg.h:203
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:42
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition: MultiArg.h:54
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:670
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
Definition: MultiArg.h:362
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:514
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: MultiArg.h:425
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:202
const_iterator begin() const
Returns an iterator over the values parsed from the command line.
Definition: MultiArg.h:197
std::string _typeDesc
The description of type T to be used in the usage.
Definition: MultiArg.h:59
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:42
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:602
virtual bool isRequired() const
Once we&#39;ve matched the first value, then the arg is no longer required.
Definition: MultiArg.h:383
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: MultiArg.h:398
Constraint< T > * _constraint
A list of constraint on this Arg.
Definition: MultiArg.h:64
virtual bool allowMore()
Used for MultiArgs and XorHandler to determine whether args can still be set.
Definition: MultiArg.h:417
A base class that defines the interface for visitors.
Definition: Visitor.h:34
std::vector< T > container_type
Definition: MultiArg.h:45
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:496
const std::vector< T > & getValue() const
Returns a vector of type T containing the values parsed from the command line.
Definition: MultiArg.h:191
bool _allowMore
Used by XorHandler to decide whether to keep parsing for this arg.
Definition: MultiArg.h:77
container_type::const_iterator const_iterator
Definition: MultiArg.h:47
The base class that manages the command line definition and passes along the parsing to the appropria...
MultiArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, const std::string &typeDesc, Visitor *v=NULL)
Constructor.
Definition: MultiArg.h:237
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:590
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition: Arg.h:141
container_type::iterator iterator
Definition: MultiArg.h:46
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:196
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:406
bool _acceptsMultipleValues
Definition: Arg.h:149
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:122
Definition: Arg.h:48
bool _hasBlanks(const std::string &s) const
Checks whether a given string has blank chars, indicating that it is a combined SwitchArg.
Definition: Arg.h:632
std::string error() const
Returns the error text.
Definition: ArgException.h:65
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition: Arg.h:611