tclap  1.2.2
ValueArg.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: ValueArg.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_VALUE_ARGUMENT_H
27 #define TCLAP_VALUE_ARGUMENT_H
28 
29 #include <string>
30 #include <vector>
31 
32 #include <tclap/Arg.h>
33 #include <tclap/Constraint.h>
34 
35 namespace TCLAP {
36 
45 template<class T>
46 class ValueArg : public Arg
47 {
48 protected:
49 
55  T _value;
56 
62 
70  std::string _typeDesc;
71 
76 
83  void _extractValue( const std::string& val );
84 
85 public:
86 
110  ValueArg( const std::string& flag,
111  const std::string& name,
112  const std::string& desc,
113  bool req,
114  T value,
115  const std::string& typeDesc,
116  Visitor* v = NULL);
117 
118 
143  ValueArg( const std::string& flag,
144  const std::string& name,
145  const std::string& desc,
146  bool req,
147  T value,
148  const std::string& typeDesc,
149  CmdLineInterface& parser,
150  Visitor* v = NULL );
151 
174  ValueArg( const std::string& flag,
175  const std::string& name,
176  const std::string& desc,
177  bool req,
178  T value,
179  Constraint<T>* constraint,
180  CmdLineInterface& parser,
181  Visitor* v = NULL );
182 
204  ValueArg( const std::string& flag,
205  const std::string& name,
206  const std::string& desc,
207  bool req,
208  T value,
209  Constraint<T>* constraint,
210  Visitor* v = NULL );
211 
221  virtual bool processArg(int* i, std::vector<std::string>& args);
222 
226  const T& getValue() const { return _value; }
227 
228  // TODO(macbishop): Non-const variant is deprecated, don't
229  // use. Remove in next major.
230  T& getValue() { return _value; }
231 
236  operator const T&() const { return getValue(); }
237 
242  virtual std::string shortID(const std::string& val = "val") const;
243 
248  virtual std::string longID(const std::string& val = "val") const;
249 
250  virtual void reset() ;
251 
252 private:
256  ValueArg<T>(const ValueArg<T>& rhs);
257  ValueArg<T>& operator=(const ValueArg<T>& rhs);
258 };
259 
260 
264 template<class T>
265 ValueArg<T>::ValueArg(const std::string& flag,
266  const std::string& name,
267  const std::string& desc,
268  bool req,
269  T val,
270  const std::string& typeDesc,
271  Visitor* v)
272  : Arg(flag, name, desc, req, true, v),
273  _value( val ),
274  _default( val ),
275  _typeDesc( typeDesc ),
276  _constraint( NULL )
277 { }
278 
279 template<class T>
280 ValueArg<T>::ValueArg(const std::string& flag,
281  const std::string& name,
282  const std::string& desc,
283  bool req,
284  T val,
285  const std::string& typeDesc,
286  CmdLineInterface& parser,
287  Visitor* v)
288  : Arg(flag, name, desc, req, true, v),
289  _value( val ),
290  _default( val ),
291  _typeDesc( typeDesc ),
292  _constraint( NULL )
293 {
294  parser.add( this );
295 }
296 
297 template<class T>
298 ValueArg<T>::ValueArg(const std::string& flag,
299  const std::string& name,
300  const std::string& desc,
301  bool req,
302  T val,
303  Constraint<T>* constraint,
304  Visitor* v)
305  : Arg(flag, name, desc, req, true, v),
306  _value( val ),
307  _default( val ),
308  _typeDesc( Constraint<T>::shortID(constraint) ),
309  _constraint( constraint )
310 { }
311 
312 template<class T>
313 ValueArg<T>::ValueArg(const std::string& flag,
314  const std::string& name,
315  const std::string& desc,
316  bool req,
317  T val,
318  Constraint<T>* constraint,
319  CmdLineInterface& parser,
320  Visitor* v)
321  : Arg(flag, name, desc, req, true, v),
322  _value( val ),
323  _default( val ),
324  _typeDesc( Constraint<T>::shortID(constraint) ), // TODO(macbishop): Will crash
325  // if constraint is NULL
326  _constraint( constraint )
327 {
328  parser.add( this );
329 }
330 
334 template<class T>
335 bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
336 {
337  if ( _ignoreable && Arg::ignoreRest() )
338  return false;
339 
340  if ( _hasBlanks( args[*i] ) )
341  return false;
342 
343  std::string flag = args[*i];
344 
345  std::string value = "";
346  trimFlag( flag, value );
347 
348  if ( argMatches( flag ) )
349  {
350  if ( _alreadySet )
351  {
352  if ( _xorSet )
353  throw( CmdLineParseException("Mutually exclusive argument"
354  " already set!", toString()));
355  else
356  throw( CmdLineParseException("Argument already set!",
357  toString()) );
358  }
359 
360  if ( Arg::delimiter() != ' ' && value == "" )
361  throw( ArgParseException("Couldn't find delimiter for this argument!",
362  toString() ) );
363 
364  if ( value == "" )
365  {
366  (*i)++;
367  if ( static_cast<unsigned int>(*i) < args.size() )
368  _extractValue( args[*i] );
369  else
370  throw( ArgParseException("Missing a value for this argument!",
371  toString() ) );
372  }
373  else
374  _extractValue( value );
375 
376  _alreadySet = true;
378  return true;
379  }
380  else
381  return false;
382 }
383 
387 template<class T>
388 std::string ValueArg<T>::shortID(const std::string& val) const
389 {
390  static_cast<void>(val); // Ignore input, don't warn
391  return Arg::shortID( _typeDesc );
392 }
393 
397 template<class T>
398 std::string ValueArg<T>::longID(const std::string& val) const
399 {
400  static_cast<void>(val); // Ignore input, don't warn
401  return Arg::longID( _typeDesc );
402 }
403 
404 template<class T>
405 void ValueArg<T>::_extractValue( const std::string& val )
406 {
407  try {
409  } catch( ArgParseException &e) {
410  throw ArgParseException(e.error(), toString());
411  }
412 
413  if ( _constraint != NULL )
414  if ( ! _constraint->check( _value ) )
415  throw( CmdLineParseException( "Value '" + val +
416  + "' does not meet constraint: "
417  + _constraint->description(),
418  toString() ) );
419 }
420 
421 template<class T>
423 {
424  Arg::reset();
425  _value = _default;
426 }
427 
428 } // namespace TCLAP
429 
430 #endif
const T & getValue() const
Returns the value of the argument.
Definition: ValueArg.h:226
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
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:405
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:55
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
T & getValue()
Definition: ValueArg.h:230
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:670
bool _xorSet
Indicates that the arg was set as part of an XOR and not on the command line.
Definition: Arg.h:147
The basic labeled argument that parses a value.
Definition: ValueArg.h:46
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:514
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:202
virtual std::string shortID(const std::string &val="val") const
Specialization of shortID.
Definition: ValueArg.h:388
T _value
The value parsed from the command line.
Definition: ValueArg.h:55
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
Constraint< T > * _constraint
A Constraint this Arg must conform to.
Definition: ValueArg.h:75
A base class that defines the interface for visitors.
Definition: Visitor.h:34
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:496
The base class that manages the command line definition and passes along the parsing to the appropria...
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
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:70
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
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:122
T _default
Used to support the reset() method so that ValueArg can be reset to their constructed value...
Definition: ValueArg.h:61
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: ValueArg.h:335
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
virtual std::string longID(const std::string &val="val") const
Specialization of longID.
Definition: ValueArg.h:398
std::string error() const
Returns the error text.
Definition: ArgException.h:65
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: ValueArg.h:422
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition: Arg.h:611
ValueArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, T value, const std::string &typeDesc, Visitor *v=NULL)
Labeled ValueArg constructor.
Definition: ValueArg.h:265