tclap 1.2.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ValueArg.h
Go to the documentation of this file.
1 /******************************************************************************
2 *
3 * file: ValueArg.h
4 *
5 * Copyright (c) 2003, Michael E. Smoot .
6 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
7 * All rights reverved.
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_VALUE_ARGUMENT_H
24 #define TCLAP_VALUE_ARGUMENT_H
25
26 #include <string>
27 #include <vector>
28
29 #include <tclap/Arg.h>
30 #include <tclap/Constraint.h>
31
32 namespace TCLAP {
33
42 template<class T>
43 class ValueArg : public Arg
44 {
45 protected:
46
52 T _value;
53
58 T _default;
59
67 std::string _typeDesc;
68
72 Constraint<T>* _constraint;
73
80 void _extractValue( const std::string& val );
81
82 public:
83
107 ValueArg( const std::string& flag,
108 const std::string& name,
109 const std::string& desc,
110 bool req,
111 T value,
112 const std::string& typeDesc,
113 Visitor* v = NULL);
114
115
140 ValueArg( const std::string& flag,
141 const std::string& name,
142 const std::string& desc,
143 bool req,
144 T value,
145 const std::string& typeDesc,
146 CmdLineInterface& parser,
147 Visitor* v = NULL );
148
171 ValueArg( const std::string& flag,
172 const std::string& name,
173 const std::string& desc,
174 bool req,
175 T value,
176 Constraint<T>* constraint,
177 CmdLineInterface& parser,
178 Visitor* v = NULL );
179
201 ValueArg( const std::string& flag,
202 const std::string& name,
203 const std::string& desc,
204 bool req,
205 T value,
206 Constraint<T>* constraint,
207 Visitor* v = NULL );
208
218 virtual bool processArg(int* i, std::vector<std::string>& args);
219
223 T& getValue() ;
224
229 virtual std::string shortID(const std::string& val = "val") const;
230
235 virtual std::string longID(const std::string& val = "val") const;
236
237 virtual void reset() ;
238
239 private:
243 ValueArg<T>(const ValueArg<T>& rhs);
244 ValueArg<T>& operator=(const ValueArg<T>& rhs);
245 };
246
247
251 template<class T>
252 ValueArg<T>::ValueArg(const std::string& flag,
253 const std::string& name,
254 const std::string& desc,
255 bool req,
256 T val,
257 const std::string& typeDesc,
258 Visitor* v)
259 : Arg(flag, name, desc, req, true, v),
260 _value( val ),
261 _default( val ),
262 _typeDesc( typeDesc ),
263 _constraint( NULL )
264 { }
265
266 template<class T>
267 ValueArg<T>::ValueArg(const std::string& flag,
268 const std::string& name,
269 const std::string& desc,
270 bool req,
271 T val,
272 const std::string& typeDesc,
273 CmdLineInterface& parser,
274 Visitor* v)
275 : Arg(flag, name, desc, req, true, v),
276 _value( val ),
277 _default( val ),
278 _typeDesc( typeDesc ),
279 _constraint( NULL )
280 {
281 parser.add( this );
282 }
283
284 template<class T>
285 ValueArg<T>::ValueArg(const std::string& flag,
286 const std::string& name,
287 const std::string& desc,
288 bool req,
289 T val,
290 Constraint<T>* constraint,
291 Visitor* v)
292 : Arg(flag, name, desc, req, true, v),
293 _value( val ),
294 _default( val ),
295 _typeDesc( constraint->shortID() ),
296 _constraint( constraint )
297 { }
298
299 template<class T>
300 ValueArg<T>::ValueArg(const std::string& flag,
301 const std::string& name,
302 const std::string& desc,
303 bool req,
304 T val,
305 Constraint<T>* constraint,
306 CmdLineInterface& parser,
307 Visitor* v)
308 : Arg(flag, name, desc, req, true, v),
309 _value( val ),
310 _default( val ),
311 _typeDesc( constraint->shortID() ),
312 _constraint( constraint )
313 {
314 parser.add( this );
315 }
316
317
321 template<class T>
322 T& ValueArg<T>::getValue() { return _value; }
323
327 template<class T>
328 bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
329 {
330 if ( _ignoreable && Arg::ignoreRest() )
331 return false;
332
333 if ( _hasBlanks( args[*i] ) )
334 return false;
335
336 std::string flag = args[*i];
337
338 std::string value = "";
339 trimFlag( flag, value );
340
341 if ( argMatches( flag ) )
342 {
343 if ( _alreadySet )
344 {
345 if ( _xorSet )
346 throw( CmdLineParseException(
347 "Mutually exclusive argument already set!",
348 toString()) );
349 else
350 throw( CmdLineParseException("Argument already set!",
351 toString()) );
352 }
353
354 if ( Arg::delimiter() != ' ' && value == "" )
355 throw( ArgParseException(
356 "Couldn't find delimiter for this argument!",
357 toString() ) );
358
359 if ( value == "" )
360 {
361 (*i)++;
362 if ( static_cast<unsigned int>(*i) < args.size() )
363 _extractValue( args[*i] );
364 else
365 throw( ArgParseException("Missing a value for this argument!",
366 toString() ) );
367 }
368 else
369 _extractValue( value );
370
371 _alreadySet = true;
372 _checkWithVisitor();
373 return true;
374 }
375 else
376 return false;
377 }
378
382 template<class T>
383 std::string ValueArg<T>::shortID(const std::string& val) const
384 {
385 static_cast<void>(val); // Ignore input, don't warn
386 return Arg::shortID( _typeDesc );
387 }
388
392 template<class T>
393 std::string ValueArg<T>::longID(const std::string& val) const
394 {
395 static_cast<void>(val); // Ignore input, don't warn
396 return Arg::longID( _typeDesc );
397 }
398
399 template<class T>
400 void ValueArg<T>::_extractValue( const std::string& val )
401 {
402 try {
403 ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
404 } catch( ArgParseException &e) {
405 throw ArgParseException(e.error(), toString());
406 }
407
408 if ( _constraint != NULL )
409 if ( ! _constraint->check( _value ) )
410 throw( CmdLineParseException( "Value '" + val +
411 + "' does not meet constraint: "
412 + _constraint->description(),
413 toString() ) );
414 }
415
416 template<class T>
417 void ValueArg<T>::reset()
418 {
419 Arg::reset();
420 _value = _default;
421 }
422
423 } // namespace TCLAP
424
425 #endif
TCLAP::ValueArg::longID
virtual std::string longID(const std::string &val="val") const
Specialization of longID.
Definition: ValueArg.h:393
TCLAP::ValueArg::_extractValue
void _extractValue(const std::string &val)
Extracts the value from the string.
Definition: ValueArg.h:400
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:64
TCLAP::Arg::longID
virtual std::string longID(const std::string &valueId="val") const
Returns a long ID for the usage.
Definition: Arg.h:523
TCLAP::CmdLineParseException
Thrown from CmdLine when the arguments on the command line are not properly specified, e.g.
Definition: ArgException.h:143
TCLAP::ValueArg::shortID
virtual std::string shortID(const std::string &val="val") const
Specialization of shortID.
Definition: ValueArg.h:383
TCLAP::Arg::reset
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: Arg.h:679
TCLAP::ValueArg
The basic labeled argument that parses a value.
Definition: ValueArg.h:43
TCLAP::Arg::delimiter
static char delimiter()
The delimiter that separates an argument flag/name from the value.
Definition: Arg.h:211
TCLAP::ValueArg::_value
T _value
The value parsed from the command line.
Definition: ValueArg.h:52
TCLAP::Constraint
The interface that defines the interaction between the Arg and Constraint.
Definition: Constraint.h:38
TCLAP::ArgException::error
std::string error() const
Returns the error text.
Definition: ArgException.h:64
TCLAP::Arg::shortID
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:505
TCLAP::ValueArg::_constraint
Constraint< T > * _constraint
A Constraint this Arg must conform to.
Definition: ValueArg.h:72
TCLAP::Visitor
A base class that defines the interface for visitors.
Definition: Visitor.h:31
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:43
Constraint.h
TCLAP::ValueArg::getValue
T & getValue()
Returns the value of the argument.
Definition: ValueArg.h:322
TCLAP::CmdLineInterface::add
virtual void add(Arg &a)=0
Adds an argument to the list of arguments to be parsed.
TCLAP::ValueArg::_typeDesc
std::string _typeDesc
A human readable description of the type to be parsed.
Definition: ValueArg.h:67
Arg.h
TCLAP::Arg::ignoreRest
static bool ignoreRest()
Whether to ignore the rest.
Definition: Arg.h:205
TCLAP::ArgTraits::ValueCategory
T::ValueCategory ValueCategory
Definition: ArgTraits.h:80
TCLAP::ExtractValue
void ExtractValue(T &destVal, const std::string &strVal, ValueLike vl)
Definition: Arg.h:415
TCLAP::ArgParseException
Thrown from within the child Arg classes when it fails to properly parse the argument it has been pas...
Definition: ArgException.h:121
TCLAP::ValueArg::_default
T _default
Used to support the reset() method so that ValueArg can be reset to their constructed value...
Definition: ValueArg.h:58
TCLAP::ValueArg::processArg
virtual bool processArg(int *i, std::vector< std::string > &args)
Handles the processing of the argument.
Definition: ValueArg.h:328
TCLAP
Definition: Arg.h:57
TCLAP::ValueArg::reset
virtual void reset()
Clears the Arg object and allows it to be reused by new command lines.
Definition: ValueArg.h:417
TCLAP::ValueArg::ValueArg
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:252