tclap 1.2.6
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
26#ifndef TCLAP_MULTIPLE_ARGUMENT_H
27#define TCLAP_MULTIPLE_ARGUMENT_H
28
29#include <string>
30#include <vector>
31
32#include <tclap/Arg.h>
33#include <tclap/Constraint.h>
34
35namespace TCLAP {
41template<class T>
42class MultiArg : public Arg
43{
44public:
45 typedef std::vector<T> container_type;
46 typedef typename container_type::iterator iterator;
47 typedef typename container_type::const_iterator const_iterator;
48
49protected:
50
54 std::vector<T> _values;
55
59 std::string _typeDesc;
60
65
72 void _extractValue( const std::string& val );
73
78
79public:
80
98 MultiArg( const std::string& flag,
99 const std::string& name,
100 const std::string& desc,
101 bool req,
102 const std::string& typeDesc,
103 Visitor* v = NULL);
104
123 MultiArg( const std::string& flag,
124 const std::string& name,
125 const std::string& desc,
126 bool req,
127 const std::string& typeDesc,
128 CmdLineInterface& parser,
129 Visitor* v = NULL );
130
146 MultiArg( const std::string& flag,
147 const std::string& name,
148 const std::string& desc,
149 bool req,
150 Constraint<T>* constraint,
151 Visitor* v = NULL );
152
169 MultiArg( const std::string& flag,
170 const std::string& name,
171 const std::string& desc,
172 bool req,
173 Constraint<T>* constraint,
174 CmdLineInterface& parser,
175 Visitor* v = NULL );
176
185 virtual bool processArg(int* i, std::vector<std::string>& args);
186
191 const std::vector<T>& getValue() const { return _values; }
192
197 const_iterator begin() const { return _values.begin(); }
198
203 const_iterator end() const { return _values.end(); }
204
209 virtual std::string shortID(const std::string& val="val") const;
210
215 virtual std::string longID(const std::string& val="val") const;
216
221 virtual bool isRequired() const;
222
223 virtual bool allowMore();
224
225 virtual void reset();
226
227private:
231 MultiArg(const MultiArg<T>& rhs);
232 MultiArg& operator=(const MultiArg<T>& rhs);
233
234};
235
236template<class T>
237MultiArg<T>::MultiArg(const std::string& flag,
238 const std::string& name,
239 const std::string& desc,
240 bool req,
241 const std::string& typeDesc,
242 Visitor* v) :
243 Arg( flag, name, desc, req, true, v ),
244 _values(std::vector<T>()),
245 _typeDesc( typeDesc ),
246 _constraint( NULL ),
247 _allowMore(false)
248{
250}
251
252template<class T>
253MultiArg<T>::MultiArg(const std::string& flag,
254 const std::string& name,
255 const std::string& desc,
256 bool req,
257 const std::string& typeDesc,
258 CmdLineInterface& parser,
259 Visitor* v)
260: Arg( flag, name, desc, req, true, v ),
261 _values(std::vector<T>()),
262 _typeDesc( typeDesc ),
263 _constraint( NULL ),
264 _allowMore(false)
265{
266 parser.add( this );
268}
269
273template<class T>
274MultiArg<T>::MultiArg(const std::string& flag,
275 const std::string& name,
276 const std::string& desc,
277 bool req,
278 Constraint<T>* constraint,
279 Visitor* v)
280: Arg( flag, name, desc, req, true, v ),
281 _values(std::vector<T>()),
282 _typeDesc( Constraint<T>::shortID(constraint) ),
283 _constraint( constraint ),
284 _allowMore(false)
285{
287}
288
289template<class T>
290MultiArg<T>::MultiArg(const std::string& flag,
291 const std::string& name,
292 const std::string& desc,
293 bool req,
294 Constraint<T>* constraint,
295 CmdLineInterface& parser,
296 Visitor* v)
297: Arg( flag, name, desc, req, true, v ),
298 _values(std::vector<T>()),
299 _typeDesc( Constraint<T>::shortID(constraint) ),
300 _constraint( constraint ),
301 _allowMore(false)
302{
303 parser.add( this );
305}
306
307template<class T>
308bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
309{
310 if ( _ignoreable && Arg::ignoreRest() )
311 return false;
312
313 if ( _hasBlanks( args[*i] ) )
314 return false;
315
316 std::string flag = args[*i];
317 std::string value = "";
318
319 trimFlag( flag, value );
320
321 if ( argMatches( flag ) )
322 {
323 // Legacy behavior: an empty attached value is treated as a missing
324 // delimiter. This branch deliberately retains that parsing behavior.
325 if ( Arg::delimiter() != ' ' && value == "" )
326 throw( ArgParseException(
327 "Couldn't find delimiter for this argument!",
328 toString() ) );
329
330 // always take the first one, regardless of start string
331 if ( value == "" )
332 {
333 (*i)++;
334 if ( static_cast<unsigned int>(*i) < args.size() )
335 _extractValue( args[*i] );
336 else
337 throw( ArgParseException("Missing a value for this argument!",
338 toString() ) );
339 }
340 else
341 _extractValue( value );
342
343 /*
344 // continuing taking the args until we hit one with a start string
345 while ( (unsigned int)(*i)+1 < args.size() &&
346 args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
347 args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
348 _extractValue( args[++(*i)] );
349 */
350
351 _alreadySet = true;
352 _checkWithVisitor();
353
354 return true;
355 }
356 else
357 return false;
358}
359
363template<class T>
364std::string MultiArg<T>::shortID(const std::string& val) const
365{
366 static_cast<void>(val); // Ignore input, don't warn
367 return Arg::shortID(_typeDesc) + " ...";
368}
369
373template<class T>
374std::string MultiArg<T>::longID(const std::string& val) const
375{
376 static_cast<void>(val); // Ignore input, don't warn
377 return Arg::longID(_typeDesc) + " (accepted multiple times)";
378}
379
384template<class T>
386{
387 if ( _required )
388 {
389 if ( _values.size() > 1 )
390 return false;
391 else
392 return true;
393 }
394 else
395 return false;
396
397}
398
399template<class T>
400void MultiArg<T>::_extractValue( const std::string& val )
401{
402 try {
403 T tmp;
404 ExtractValue(tmp, val, typename ArgTraits<T>::ValueCategory());
405 _values.push_back(tmp);
406 } catch( ArgParseException &e) {
407 throw ArgParseException(e.error(), toString());
408 }
409
410 if ( _constraint != NULL )
411 if ( ! _constraint->check( _values.back() ) )
412 throw( CmdLineParseException( "Value '" + val +
413 "' does not meet constraint: " +
414 _constraint->description(),
415 toString() ) );
416}
417
418template<class T>
420{
421 bool am = _allowMore;
422 _allowMore = true;
423 return am;
424}
425
426template<class T>
428{
429 // Legacy XOR bookkeeping: _allowMore survives reset(). After one parse,
430 // allowMore() can report this MultiArg as already counted, so a reused
431 // XOR parser may report a required argument missing. This branch retains
432 // that behavior; construct a fresh parser and arguments for another parse.
433 Arg::reset();
434 _values.clear();
435}
436
437} // namespace TCLAP
438
439#endif
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:56
bool _acceptsMultipleValues
Definition Arg.h:149
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:520
static bool ignoreRest()
Whether to ignore the rest.
Definition Arg.h:199
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition Arg.h:205
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:676
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:502
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
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:43
An argument that allows multiple values of type T to be specified.
Definition MultiArg.h:43
std::string _typeDesc
The description of type T to be used in the usage.
Definition MultiArg.h:59
const std::vector< T > & getValue() const
Returns a vector of type T containing the values parsed from the command line.
Definition MultiArg.h:191
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition MultiArg.h:400
container_type::iterator iterator
Definition MultiArg.h:46
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition MultiArg.h:308
container_type::const_iterator const_iterator
Definition MultiArg.h:47
const_iterator end() const
Returns the end of the values parsed from the command line.
Definition MultiArg.h:203
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:237
virtual bool isRequired() const
Once we've matched the first value, then the arg is no longer required.
Definition MultiArg.h:385
const_iterator begin() const
Returns an iterator over the values parsed from the command line.
Definition MultiArg.h:197
bool _allowMore
Used by XorHandler to decide whether to keep parsing for this arg.
Definition MultiArg.h:77
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition MultiArg.h:54
virtual bool allowMore()
Used for MultiArgs and XorHandler to determine whether args can still be set.
Definition MultiArg.h:419
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition MultiArg.h:427
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
Definition MultiArg.h:364
std::vector< T > container_type
Definition MultiArg.h:45
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
Definition MultiArg.h:374
Constraint< T > * _constraint
A list of constraint on this Arg.
Definition MultiArg.h:64
A base class that defines the interface for visitors.
Definition Visitor.h:35
Definition Arg.h:48
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition Arg.h:412
A value like argument value type is a value that can be set using operator>>.
Definition ArgTraits.h:39