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
34namespace TCLAP {
35
44template <class T>
45class ValueArg : public Arg {
46protected:
53
59
67 std::string _typeDesc;
68
73
80 void _extractValue(const std::string &val);
81
82public:
103 ValueArg(const std::string &flag, const std::string &name,
104 const std::string &desc, bool req, T value,
105 const std::string &typeDesc, Visitor *v = NULL);
106
128 ValueArg(const std::string &flag, const std::string &name,
129 const std::string &desc, bool req, T value,
130 const std::string &typeDesc, ArgContainer &parser,
131 Visitor *v = NULL);
132
152 ValueArg(const std::string &flag, const std::string &name,
153 const std::string &desc, bool req, T value,
154 const Constraint<T> *constraint, ArgContainer &parser,
155 Visitor *v = NULL);
156
175 ValueArg(const std::string &flag, const std::string &name,
176 const std::string &desc, bool req, T value,
177 const Constraint<T> *constraint, Visitor *v = NULL);
178
188 virtual bool processArg(int *i, std::vector<std::string> &args);
189
193 const T &getValue() const { return _value; }
194
199 operator const T &() const { return getValue(); }
200
205 virtual std::string shortID(const std::string &val = "val") const;
206
211 virtual std::string longID(const std::string &val = "val") const;
212
213 virtual void reset();
214
215private:
216 void _extractNextValue(int *index, std::vector<std::string> &args);
217
221 ValueArg(const ValueArg<T>& rhs);
222 ValueArg& operator=(const ValueArg<T>& rhs);
223};
224
228template <class T>
229ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
230 const std::string &desc, bool req, T val,
231 const std::string &typeDesc, Visitor *v)
232 : Arg(flag, name, desc, req, true, v),
233 _value(val),
234 _default(val),
235 _typeDesc(typeDesc),
236 _constraint(NULL) {}
237
238template <class T>
239ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
240 const std::string &desc, bool req, T val,
241 const std::string &typeDesc, ArgContainer &parser,
242 Visitor *v)
243 : Arg(flag, name, desc, req, true, v),
244 _value(val),
245 _default(val),
246 _typeDesc(typeDesc),
247 _constraint(NULL) {
248 parser.add(this);
249}
250
251template <class T>
252ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
253 const std::string &desc, bool req, T val,
254 const Constraint<T> *constraint, Visitor *v)
255 : Arg(flag, name, desc, req, true, v),
256 _value(val),
257 _default(val),
258 _typeDesc(Constraint<T>::shortID(constraint)),
259 _constraint(constraint) {}
260
261template <class T>
262ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
263 const std::string &desc, bool req, T val,
264 const Constraint<T> *constraint, ArgContainer &parser,
265 Visitor *v)
266 : Arg(flag, name, desc, req, true, v),
267 _value(val),
268 _default(val),
269 _typeDesc(Constraint<T>::shortID(constraint)),
270 _constraint(constraint) {
271 parser.add(this);
272}
273
277template <class T>
278bool ValueArg<T>::processArg(int *i, std::vector<std::string> &args) {
279 if (_hasBlanks(args[*i])) return false;
280
281 std::string flag = args[*i];
282
283 std::string value = "";
284 trimFlag(flag, value);
285 const bool hasAttachedValue = flag != args[*i];
286
287 if (!argMatches(flag)) return false;
288
289 if (_alreadySet) {
290 throw(CmdLineParseException("Argument already set!", toString()));
291 }
292
293 if (this->getDelimiter() != ' ' && !hasAttachedValue)
294 throw(ArgParseException(
295 "Couldn't find delimiter for this argument!", toString()));
296
297 if (!hasAttachedValue) {
298 _extractNextValue(i, args);
299 } else {
300 _extractValue(value);
301 }
302
303 _alreadySet = true;
304 _setBy = flag;
305 _checkWithVisitor();
306 return true;
307}
308
309template <class T>
310void ValueArg<T>::_extractNextValue(int *index,
311 std::vector<std::string> &args) {
312 (*index)++;
313 if (static_cast<unsigned int>(*index) < args.size())
314 _extractValue(args[*index]);
315 else
316 throw(ArgParseException("Missing a value for this argument!",
317 toString()));
318}
319
323template <class T>
324std::string ValueArg<T>::shortID(const std::string &) const {
325 return Arg::shortID("<" + _typeDesc + ">");
326}
327
331template <class T>
332std::string ValueArg<T>::longID(const std::string &) const {
333 return Arg::longID("<" + _typeDesc + ">");
334}
335
336template <class T>
337void ValueArg<T>::_extractValue(const std::string &val) {
338 try {
339 ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
340 } catch (ArgParseException &e) {
341 throw ArgParseException(e.error(), toString());
342 }
343
344 if (_constraint != NULL && !_constraint->check(_value))
345 throw(CmdLineParseException("Value '" + val +
346 +"' does not meet constraint: " +
347 _constraint->description(),
348 toString()));
349}
350
351template <class T>
353 Arg::reset();
354 _value = _default;
355}
356
357} // namespace TCLAP
358
359#endif // TCLAP_VALUE_ARG_H
Interface that allows adding an Arg to a "container".
virtual ArgContainer & add(Arg &a)=0
Adds an argument.
std::string error() const
Returns the error text.
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:66
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:581
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:817
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:568
Thrown from CmdLine when the arguments on the command line are not properly specified,...
The interface that defines the interaction between the Arg and Constraint.
Definition Constraint.h:41
The basic labeled argument that parses a value.
Definition ValueArg.h:45
virtual std::string longID(const std::string &val="val") const
Specialization of longID.
Definition ValueArg.h:332
T _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
Definition ValueArg.h:58
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition ValueArg.h:352
T _value
The value parsed from the command line.
Definition ValueArg.h:52
const Constraint< T > * _constraint
A Constraint this Arg must conform to.
Definition ValueArg.h:72
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition ValueArg.h:278
std::string _typeDesc
A human readable description of the type to be parsed.
Definition ValueArg.h:67
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:229
const T & getValue() const
Returns the value of the argument.
Definition ValueArg.h:193
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition ValueArg.h:337
virtual std::string shortID(const std::string &val="val") const
Specialization of shortID.
Definition ValueArg.h:324
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:52
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition Arg.h:470
A value like argument value type is a value that can be set using operator>>.
Definition ArgTraits.h:39