tclap 1.2.2
MultiArg.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * file: MultiArg.h
4 *
5 * Copyright (c) 2003, Michael E. Smoot .
6 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
7 * All rights reserved.
8 *
9 * See the file COPYING in the top directory of this distribution for
10 * more information.
11 *
12 * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 * DEALINGS IN THE SOFTWARE.
19 *
20 *****************************************************************************/
21
22
23#ifndef TCLAP_MULTIPLE_ARGUMENT_H
24#define TCLAP_MULTIPLE_ARGUMENT_H
25
26#include <string>
27#include <vector>
28
29#include <tclap/Arg.h>
30#include <tclap/Constraint.h>
31
32namespace TCLAP {
38template<class T>
39class MultiArg : public Arg
40{
41public:
42 typedef std::vector<T> container_type;
43 typedef typename container_type::iterator iterator;
44 typedef typename container_type::const_iterator const_iterator;
45
46protected:
47
51 std::vector<T> _values;
52
56 std::string _typeDesc;
57
62
69 void _extractValue( const std::string& val );
70
75
76public:
77
95 MultiArg( const std::string& flag,
96 const std::string& name,
97 const std::string& desc,
98 bool req,
99 const std::string& typeDesc,
100 Visitor* v = NULL);
101
120 MultiArg( const std::string& flag,
121 const std::string& name,
122 const std::string& desc,
123 bool req,
124 const std::string& typeDesc,
125 CmdLineInterface& parser,
126 Visitor* v = NULL );
127
143 MultiArg( const std::string& flag,
144 const std::string& name,
145 const std::string& desc,
146 bool req,
147 Constraint<T>* constraint,
148 Visitor* v = NULL );
149
166 MultiArg( const std::string& flag,
167 const std::string& name,
168 const std::string& desc,
169 bool req,
170 Constraint<T>* constraint,
171 CmdLineInterface& parser,
172 Visitor* v = NULL );
173
182 virtual bool processArg(int* i, std::vector<std::string>& args);
183
188 const std::vector<T>& getValue();
189
194 const_iterator begin() const { return _values.begin(); }
195
200 const_iterator end() const { return _values.end(); }
201
206 virtual std::string shortID(const std::string& val="val") const;
207
212 virtual std::string longID(const std::string& val="val") const;
213
218 virtual bool isRequired() const;
219
220 virtual bool allowMore();
221
222 virtual void reset();
223
224private:
228 MultiArg<T>(const MultiArg<T>& rhs);
229 MultiArg<T>& operator=(const MultiArg<T>& rhs);
230
231};
232
233template<class T>
234MultiArg<T>::MultiArg(const std::string& flag,
235 const std::string& name,
236 const std::string& desc,
237 bool req,
238 const std::string& typeDesc,
239 Visitor* v) :
240 Arg( flag, name, desc, req, true, v ),
241 _values(std::vector<T>()),
242 _typeDesc( typeDesc ),
243 _constraint( NULL ),
244 _allowMore(false)
245{
247}
248
249template<class T>
250MultiArg<T>::MultiArg(const std::string& flag,
251 const std::string& name,
252 const std::string& desc,
253 bool req,
254 const std::string& typeDesc,
255 CmdLineInterface& parser,
256 Visitor* v)
257: Arg( flag, name, desc, req, true, v ),
258 _values(std::vector<T>()),
259 _typeDesc( typeDesc ),
260 _constraint( NULL ),
261 _allowMore(false)
262{
263 parser.add( this );
265}
266
270template<class T>
271MultiArg<T>::MultiArg(const std::string& flag,
272 const std::string& name,
273 const std::string& desc,
274 bool req,
275 Constraint<T>* constraint,
276 Visitor* v)
277: Arg( flag, name, desc, req, true, v ),
278 _values(std::vector<T>()),
279 _typeDesc( constraint->shortID() ),
280 _constraint( constraint ),
281 _allowMore(false)
282{
284}
285
286template<class T>
287MultiArg<T>::MultiArg(const std::string& flag,
288 const std::string& name,
289 const std::string& desc,
290 bool req,
291 Constraint<T>* constraint,
292 CmdLineInterface& parser,
293 Visitor* v)
294: Arg( flag, name, desc, req, true, v ),
295 _values(std::vector<T>()),
296 _typeDesc( constraint->shortID() ),
297 _constraint( constraint ),
298 _allowMore(false)
299{
300 parser.add( this );
302}
303
304template<class T>
305const std::vector<T>& MultiArg<T>::getValue() { return _values; }
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 if ( Arg::delimiter() != ' ' && value == "" )
324 throw( ArgParseException(
325 "Couldn't find delimiter for this argument!",
326 toString() ) );
327
328 // always take the first one, regardless of start string
329 if ( value == "" )
330 {
331 (*i)++;
332 if ( static_cast<unsigned int>(*i) < args.size() )
333 _extractValue( args[*i] );
334 else
335 throw( ArgParseException("Missing a value for this argument!",
336 toString() ) );
337 }
338 else
339 _extractValue( value );
340
341 /*
342 // continuing taking the args until we hit one with a start string
343 while ( (unsigned int)(*i)+1 < args.size() &&
344 args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
345 args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
346 _extractValue( args[++(*i)] );
347 */
348
349 _alreadySet = true;
351
352 return true;
353 }
354 else
355 return false;
356}
357
361template<class T>
362std::string MultiArg<T>::shortID(const std::string& val) const
363{
364 static_cast<void>(val); // Ignore input, don't warn
365 return Arg::shortID(_typeDesc) + " ... ";
366}
367
371template<class T>
372std::string MultiArg<T>::longID(const std::string& val) const
373{
374 static_cast<void>(val); // Ignore input, don't warn
375 return Arg::longID(_typeDesc) + " (accepted multiple times)";
376}
377
382template<class T>
384{
385 if ( _required )
386 {
387 if ( _values.size() > 1 )
388 return false;
389 else
390 return true;
391 }
392 else
393 return false;
394
395}
396
397template<class T>
398void MultiArg<T>::_extractValue( const std::string& val )
399{
400 try {
401 T tmp;
402 ExtractValue(tmp, val, typename ArgTraits<T>::ValueCategory());
403 _values.push_back(tmp);
404 } catch( ArgParseException &e) {
405 throw ArgParseException(e.error(), toString());
406 }
407
408 if ( _constraint != NULL )
409 if ( ! _constraint->check( _values.back() ) )
410 throw( CmdLineParseException( "Value '" + val +
411 "' does not meet constraint: " +
412 _constraint->description(),
413 toString() ) );
414}
415
416template<class T>
418{
419 bool am = _allowMore;
420 _allowMore = true;
421 return am;
422}
423
424template<class T>
426{
427 Arg::reset();
428 _values.clear();
429}
430
431} // namespace TCLAP
432
433#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...
void _checkWithVisitor() const
Performs the special handling described by the Visitor.
Definition Arg.h:612
bool _acceptsMultipleValues
Definition Arg.h:159
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition Arg.h:524
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:642
static bool ignoreRest()
Whether to ignore the rest.
Definition Arg.h:206
bool _alreadySet
Indicates whether the argument has been set.
Definition Arg.h:138
bool _ignoreable
Whether this argument can be ignored, if desired.
Definition Arg.h:151
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition Arg.h:212
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition Arg.h:680
virtual bool argMatches(const std::string &s) const
A method that tests whether a string matches this argument.
Definition Arg.h:591
bool _required
Indicating whether the argument is required.
Definition Arg.h:118
virtual void trimFlag(std::string &flag, std::string &value) const
Trims a value off of the flag.
Definition Arg.h:621
virtual std::string toString() const
Returns a simple string representation of the argument.
Definition Arg.h:600
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition Arg.h:506
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:39
An argument that allows multiple values of type T to be specified.
Definition MultiArg.h:40
std::string _typeDesc
The description of type T to be used in the usage.
Definition MultiArg.h:56
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition MultiArg.h:398
container_type::iterator iterator
Definition MultiArg.h:43
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:44
const_iterator end() const
Returns the end of the values parsed from the command line.
Definition MultiArg.h:200
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:234
virtual bool isRequired() const
Once we've matched the first value, then the arg is no longer required.
Definition MultiArg.h:383
const_iterator begin() const
Returns an iterator over the values parsed from the command line.
Definition MultiArg.h:194
bool _allowMore
Used by XorHandler to decide whether to keep parsing for this arg.
Definition MultiArg.h:74
std::vector< T > _values
The list of values parsed from the CmdLine.
Definition MultiArg.h:51
virtual bool allowMore()
Used for MultiArgs and XorHandler to determine whether args can still be set.
Definition MultiArg.h:417
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition MultiArg.h:425
virtual std::string shortID(const std::string &val="val") const
Returns the a short id string.
Definition MultiArg.h:362
std::vector< T > container_type
Definition MultiArg.h:42
virtual std::string longID(const std::string &val="val") const
Returns the a long id string.
Definition MultiArg.h:372
Constraint< T > * _constraint
A list of constraint on this Arg.
Definition MultiArg.h:61
const std::vector< T > & getValue()
Returns a vector of type T containing the values parsed from the command line.
Definition MultiArg.h:305
A base class that defines the interface for visitors.
Definition Visitor.h:32
Definition Arg.h:58
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition Arg.h:416
T::ValueCategory ValueCategory
Definition ArgTraits.h:80