tclap  1.2.1
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 
56  std::string _typeDesc;
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,
125  CmdLineInterface& parser,
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,
171  CmdLineInterface& parser,
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>
234 MultiArg<T>::MultiArg(const std::string& flag,
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>
250 MultiArg<T>::MultiArg(const std::string& flag,
251  const std::string& name,
252  const std::string& desc,
253  bool req,
254  const std::string& typeDesc,
255  CmdLineInterface& parser,
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>
271 MultiArg<T>::MultiArg(const std::string& flag,
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>
287 MultiArg<T>::MultiArg(const std::string& flag,
288  const std::string& name,
289  const std::string& desc,
290  bool req,
291  Constraint<T>* constraint,
292  CmdLineInterface& parser,
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;
350  _checkWithVisitor();
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
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:64
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:143
const std::vector< T > & getValue()
Returns a vector of type T containing the values parsed from the command line.
Definition: MultiArg.h:305
const_iterator end() const
Returns the end of the values parsed from the command line.
Definition: MultiArg.h:200
An argument that allows multiple values of type T to be specified.
Definition: MultiArg.h:39
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition: MultiArg.h:51
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:679
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:523
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:211
const_iterator begin() const
Returns an iterator over the values parsed from the command line.
Definition: MultiArg.h:194
std::string _typeDesc
The description of type T to be used in the usage.
Definition: MultiArg.h:56
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:38
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:61
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:31
std::vector< T > container_type
Definition: MultiArg.h:42
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:505
bool _allowMore
Used by XorHandler to decide whether to keep parsing for this arg.
Definition: MultiArg.h:74
container_type::const_iterator const_iterator
Definition: MultiArg.h:44
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:234
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
container_type::iterator iterator
Definition: MultiArg.h:43
static bool ignoreRest()
Whether to ignore the rest.
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
bool _acceptsMultipleValues
Definition: Arg.h:158
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:121
Definition: Arg.h:57
std::string error() const
Returns the error text.
Definition: ArgException.h:64