tclap: ValueArg.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# ValueArg.h

Go to the documentation of this file.

    00001 /******************************************************************************
    00002  *
    00003  *  file:  ValueArg.h
    00004  *
    00005  *  Copyright (c) 2003, Michael E. Smoot .
    00006  *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
    00007  *  All rights reverved.
    00008  *
    00009  *  See the file COPYING in the top directory of this distribution for
    00010  *  more information.
    00011  *
    00012  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
    00013  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00014  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    00015  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00016  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    00017  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    00018  *  DEALINGS IN THE SOFTWARE.
    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 <tclap/Arg.h>
    00030 #include <tclap/Constraint.h>
    00031
    00032 namespace TCLAP {
    00033
    00042 template<class T>
    00043 class ValueArg : public Arg
    00044 {
    00045     protected:
    00046
    00052         T _value;
    00053
    00058         T _default;
    00059
    00067         std::string _typeDesc;
    00068
    00072         Constraint<T>* _constraint;
    00073
    00080         void _extractValue( const std::string& val );
    00081
    00082     public:
    00083
    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
    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
    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
    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
    00218         virtual bool processArg(int* i, std::vector<std::string>& args);
    00219
    00223         T& getValue() ;
    00224
    00229         virtual std::string shortID(const std::string& val = "val") const;
    00230
    00235         virtual std::string longID(const std::string& val = "val") const;
    00236
    00237         virtual void reset() ;
    00238
    00239 private:
    00243        ValueArg<T>(const ValueArg<T>& rhs);
    00244        ValueArg<T>& operator=(const ValueArg<T>& rhs);
    00245 };
    00246
    00247
    00251 template<class T>
    00252 ValueArg<T>::ValueArg(const std::string& flag,
    00253                       const std::string& name,
    00254                       const std::string& desc,
    00255                       bool req,
    00256                       T val,
    00257                       const std::string& typeDesc,
    00258                       Visitor* v)
    00259 : Arg(flag, name, desc, req, true, v),
    00260   _value( val ),
    00261   _default( val ),
    00262   _typeDesc( typeDesc ),
    00263   _constraint( NULL )
    00264 { }
    00265
    00266 template<class T>
    00267 ValueArg<T>::ValueArg(const std::string& flag,
    00268                       const std::string& name,
    00269                       const std::string& desc,
    00270                       bool req,
    00271                       T val,
    00272                       const std::string& typeDesc,
    00273                       CmdLineInterface& parser,
    00274                       Visitor* v)
    00275 : Arg(flag, name, desc, req, true, v),
    00276   _value( val ),
    00277   _default( val ),
    00278   _typeDesc( typeDesc ),
    00279   _constraint( NULL )
    00280 {
    00281     parser.add( this );
    00282 }
    00283
    00284 template<class T>
    00285 ValueArg<T>::ValueArg(const std::string& flag,
    00286                       const std::string& name,
    00287                       const std::string& desc,
    00288                       bool req,
    00289                       T val,
    00290                       Constraint<T>* constraint,
    00291                       Visitor* v)
    00292 : Arg(flag, name, desc, req, true, v),
    00293   _value( val ),
    00294   _default( val ),
    00295   _typeDesc( constraint->shortID() ),
    00296   _constraint( constraint )
    00297 { }
    00298
    00299 template<class T>
    00300 ValueArg<T>::ValueArg(const std::string& flag,
    00301                       const std::string& name,
    00302                       const std::string& desc,
    00303                       bool req,
    00304                       T val,
    00305                       Constraint<T>* constraint,
    00306                       CmdLineInterface& parser,
    00307                       Visitor* v)
    00308 : Arg(flag, name, desc, req, true, v),
    00309   _value( val ),
    00310   _default( val ),
    00311   _typeDesc( constraint->shortID() ),
    00312   _constraint( constraint )
    00313 {
    00314     parser.add( this );
    00315 }
    00316
    00317
    00321 template<class T>
    00322 T& ValueArg<T>::getValue() { return _value; }
    00323
    00327 template<class T>
    00328 bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
    00329 {
    00330     if ( _ignoreable && Arg::ignoreRest() )
    00331         return false;
    00332
    00333     if ( _hasBlanks( args[*i] ) )
    00334         return false;
    00335
    00336     std::string flag = args[*i];
    00337
    00338     std::string value = "";
    00339     trimFlag( flag, value );
    00340
    00341     if ( argMatches( flag ) )
    00342     {
    00343         if ( _alreadySet )
    00344         {
    00345             if ( _xorSet )
    00346                 throw( CmdLineParseException(
    00347                        "Mutually exclusive argument already set!",
    00348                                              toString()) );
    00349             else
    00350                 throw( CmdLineParseException("Argument already set!",
    00351                                              toString()) );
    00352         }
    00353
    00354         if ( Arg::delimiter() != ' ' && value == "" )
    00355             throw( ArgParseException(
    00356                             "Couldn't find delimiter for this argument!",
    00357                              toString() ) );
    00358
    00359         if ( value == "" )
    00360         {
    00361             (*i)++;
    00362             if ( static_cast<unsigned int>(*i) < args.size() )
    00363                 _extractValue( args[*i] );
    00364             else
    00365                 throw( ArgParseException("Missing a value for this argument!",
    00366                                                     toString() ) );
    00367         }
    00368         else
    00369             _extractValue( value );
    00370
    00371         _alreadySet = true;
    00372         _checkWithVisitor();
    00373         return true;
    00374     }
    00375     else
    00376         return false;
    00377 }
    00378
    00382 template<class T>
    00383 std::string ValueArg<T>::shortID(const std::string& val) const
    00384 {
    00385     static_cast<void>(val); // Ignore input, don't warn
    00386     return Arg::shortID( _typeDesc );
    00387 }
    00388
    00392 template<class T>
    00393 std::string ValueArg<T>::longID(const std::string& val) const
    00394 {
    00395     static_cast<void>(val); // Ignore input, don't warn
    00396     return Arg::longID( _typeDesc );
    00397 }
    00398
    00399 template<class T>
    00400 void ValueArg<T>::_extractValue( const std::string& val )
    00401 {
    00402     try {
    00403     ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
    00404     } catch( ArgParseException &e) {
    00405     throw ArgParseException(e.error(), toString());
    00406     }
    00407
    00408     if ( _constraint != NULL )
    00409     if ( ! _constraint->check( _value ) )
    00410         throw( CmdLineParseException( "Value '" + val +
    00411                       + "' does not meet constraint: "
    00412                       + _constraint->description(),
    00413                       toString() ) );
    00414 }
    00415
    00416 template<class T>
    00417 void ValueArg<T>::reset()
    00418 {
    00419     Arg::reset();
    00420     _value = _default;
    00421 }
    00422
    00423 } // namespace TCLAP
    00424
    00425 #endif