tclap: MultiArg.h Source File

Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# MultiArg.h

Go to the documentation of this file.

    00001 /******************************************************************************
    00002  *
    00003  *  file:  MultiArg.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_MULTIPLE_ARGUMENT_H
    00024 #define TCLAP_MULTIPLE_ARGUMENT_H
    00025
    00026 #include <string>
    00027 #include <vector>
    00028
    00029 #include <tclap/Arg.h>
    00030 #include <tclap/Constraint.h>
    00031
    00032 namespace TCLAP {
    00038 template<class T>
    00039 class MultiArg : public Arg
    00040 {
    00041 public:
    00042     typedef std::vector<T> container_type;
    00043     typedef typename container_type::iterator iterator;
    00044     typedef typename container_type::const_iterator const_iterator;
    00045
    00046 protected:
    00047
    00051     std::vector<T> _values;
    00052
    00056     std::string _typeDesc;
    00057
    00061     Constraint<T>* _constraint;
    00062
    00069     void _extractValue( const std::string& val );
    00070
    00074     bool _allowMore;
    00075
    00076 public:
    00077
    00095     MultiArg( const std::string& flag,
    00096                   const std::string& name,
    00097                   const std::string& desc,
    00098                   bool req,
    00099                   const std::string& typeDesc,
    00100                   Visitor* v = NULL);
    00101
    00120     MultiArg( const std::string& flag,
    00121                   const std::string& name,
    00122                   const std::string& desc,
    00123                   bool req,
    00124                   const std::string& typeDesc,
    00125                   CmdLineInterface& parser,
    00126                   Visitor* v = NULL );
    00127
    00143     MultiArg( const std::string& flag,
    00144                   const std::string& name,
    00145                   const std::string& desc,
    00146                   bool req,
    00147                   Constraint<T>* constraint,
    00148                   Visitor* v = NULL );
    00149
    00166     MultiArg( const std::string& flag,
    00167                   const std::string& name,
    00168                   const std::string& desc,
    00169                   bool req,
    00170                   Constraint<T>* constraint,
    00171                   CmdLineInterface& parser,
    00172                   Visitor* v = NULL );
    00173
    00182     virtual bool processArg(int* i, std::vector<std::string>& args);
    00183
    00188     const std::vector<T>& getValue();
    00189
    00194     const_iterator begin() const { return _values.begin(); }
    00195
    00200     const_iterator end() const { return _values.end(); }
    00201
    00206     virtual std::string shortID(const std::string& val="val") const;
    00207
    00212     virtual std::string longID(const std::string& val="val") const;
    00213
    00218     virtual bool isRequired() const;
    00219
    00220     virtual bool allowMore();
    00221
    00222     virtual void reset();
    00223
    00224 };
    00225
    00226 template<class T>
    00227 MultiArg<T>::MultiArg(const std::string& flag,
    00228                       const std::string& name,
    00229                       const std::string& desc,
    00230                       bool req,
    00231                       const std::string& typeDesc,
    00232                       Visitor* v)
    00233 : Arg( flag, name, desc, req, true, v ),
    00234   _typeDesc( typeDesc ),
    00235   _constraint( NULL ),
    00236   _allowMore(false)
    00237 {
    00238     _acceptsMultipleValues = true;
    00239 }
    00240
    00241 template<class T>
    00242 MultiArg<T>::MultiArg(const std::string& flag,
    00243                       const std::string& name,
    00244                       const std::string& desc,
    00245                       bool req,
    00246                       const std::string& typeDesc,
    00247                       CmdLineInterface& parser,
    00248                       Visitor* v)
    00249 : Arg( flag, name, desc, req, true, v ),
    00250   _typeDesc( typeDesc ),
    00251   _constraint( NULL ),
    00252   _allowMore(false)
    00253 {
    00254     parser.add( this );
    00255     _acceptsMultipleValues = true;
    00256 }
    00257
    00261 template<class T>
    00262 MultiArg<T>::MultiArg(const std::string& flag,
    00263                       const std::string& name,
    00264                       const std::string& desc,
    00265                       bool req,
    00266                       Constraint<T>* constraint,
    00267                       Visitor* v)
    00268 : Arg( flag, name, desc, req, true, v ),
    00269   _typeDesc( constraint->shortID() ),
    00270   _constraint( constraint ),
    00271   _allowMore(false)
    00272 {
    00273     _acceptsMultipleValues = true;
    00274 }
    00275
    00276 template<class T>
    00277 MultiArg<T>::MultiArg(const std::string& flag,
    00278                       const std::string& name,
    00279                       const std::string& desc,
    00280                       bool req,
    00281                       Constraint<T>* constraint,
    00282                       CmdLineInterface& parser,
    00283                       Visitor* v)
    00284 : Arg( flag, name, desc, req, true, v ),
    00285   _typeDesc( constraint->shortID() ),
    00286   _constraint( constraint ),
    00287   _allowMore(false)
    00288 {
    00289     parser.add( this );
    00290     _acceptsMultipleValues = true;
    00291 }
    00292
    00293 template<class T>
    00294 const std::vector<T>& MultiArg<T>::getValue() { return _values; }
    00295
    00296 template<class T>
    00297 bool MultiArg<T>::processArg(int *i, std::vector<std::string>& args)
    00298 {
    00299     if ( _ignoreable && Arg::ignoreRest() )
    00300         return false;
    00301
    00302     if ( _hasBlanks( args[*i] ) )
    00303         return false;
    00304
    00305     std::string flag = args[*i];
    00306     std::string value = "";
    00307
    00308     trimFlag( flag, value );
    00309
    00310     if ( argMatches( flag ) )
    00311     {
    00312         if ( Arg::delimiter() != ' ' && value == "" )
    00313             throw( ArgParseException(
    00314                        "Couldn't find delimiter for this argument!",
    00315                        toString() ) );
    00316
    00317         // always take the first one, regardless of start string
    00318         if ( value == "" )
    00319         {
    00320             (*i)++;
    00321             if ( static_cast<unsigned int>(*i) < args.size() )
    00322                 _extractValue( args[*i] );
    00323             else
    00324                 throw( ArgParseException("Missing a value for this argument!",
    00325                                          toString() ) );
    00326         }
    00327         else
    00328             _extractValue( value );
    00329
    00330         /*
    00331         // continuing taking the args until we hit one with a start string
    00332         while ( (unsigned int)(*i)+1 < args.size() &&
    00333                 args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 &&
    00334                 args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 )
    00335                 _extractValue( args[++(*i)] );
    00336         */
    00337
    00338         _alreadySet = true;
    00339         _checkWithVisitor();
    00340
    00341         return true;
    00342     }
    00343     else
    00344         return false;
    00345 }
    00346
    00350 template<class T>
    00351 std::string MultiArg<T>::shortID(const std::string& val) const
    00352 {
    00353     static_cast<void>(val); // Ignore input, don't warn
    00354     return Arg::shortID(_typeDesc) + " ... ";
    00355 }
    00356
    00360 template<class T>
    00361 std::string MultiArg<T>::longID(const std::string& val) const
    00362 {
    00363     static_cast<void>(val); // Ignore input, don't warn
    00364     return Arg::longID(_typeDesc) + "  (accepted multiple times)";
    00365 }
    00366
    00371 template<class T>
    00372 bool MultiArg<T>::isRequired() const
    00373 {
    00374     if ( _required )
    00375     {
    00376         if ( _values.size() > 1 )
    00377             return false;
    00378         else
    00379             return true;
    00380     }
    00381     else
    00382         return false;
    00383
    00384 }
    00385
    00386 template<class T>
    00387 void MultiArg<T>::_extractValue( const std::string& val )
    00388 {
    00389     try {
    00390     T tmp;
    00391     ExtractValue(tmp, val, typename ArgTraits<T>::ValueCategory());
    00392     _values.push_back(tmp);
    00393     } catch( ArgParseException &e) {
    00394     throw ArgParseException(e.error(), toString());
    00395     }
    00396
    00397     if ( _constraint != NULL )
    00398     if ( ! _constraint->check( _values.back() ) )
    00399         throw( CmdLineParseException( "Value '" + val +
    00400                       "' does not meet constraint: " +
    00401                       _constraint->description(),
    00402                       toString() ) );
    00403 }
    00404
    00405 template<class T>
    00406 bool MultiArg<T>::allowMore()
    00407 {
    00408     bool am = _allowMore;
    00409     _allowMore = true;
    00410     return am;
    00411 }
    00412
    00413 template<class T>
    00414 void MultiArg<T>::reset()
    00415 {
    00416     Arg::reset();
    00417     _values.clear();
    00418 }
    00419
    00420 } // namespace TCLAP
    00421
    00422 #endif