tclap  1.4.0
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 #ifndef TCLAP_VALUE_ARG_H
26 #define TCLAP_VALUE_ARG_H
27 
28 #include <tclap/Arg.h>
29 #include <tclap/Constraint.h>
30 
31 #include <string>
32 #include <vector>
33 
34 namespace TCLAP {
35 
44 template <class T>
45 class ValueArg : public Arg {
46 protected:
52  T _value;
53 
59 
67  std::string _typeDesc;
68 
73 
80  void _extractValue(const std::string &val);
81 
82 public:
106  ValueArg(const std::string &flag, const std::string &name,
107  const std::string &desc, bool req, T value,
108  const std::string &typeDesc, Visitor *v = NULL);
109 
134  ValueArg(const std::string &flag, const std::string &name,
135  const std::string &desc, bool req, T value,
136  const std::string &typeDesc, ArgContainer &parser,
137  Visitor *v = NULL);
138 
161  ValueArg(const std::string &flag, const std::string &name,
162  const std::string &desc, bool req, T value,
163  Constraint<T> *constraint, ArgContainer &parser,
164  Visitor *v = NULL);
165 
187  ValueArg(const std::string &flag, const std::string &name,
188  const std::string &desc, bool req, T value,
189  Constraint<T> *constraint, Visitor *v = NULL);
190 
200  virtual bool processArg(int *i, std::vector<std::string> &args);
201 
205  const T &getValue() const { return _value; }
206 
211  operator const T &() const { return getValue(); }
212 
217  virtual std::string shortID(const std::string &val = "val") const;
218 
223  virtual std::string longID(const std::string &val = "val") const;
224 
225  virtual void reset();
226 
227 private:
231  ValueArg<T>(const ValueArg<T> &rhs);
232  ValueArg<T> &operator=(const ValueArg<T> &rhs);
233 };
234 
238 template <class T>
239 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
240  const std::string &desc, bool req, T val,
241  const std::string &typeDesc, Visitor *v)
242  : Arg(flag, name, desc, req, true, v),
243  _value(val),
244  _default(val),
245  _typeDesc(typeDesc),
246  _constraint(NULL) {}
247 
248 template <class T>
249 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
250  const std::string &desc, bool req, T val,
251  const std::string &typeDesc, ArgContainer &parser,
252  Visitor *v)
253  : Arg(flag, name, desc, req, true, v),
254  _value(val),
255  _default(val),
256  _typeDesc(typeDesc),
257  _constraint(NULL) {
258  parser.add(this);
259 }
260 
261 template <class T>
262 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
263  const std::string &desc, bool req, T val,
264  Constraint<T> *constraint, Visitor *v)
265  : Arg(flag, name, desc, req, true, v),
266  _value(val),
267  _default(val),
268  _typeDesc(Constraint<T>::shortID(constraint)),
269  _constraint(constraint) {}
270 
271 template <class T>
272 ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
273  const std::string &desc, bool req, T val,
274  Constraint<T> *constraint, ArgContainer &parser,
275  Visitor *v)
276  : Arg(flag, name, desc, req, true, v),
277  _value(val),
278  _default(val),
279  _typeDesc(Constraint<T>::shortID(constraint)),
280  _constraint(constraint) {
281  parser.add(this);
282 }
283 
287 template <class T>
288 bool ValueArg<T>::processArg(int *i, std::vector<std::string> &args) {
289  if (_hasBlanks(args[*i])) return false;
290 
291  std::string flag = args[*i];
292 
293  std::string value = "";
294  trimFlag(flag, value);
295 
296  if (argMatches(flag)) {
297  if (_alreadySet) {
298  throw(CmdLineParseException("Argument already set!", toString()));
299  }
300 
301  if (Arg::delimiter() != ' ' && value == "")
302  throw(ArgParseException(
303  "Couldn't find delimiter for this argument!", toString()));
304 
305  if (value == "") {
306  (*i)++;
307  if (static_cast<unsigned int>(*i) < args.size())
308  _extractValue(args[*i]);
309  else
310  throw(ArgParseException("Missing a value for this argument!",
311  toString()));
312  } else {
313  _extractValue(value);
314  }
315 
316  _alreadySet = true;
317  _setBy = flag;
319  return true;
320  } else {
321  return false;
322  }
323 }
324 
328 template <class T>
329 std::string ValueArg<T>::shortID(const std::string &) const {
330  return Arg::shortID("<" + _typeDesc + ">");
331 }
332 
336 template <class T>
337 std::string ValueArg<T>::longID(const std::string &) const {
338  return Arg::longID("<" + _typeDesc + ">");
339 }
340 
341 template <class T>
342 void ValueArg<T>::_extractValue(const std::string &val) {
343  try {
345  } catch (ArgParseException &e) {
346  throw ArgParseException(e.error(), toString());
347  }
348 
349  if (_constraint != NULL)
350  if (!_constraint->check(_value))
351  throw(CmdLineParseException("Value '" + val +
352  +"' does not meet constraint: " +
353  _constraint->description(),
354  toString()));
355 }
356 
357 template <class T>
359  Arg::reset();
360  _value = _default;
361 }
362 
363 } // namespace TCLAP
364 
365 #endif // TCLAP_VALUE_ARG_H
const T & getValue() const
Returns the value of the argument.
Definition: ValueArg.h:205
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition: Arg.h:535
bool _alreadySet
Indicates whether the argument has been set.
Definition: Arg.h:122
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:342
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:53
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: ArgException.h:129
A value like argument value type is a value that can be set using operator>>.
Definition: ArgTraits.h:39
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:595
The basic labeled argument that parses a value.
Definition: ValueArg.h:45
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:498
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:186
virtual std::string shortID(const std::string &val="val") const
Specialization of shortID.
Definition: ValueArg.h:329
T _value
The value parsed from the command line.
Definition: ValueArg.h:52
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:41
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition: Arg.h:553
Interface that allows adding an Arg to a "container".
Definition: ArgContainer.h:39
Constraint< T > * _constraint
A Constraint this Arg must conform to.
Definition: ValueArg.h:72
A base class that defines the interface for visitors.
Definition: Visitor.h:32
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:485
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition: Arg.h:543
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:67
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:399
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:110
T _default
Used to support the reset() method so that ValueArg can be reset to their constructed value...
Definition: ValueArg.h:58
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: ValueArg.h:288
Definition: Arg.h:46
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:577
virtual std::string longID(const std::string &val="val") const
Specialization of longID.
Definition: ValueArg.h:337
std::string error() const
Returns the error text.
Definition: ArgException.h:61
std::string _setBy
Indicates the value specified to set this flag (like -a or –all).
Definition: Arg.h:126
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: ValueArg.h:358
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition: Arg.h:560
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:239