00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef TCLAP_VALUE_ARGUMENT_H
00024 #define TCLAP_VALUE_ARGUMENT_H
00025
00026 #include <string>
00027 #include <vector>
00028
00029 #include <sbia/tclap/Arg.h>
00030 #include <sbia/tclap/Constraint.h>
00031
00032 namespace TCLAP {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 template<class T>
00043 class ValueArg : public Arg
00044 {
00045 protected:
00046
00047
00048
00049
00050
00051
00052 T _value;
00053
00054
00055
00056
00057
00058 T _default;
00059
00060
00061
00062
00063
00064
00065
00066
00067 std::string _typeDesc;
00068
00069
00070
00071
00072 Constraint<T>* _constraint;
00073
00074
00075
00076
00077
00078
00079
00080 void _extractValue( const std::string& val );
00081
00082 public:
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 ValueArg( const std::string& flag,
00108 const std::string& name,
00109 const std::string& desc,
00110 bool req,
00111 T value,
00112 const std::string& typeDesc,
00113 Visitor* v = NULL);
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 ValueArg( const std::string& flag,
00141 const std::string& name,
00142 const std::string& desc,
00143 bool req,
00144 T value,
00145 const std::string& typeDesc,
00146 CmdLineInterface& parser,
00147 Visitor* v = NULL );
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 ValueArg( const std::string& flag,
00172 const std::string& name,
00173 const std::string& desc,
00174 bool req,
00175 T value,
00176 Constraint<T>* constraint,
00177 CmdLineInterface& parser,
00178 Visitor* v = NULL );
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 ValueArg( const std::string& flag,
00202 const std::string& name,
00203 const std::string& desc,
00204 bool req,
00205 T value,
00206 Constraint<T>* constraint,
00207 Visitor* v = NULL );
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 virtual bool processArg(int* i, std::vector<std::string>& args);
00219
00220
00221
00222
00223 T& getValue() ;
00224
00225
00226
00227
00228
00229 virtual std::string shortID(const std::string& val = "val") const;
00230
00231
00232
00233
00234
00235 virtual std::string longID(const std::string& val = "val") const;
00236
00237 virtual void reset() ;
00238
00239 private:
00240
00241
00242
00243 ValueArg<T>(const ValueArg<T>& rhs);
00244 ValueArg<T>& operator=(const ValueArg<T>& rhs);
00245 };
00246
00247
00248 template<class T>
00249 ValueArg<T>::ValueArg(const std::string& flag,
00250 const std::string& name,
00251 const std::string& desc,
00252 bool req,
00253 T val,
00254 const std::string& typeDesc,
00255 Visitor* v)
00256 : Arg(flag, name, desc, req, true, v),
00257 _value( val ),
00258 _default( val ),
00259 _typeDesc( typeDesc ),
00260 _constraint( NULL )
00261 { }
00262
00263 template<class T>
00264 ValueArg<T>::ValueArg(const std::string& flag,
00265 const std::string& name,
00266 const std::string& desc,
00267 bool req,
00268 T val,
00269 const std::string& typeDesc,
00270 CmdLineInterface& parser,
00271 Visitor* v)
00272 : Arg(flag, name, desc, req, true, v),
00273 _value( val ),
00274 _default( val ),
00275 _typeDesc( typeDesc ),
00276 _constraint( NULL )
00277 {
00278 parser.add( this );
00279 }
00280
00281 template<class T>
00282 ValueArg<T>::ValueArg(const std::string& flag,
00283 const std::string& name,
00284 const std::string& desc,
00285 bool req,
00286 T val,
00287 Constraint<T>* constraint,
00288 Visitor* v)
00289 : Arg(flag, name, desc, req, true, v),
00290 _value( val ),
00291 _default( val ),
00292 _typeDesc( constraint->shortID() ),
00293 _constraint( constraint )
00294 { }
00295
00296 template<class T>
00297 ValueArg<T>::ValueArg(const std::string& flag,
00298 const std::string& name,
00299 const std::string& desc,
00300 bool req,
00301 T val,
00302 Constraint<T>* constraint,
00303 CmdLineInterface& parser,
00304 Visitor* v)
00305 : Arg(flag, name, desc, req, true, v),
00306 _value( val ),
00307 _default( val ),
00308 _typeDesc( constraint->shortID() ),
00309 _constraint( constraint )
00310 {
00311 parser.add( this );
00312 }
00313
00314 template<class T>
00315 T& ValueArg<T>::getValue() { return _value; }
00316
00317 template<class T>
00318 bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
00319 {
00320 if ( _ignoreable && Arg::ignoreRest() )
00321 return false;
00322
00323 if ( _hasBlanks( args[*i] ) )
00324 return false;
00325
00326 std::string flag = args[*i];
00327
00328 std::string value = "";
00329 trimFlag( flag, value );
00330
00331 if ( argMatches( flag ) )
00332 {
00333 if ( _alreadySet )
00334 {
00335 if ( _xorSet )
00336 throw( CmdLineParseException(
00337 "Mutually exclusive argument already set!",
00338 toString()) );
00339 else
00340 throw( CmdLineParseException("Argument already set!",
00341 toString()) );
00342 }
00343
00344 if ( Arg::delimiter() != ' ' && value == "" )
00345 throw( ArgParseException(
00346 "Couldn't find delimiter for this argument!",
00347 toString() ) );
00348
00349 if ( value == "" )
00350 {
00351 (*i)++;
00352 if ( static_cast<unsigned int>(*i) < args.size() )
00353 _extractValue( args[*i] );
00354 else
00355 throw( ArgParseException("Missing a value for this argument!",
00356 toString() ) );
00357 }
00358 else
00359 _extractValue( value );
00360
00361 _alreadySet = true;
00362 _checkWithVisitor();
00363 return true;
00364 }
00365 else
00366 return false;
00367 }
00368
00369 template<class T>
00370 std::string ValueArg<T>::shortID(const std::string& val) const
00371 {
00372 static_cast<void>(val);
00373 return Arg::shortID( _typeDesc );
00374 }
00375
00376 template<class T>
00377 std::string ValueArg<T>::longID(const std::string& val) const
00378 {
00379 static_cast<void>(val);
00380 return Arg::longID( _typeDesc );
00381 }
00382
00383 template<class T>
00384 void ValueArg<T>::_extractValue( const std::string& val )
00385 {
00386 try {
00387 ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
00388 } catch( ArgParseException &e) {
00389 throw ArgParseException(e.error(), toString());
00390 }
00391
00392 if ( _constraint != NULL )
00393 if ( ! _constraint->check( _value ) )
00394 throw( CmdLineParseException( "Value '" + val +
00395 + "' does not meet constraint: "
00396 + _constraint->description(),
00397 toString() ) );
00398 }
00399
00400 template<class T>
00401 void ValueArg<T>::reset()
00402 {
00403 Arg::reset();
00404 _value = _default;
00405 }
00406
00407 }
00408
00409 #endif