tclap: ValueArg.h Source File

Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | 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 };
    00240
    00241
    00245 template<class T>
    00246 ValueArg<T>::ValueArg(const std::string& flag,
    00247                       const std::string& name,
    00248                       const std::string& desc,
    00249                       bool req,
    00250                       T val,
    00251                       const std::string& typeDesc,
    00252                       Visitor* v)
    00253 : Arg(flag, name, desc, req, true, v),
    00254   _value( val ),
    00255   _default( val ),
    00256   _typeDesc( typeDesc ),
    00257   _constraint( NULL )
    00258 { }
    00259
    00260 template<class T>
    00261 ValueArg<T>::ValueArg(const std::string& flag,
    00262                       const std::string& name,
    00263                       const std::string& desc,
    00264                       bool req,
    00265                       T val,
    00266                       const std::string& typeDesc,
    00267                       CmdLineInterface& parser,
    00268                       Visitor* v)
    00269 : Arg(flag, name, desc, req, true, v),
    00270   _value( val ),
    00271   _default( val ),
    00272   _typeDesc( typeDesc ),
    00273   _constraint( NULL )
    00274 {
    00275     parser.add( this );
    00276 }
    00277
    00278 template<class T>
    00279 ValueArg<T>::ValueArg(const std::string& flag,
    00280                       const std::string& name,
    00281                       const std::string& desc,
    00282                       bool req,
    00283                       T val,
    00284                       Constraint<T>* constraint,
    00285                       Visitor* v)
    00286 : Arg(flag, name, desc, req, true, v),
    00287   _value( val ),
    00288   _default( val ),
    00289   _typeDesc( constraint->shortID() ),
    00290   _constraint( constraint )
    00291 { }
    00292
    00293 template<class T>
    00294 ValueArg<T>::ValueArg(const std::string& flag,
    00295                       const std::string& name,
    00296                       const std::string& desc,
    00297                       bool req,
    00298                       T val,
    00299                       Constraint<T>* constraint,
    00300                       CmdLineInterface& parser,
    00301                       Visitor* v)
    00302 : Arg(flag, name, desc, req, true, v),
    00303   _value( val ),
    00304   _default( val ),
    00305   _typeDesc( constraint->shortID() ),
    00306   _constraint( constraint )
    00307 {
    00308     parser.add( this );
    00309 }
    00310
    00311
    00315 template<class T>
    00316 T& ValueArg<T>::getValue() { return _value; }
    00317
    00321 template<class T>
    00322 bool ValueArg<T>::processArg(int *i, std::vector<std::string>& args)
    00323 {
    00324     if ( _ignoreable && Arg::ignoreRest() )
    00325         return false;
    00326
    00327     if ( _hasBlanks( args[*i] ) )
    00328         return false;
    00329
    00330     std::string flag = args[*i];
    00331
    00332     std::string value = "";
    00333     trimFlag( flag, value );
    00334
    00335     if ( argMatches( flag ) )
    00336     {
    00337         if ( _alreadySet )
    00338             throw( CmdLineParseException("Argument already set!", toString()) );
    00339
    00340         if ( Arg::delimiter() != ' ' && value == "" )
    00341             throw( ArgParseException(
    00342                             "Couldn't find delimiter for this argument!",
    00343                              toString() ) );
    00344
    00345         if ( value == "" )
    00346         {
    00347             (*i)++;
    00348             if ( static_cast<unsigned int>(*i) < args.size() )
    00349                 _extractValue( args[*i] );
    00350             else
    00351                 throw( ArgParseException("Missing a value for this argument!",
    00352                                                     toString() ) );
    00353         }
    00354         else
    00355             _extractValue( value );
    00356
    00357         _alreadySet = true;
    00358         _checkWithVisitor();
    00359         return true;
    00360     }
    00361     else
    00362         return false;
    00363 }
    00364
    00368 template<class T>
    00369 std::string ValueArg<T>::shortID(const std::string& val) const
    00370 {
    00371     static_cast<void>(val); // Ignore input, don't warn
    00372     return Arg::shortID( _typeDesc );
    00373 }
    00374
    00378 template<class T>
    00379 std::string ValueArg<T>::longID(const std::string& val) const
    00380 {
    00381     static_cast<void>(val); // Ignore input, don't warn
    00382     return Arg::longID( _typeDesc );
    00383 }
    00384
    00385 template<class T>
    00386 void ValueArg<T>::_extractValue( const std::string& val )
    00387 {
    00388     try {
    00389     ExtractValue(_value, val, typename ArgTraits<T>::ValueCategory());
    00390     } catch( ArgParseException &e) {
    00391     throw ArgParseException(e.error(), toString());
    00392     }
    00393
    00394     if ( _constraint != NULL )
    00395     if ( ! _constraint->check( _value ) )
    00396         throw( CmdLineParseException( "Value '" + val +
    00397                       + "' does not meet constraint: "
    00398                       + _constraint->description(),
    00399                       toString() ) );
    00400 }
    00401
    00402 template<class T>
    00403 void ValueArg<T>::reset()
    00404 {
    00405     Arg::reset();
    00406     _value = _default;
    00407 }
    00408
    00409 } // namespace TCLAP
    00410
    00411 #endif