tclap  1.2.0
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 
59 
67  std::string _typeDesc;
68 
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 };
240 
241 
245 template<class T>
246 ValueArg<T>::ValueArg(const std::string& flag,
247  const std::string& name,
248  const std::string& desc,
249  bool req,
250  T val,
251  const std::string& typeDesc,
252  Visitor* v)
253 : Arg(flag, name, desc, req, true, v),
254  _value( val ),
255  _default( val ),
256  _typeDesc( typeDesc ),
257  _constraint( NULL )
258 { }
259 
260 template<class T>
261 ValueArg<T>::ValueArg(const std::string& flag,
262  const std::string& name,
263  const std::string& desc,
264  bool req,
265  T val,
266  const std::string& typeDesc,
267  CmdLineInterface& parser,
268  Visitor* v)
269 : Arg(flag, name, desc, req, true, v),
270  _value( val ),
271  _default( val ),
272  _typeDesc( typeDesc ),
273  _constraint( NULL )
274 {
275  parser.add( this );
276 }
277 
278 template<class T>
279 ValueArg<T>::ValueArg(const std::string& flag,
280  const std::string& name,
281  const std::string& desc,
282  bool req,
283  T val,
284  Constraint<T>* constraint,
285  Visitor* v)
286 : Arg(flag, name, desc, req, true, v),
287  _value( val ),
288  _default( val ),
289  _typeDesc( constraint->shortID() ),
290  _constraint( constraint )
291 { }
292 
293 template<class T>
294 ValueArg<T>::ValueArg(const std::string& flag,
295  const std::string& name,
296  const std::string& desc,
297  bool req,
298  T val,
299  Constraint<T>* constraint,
300  CmdLineInterface& parser,
301  Visitor* v)
302 : Arg(flag, name, desc, req, true, v),
303  _value( val ),
304  _default( val ),
305  _typeDesc( constraint->shortID() ),
306  _constraint( constraint )
307 {
308  parser.add( this );
309 }
310 
311 
315 template<class T>
316 T& ValueArg<T>::getValue() { return _value; }
317 
321 template<class T>
322 bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
323 {
324  if ( _ignoreable && Arg::ignoreRest() )
325  return false;
326 
327  if ( _hasBlanks( args[*i] ) )
328  return false;
329 
330  std::string flag = args[*i];
331 
332  std::string value = "";
333  trimFlag( flag, value );
334 
335  if ( argMatches( flag ) )
336  {
337  if ( _alreadySet )
338  throw( CmdLineParseException("Argument already set!", toString()) );
339 
340  if ( Arg::delimiter() != ' ' && value == "" )
341  throw( ArgParseException(
342  "Couldn't find delimiter for this argument!",
343  toString() ) );
344 
345  if ( value == "" )
346  {
347  (*i)++;
348  if ( static_cast<unsigned int>(*i) < args.size() )
349  _extractValue( args[*i] );
350  else
351  throw( ArgParseException("Missing a value for this argument!",
352  toString() ) );
353  }
354  else
355  _extractValue( value );
356 
357  _alreadySet = true;
358  _checkWithVisitor();
359  return true;
360  }
361  else
362  return false;
363 }
364 
368 template<class T>
369 std::string ValueArg<T>::shortID(const std::string& val) const
370 {
371  static_cast<void>(val); // Ignore input, don't warn
372  return Arg::shortID( _typeDesc );
373 }
374 
378 template<class T>
379 std::string ValueArg<T>::longID(const std::string& val) const
380 {
381  static_cast<void>(val); // Ignore input, don't warn
382  return Arg::longID( _typeDesc );
383 }
384 
385 template<class T>
386 void ValueArg<T>::_extractValue( const std::string& val )
387 {
388  try {
389  ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
390  } catch( ArgParseException &e) {
391  throw ArgParseException(e.error(), toString());
392  }
393 
394  if ( _constraint != NULL )
395  if ( ! _constraint->check( _value ) )
396  throw( CmdLineParseException( "Value '" + val +
397  + "' does not meet constraint: "
398  + _constraint->description(),
399  toString() ) );
400 }
401 
402 template<class T>
404 {
405  Arg::reset();
406  _value = _default;
407 }
408 
409 } // namespace TCLAP
410 
411 #endif