tclap 1.4.0
MultiArg.h
Go to the documentation of this file.
1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2
3/******************************************************************************
4 *
5 * file: MultiArg.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_MULTI_ARG_H
26#define TCLAP_MULTI_ARG_H
27
28#include <tclap/Arg.h>
29#include <tclap/Constraint.h>
30
31#include <string>
32#include <vector>
33
34namespace TCLAP {
40template <class T>
41class MultiArg : public Arg {
42public:
43 typedef std::vector<T> container_type;
44 typedef typename container_type::iterator iterator;
45 typedef typename container_type::const_iterator const_iterator;
46
47protected:
51 std::vector<T> _values;
52
56 std::string _typeDesc;
57
62
69 void _extractValue(const std::string &val);
70
76
77public:
95 MultiArg(const std::string &flag, const std::string &name,
96 const std::string &desc, bool req, const std::string &typeDesc,
97 Visitor *v = NULL);
98
117 MultiArg(const std::string &flag, const std::string &name,
118 const std::string &desc, bool req, const std::string &typeDesc,
119 ArgContainer &parser, Visitor *v = NULL);
120
136 MultiArg(const std::string &flag, const std::string &name,
137 const std::string &desc, bool req, const Constraint<T> *constraint,
138 Visitor *v = NULL);
139
156 MultiArg(const std::string &flag, const std::string &name,
157 const std::string &desc, bool req, const Constraint<T> *constraint,
158 ArgContainer &parser, Visitor *v = NULL);
159
168 virtual bool processArg(int *i, std::vector<std::string> &args);
169
174 const std::vector<T> &getValue() const { return _values; }
175
180 const_iterator begin() const { return _values.begin(); }
181
186 const_iterator end() const { return _values.end(); }
187
192 virtual std::string shortID(const std::string &val = "val") const;
193
198 virtual std::string longID(const std::string &val = "val") const;
199
200 virtual bool allowMore();
201
202 virtual void reset();
203
204private:
205 void _extractNextValue(int *index, std::vector<std::string> &args);
206
210 MultiArg(const MultiArg<T>& rhs);
211 MultiArg& operator=(const MultiArg<T>& rhs);
212};
213
214template <class T>
215MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
216 const std::string &desc, bool req,
217 const std::string &typeDesc, Visitor *v)
218 : Arg(flag, name, desc, req, true, v),
219 _values(std::vector<T>()),
220 _typeDesc(typeDesc),
221 _constraint(NULL),
222 _allowMore(false) {
224}
225
226template <class T>
227MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
228 const std::string &desc, bool req,
229 const std::string &typeDesc, ArgContainer &parser,
230 Visitor *v)
231 : Arg(flag, name, desc, req, true, v),
232 _values(std::vector<T>()),
233 _typeDesc(typeDesc),
234 _constraint(NULL),
235 _allowMore(false) {
236 parser.add(this);
238}
239
243template <class T>
244MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
245 const std::string &desc, bool req,
246 const Constraint<T> *constraint, Visitor *v)
247 : Arg(flag, name, desc, req, true, v),
248 _values(std::vector<T>()),
249 _typeDesc(Constraint<T>::shortID(constraint)),
250 _constraint(constraint),
251 _allowMore(false) {
253}
254
255template <class T>
256MultiArg<T>::MultiArg(const std::string &flag, const std::string &name,
257 const std::string &desc, bool req,
258 const Constraint<T> *constraint, ArgContainer &parser,
259 Visitor *v)
260 : Arg(flag, name, desc, req, true, v),
261 _values(std::vector<T>()),
262 _typeDesc(Constraint<T>::shortID(constraint)),
263 _constraint(constraint),
264 _allowMore(false) {
265 parser.add(this);
267}
268
269template <class T>
270bool MultiArg<T>::processArg(int *i, std::vector<std::string> &args) {
271 if (_hasBlanks(args[*i])) return false;
272
273 std::string flag = args[*i];
274 std::string value = "";
275
276 trimFlag(flag, value);
277 const bool hasAttachedValue = flag != args[*i];
278
279 if (!argMatches(flag)) return false;
280
281 if (this->getDelimiter() != ' ' && !hasAttachedValue)
282 throw(ArgParseException(
283 "Couldn't find delimiter for this argument!", toString()));
284
285 if (!hasAttachedValue) {
286 _extractNextValue(i, args);
287 } else {
288 _extractValue(value);
289 }
290
291 _alreadySet = true;
292 _setBy = flag;
293 _checkWithVisitor();
294
295 return true;
296}
297
298template <class T>
299void MultiArg<T>::_extractNextValue(int *index,
300 std::vector<std::string> &args) {
301 (*index)++;
302 if (static_cast<unsigned int>(*index) < args.size())
303 _extractValue(args[*index]);
304 else
305 throw(ArgParseException("Missing a value for this argument!",
306 toString()));
307}
308
312template <class T>
313std::string MultiArg<T>::shortID(const std::string &val) const {
314 static_cast<void>(val); // Ignore input, don't warn
315 return Arg::shortID("<" + _typeDesc + ">") + " ...";
316}
317
321template <class T>
322std::string MultiArg<T>::longID(const std::string &val) const {
323 static_cast<void>(val); // Ignore input, don't warn
324 return Arg::longID("<" + _typeDesc + ">") + " (accepted multiple times)";
325}
326
327template <class T>
328void MultiArg<T>::_extractValue(const std::string &val) {
329 try {
330 T tmp;
331 ExtractValue(tmp, val, typename ArgTraits<T>::ValueCategory());
332 _values.push_back(tmp);
333 } catch (ArgParseException &e) {
334 throw ArgParseException(e.error(), toString());
335 }
336
337 if (_constraint != NULL && !_constraint->check(_values.back()))
339 "Value '" + val +
340 "' does not meet constraint: " + _constraint->description(),
341 toString()));
342}
343
344template <class T>
346 bool am = _allowMore;
347 _allowMore = true;
348 return am;
349}
350
351template <class T>
353 Arg::reset();
354 _values.clear();
355}
356
357} // namespace TCLAP
358
359#endif // TCLAP_MULTI_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
bool _acceptsMultipleValues
Definition Arg.h:177
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
An argument that allows multiple values of type T to be specified.
Definition MultiArg.h:41
std::string _typeDesc
The description of type T to be used in the usage.
Definition MultiArg.h:56
const std::vector< T > & getValue() const
Returns a vector of type T containing the values parsed from the command line.
Definition MultiArg.h:174
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition MultiArg.h:328
container_type::iterator iterator
Definition MultiArg.h:44
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition MultiArg.h:270
container_type::const_iterator const_iterator
Definition MultiArg.h:45
const_iterator end() const
Returns the end of the values parsed from the command line.
Definition MultiArg.h:186
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:215
const_iterator begin() const
Returns an iterator over the values parsed from the command line.
Definition MultiArg.h:180
bool _allowMore
Used by MultiArg to decide whether to keep parsing for this arg.
Definition MultiArg.h:75
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition MultiArg.h:51
virtual bool allowMore()
Used for MultiArgs to determine whether args can still be set.
Definition MultiArg.h:345
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition MultiArg.h:352
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
Definition MultiArg.h:313
std::vector< T > container_type
Definition MultiArg.h:43
const Constraint< T > * _constraint
A constraint that the values of this Arg must conform to.
Definition MultiArg.h:61
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
Definition MultiArg.h:322
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