tclap 1.6.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 <utility>
33#include <vector>
34
35namespace TCLAP {
36
45template <class T>
46class ValueArg : public Arg {
47protected:
54
60
68 std::string _typeDesc;
69
74
81 void _extractValue(const std::string &val)
83
84public:
105 ValueArg(const std::string &flag, const std::string &name,
106 const std::string &desc, bool req, T value,
107 std::string typeDesc, Visitor *v = nullptr);
108
130 ValueArg(const std::string &flag, const std::string &name,
131 const std::string &desc, bool req, T value,
132 std::string typeDesc, ArgContainer &parser,
133 Visitor *v = nullptr);
134
154 ValueArg(const std::string &flag, const std::string &name,
155 const std::string &desc, bool req, T value,
156 const Constraint<T> *constraint, ArgContainer &parser,
157 Visitor *v = nullptr);
158
177 ValueArg(const std::string &flag, const std::string &name,
178 const std::string &desc, bool req, T value,
179 const Constraint<T> *constraint, Visitor *v = nullptr);
180
190 bool processArg(int *i, std::vector<std::string> &args) override;
191
195 const T &getValue() const noexcept { return _value; }
196
201 operator const T &() const noexcept { return getValue(); }
202
207 [[nodiscard]] std::string shortID(
208 const std::string &val = "val") const override;
209
214 [[nodiscard]] std::string longID(
215 const std::string &val = "val") const override;
216
217 void reset() override;
218
222 ValueArg(const ValueArg<T> &rhs) = delete;
223 ValueArg &operator=(const ValueArg<T> &rhs) = delete;
224
225private:
226 void _extractNextValue(int *index, std::vector<std::string> &args);
227};
228
232template <class T>
233ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
234 const std::string &desc, bool req, T val,
235 std::string typeDesc, Visitor *v)
236 : Arg(flag, name, desc, req, true, v),
237 _value(val),
238 _default(val),
239 _typeDesc(std::move(typeDesc)),
240 _constraint(nullptr) {}
241
242template <class T>
243ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
244 const std::string &desc, bool req, T val,
245 std::string typeDesc, ArgContainer &parser,
246 Visitor *v)
247 : Arg(flag, name, desc, req, true, v),
248 _value(val),
249 _default(val),
250 _typeDesc(std::move(typeDesc)),
251 _constraint(nullptr) {
252 parser.add(this);
253}
254
255template <class T>
256ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
257 const std::string &desc, bool req, T val,
258 const Constraint<T> *constraint, Visitor *v)
259 : Arg(flag, name, desc, req, true, v),
260 _value(val),
261 _default(val),
262 _typeDesc(Constraint<T>::shortID(constraint)),
263 _constraint(constraint) {}
264
265template <class T>
266ValueArg<T>::ValueArg(const std::string &flag, const std::string &name,
267 const std::string &desc, bool req, T val,
268 const Constraint<T> *constraint, ArgContainer &parser,
269 Visitor *v)
270 : Arg(flag, name, desc, req, true, v),
271 _value(val),
272 _default(val),
273 _typeDesc(Constraint<T>::shortID(constraint)),
274 _constraint(constraint) {
275 parser.add(this);
276}
277
281template <class T>
282bool ValueArg<T>::processArg(int *i, std::vector<std::string> &args) {
283 if (_hasBlanks(args[*i])) return false;
284
285 std::string flag = args[*i];
286
287 std::string value = "";
288 trimFlag(flag, value);
289 const bool hasAttachedValue = flag != args[*i];
290
291 if (!argMatches(flag)) return false;
292
293 if (_alreadySet) {
294 throw(CmdLineParseException("Argument already set!", toString()));
295 }
296
297 if (this->getDelimiter() != ' ' && !hasAttachedValue)
298 throw(ArgParseException("Couldn't find delimiter for this argument!",
299 toString()));
300
301 if (!hasAttachedValue) {
302 _extractNextValue(i, args);
303 } else {
304 _extractValue(value);
305 }
306
307 _alreadySet = true;
308 _setBy = flag;
309 _checkWithVisitor();
310 return true;
311}
312
313template <class T>
314void ValueArg<T>::_extractNextValue(int *index,
315 std::vector<std::string> &args) {
316 (*index)++;
317 if (static_cast<unsigned int>(*index) < args.size())
318 _extractValue(args[*index]);
319 else
320 throw(ArgParseException("Missing a value for this argument!",
321 toString()));
322}
323
327template <class T>
328std::string ValueArg<T>::shortID(const std::string &) const {
329 return Arg::shortID("<" + _typeDesc + ">");
330}
331
335template <class T>
336std::string ValueArg<T>::longID(const std::string &) const {
337 return Arg::longID("<" + _typeDesc + ">");
338}
339
340template <class T>
341void ValueArg<T>::_extractValue(const std::string &val)
343{
344 try {
345 ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
346 } catch (ArgParseException &e) {
347 throw ArgParseException(e.error(), toString());
348 }
349
350 if (_constraint != nullptr && !_constraint->check(_value))
352 "Value '" + val +
353 +"' does not meet constraint: " + _constraint->description(),
354 toString()));
355}
356
357template <class T>
359 Arg::reset();
360 _value = _default;
361}
362
363} // namespace TCLAP
364
365#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...
typename DefaultArgTrait< T >::ValueCategory ValueCategory
Definition ArgTraits.h:113
A virtual base class that defines the essential data for all arguments.
Definition Arg.h:69
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:603
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:823
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:590
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:46
ValueArg(const ValueArg< T > &rhs)=delete
Prevent accidental copying.
T _default
Used to support the reset() method so that ValueArg can be reset to their constructed value.
Definition ValueArg.h:59
bool processArg(int *i, std::vector< std::string > &args) override
Handles the processing of the argument.
Definition ValueArg.h:282
ValueArg & operator=(const ValueArg< T > &rhs)=delete
T _value
The value parsed from the command line.
Definition ValueArg.h:53
const Constraint< T > * _constraint
A Constraint this Arg must conform to.
Definition ValueArg.h:73
void reset() override
Clears the Arg object and allows it to be reused by new command lines.
Definition ValueArg.h:358
std::string longID(const std::string &val="val") const override
Specialization of longID.
Definition ValueArg.h:336
std::string shortID(const std::string &val="val") const override
Specialization of shortID.
Definition ValueArg.h:328
std::string _typeDesc
A human readable description of the type to be parsed.
Definition ValueArg.h:68
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition ValueArg.h:341
ValueArg(const std::string &flag, const std::string &name, const std::string &desc, bool req, T value, std::string typeDesc, Visitor *v=nullptr)
Labeled ValueArg constructor.
Definition ValueArg.h:233
const T & getValue() const noexcept
Returns the value of the argument.
Definition ValueArg.h:195
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:54
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition Arg.h:494