Ignore:
Timestamp:
24/08/10 11:23:06 (21 months ago)
Author:
Freddie Akeroyd
Message:

Update to tclap 1.2.0 from http://tclap.sourceforge.net/
Refs #244

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/third_party/tclap/Arg.h

    r857 r1515  
    1  
    2 /******************************************************************************  
    3  *  
     1// -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 
     2 
     3/****************************************************************************** 
     4 * 
    45 *  file:  Arg.h 
    5  *  
     6 * 
    67 *  Copyright (c) 2003, Michael E. Smoot . 
    78 *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno . 
    89 *  All rights reverved. 
    9  *  
     10 * 
    1011 *  See the file COPYING in the top directory of this distribution for 
    1112 *  more information. 
    12  *   
    13  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS  
    14  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
    15  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL  
    16  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
    17  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING  
    18  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER  
    19  *  DEALINGS IN THE SOFTWARE.   
    20  *   
    21  *****************************************************************************/  
     13 * 
     14 *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 
     15 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
     16 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
     17 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
     18 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
     19 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
     20 *  DEALINGS IN THE SOFTWARE. 
     21 * 
     22 *****************************************************************************/ 
    2223 
    2324 
    2425#ifndef TCLAP_ARGUMENT_H 
    2526#define TCLAP_ARGUMENT_H 
     27 
     28#ifdef ___HAVE_CONFIG_H 
     29#include <config.h> 
     30#else 
     31#define HAVE_SSTREAM 
     32#endif 
    2633 
    2734#include <string> 
     
    2936#include <list> 
    3037#include <iostream> 
     38#include <iomanip> 
     39#include <cstdio> 
     40 
     41#if defined(HAVE_SSTREAM) 
     42#include <sstream> 
     43typedef std::istringstream istringstream; 
     44#elif defined(HAVE_STRSTREAM) 
     45#include <strstream> 
     46typedef std::istrstream istringstream; 
     47#else 
     48#error "Need a stringstream (sstream or strstream) to compile!" 
     49#endif 
    3150 
    3251#include <tclap/ArgException.h> 
    3352#include <tclap/Visitor.h> 
    3453#include <tclap/CmdLineInterface.h> 
     54#include <tclap/ArgTraits.h> 
     55#include <tclap/StandardTraits.h> 
    3556 
    3657namespace TCLAP { 
    3758 
    38 /**  
     59/** 
    3960 * A virtual base class that defines the essential data for all arguments. 
    4061 * This class, or one of its existing children, must be subclassed to do 
    41  * anything.  
     62 * anything. 
    4263 */ 
    4364class Arg 
    4465{ 
    45         private:  
     66        private: 
    4667 
    4768                /** 
     
    5475                 * value. 
    5576                 */ 
    56                 static char& delimiterRef() { static char delim = ' '; return delim; }  
     77                static char& delimiterRef() { static char delim = ' '; return delim; } 
    5778 
    5879        protected: 
    5980 
    60                 /**  
     81                /** 
    6182                 * The single char flag used to identify the argument. 
    62                  * This value (preceded by a dash {-}), can be used to identify  
    63                  * an argument on the command line.  The _flag can be blank,  
     83                 * This value (preceded by a dash {-}), can be used to identify 
     84                 * an argument on the command line.  The _flag can be blank, 
    6485                 * in fact this is how unlabeled args work.  Unlabeled args must 
    65                  * override appropriate functions to get correct handling. Note  
     86                 * override appropriate functions to get correct handling. Note 
    6687                 * that the _flag does NOT include the dash as part of the flag. 
    6788                 */ 
     
    7091                /** 
    7192                 * A single work namd indentifying the argument. 
    72                  * This value (preceded by two dashed {--}) can also be used  
     93                 * This value (preceded by two dashed {--}) can also be used 
    7394                 * to identify an argument on the command line.  Note that the 
    7495                 * _name does NOT include the two dashes as part of the _name. The 
     
    7899 
    79100                /** 
    80                  * Description of the argument.  
     101                 * Description of the argument. 
    81102                 */ 
    82103                std::string _description; 
    83104 
    84                 /**  
     105                /** 
    85106                 * Indicating whether the argument is required. 
    86107                 */ 
     
    88109 
    89110                /** 
    90                  * Label to be used in usage description.  Normally set to  
     111                 * Label to be used in usage description.  Normally set to 
    91112                 * "required", but can be changed when necessary. 
    92113                 */ 
     
    126147                bool _xorSet; 
    127148 
     149                bool _acceptsMultipleValues; 
     150 
    128151                /** 
    129152                 * Performs the special handling described by the Vistitor. 
     
    132155 
    133156                /** 
    134                  * Primary constructor. YOU (yes you) should NEVER construct an Arg  
     157                 * Primary constructor. YOU (yes you) should NEVER construct an Arg 
    135158                 * directly, this is a base class that is extended by various children 
    136                  * that are meant to be used.  Use SwitchArg, ValueArg, MultiArg,  
     159                 * that are meant to be used.  Use SwitchArg, ValueArg, MultiArg, 
    137160                 * UnlabeledValueArg, or UnlabeledMultiArg instead. 
    138161                 * 
     
    144167                 * \param v - The visitor checked by the argument. Defaults to NULL. 
    145168                 */ 
    146                 Arg( const std::string& flag,  
    147                          const std::string& name,  
    148                          const std::string& desc,  
    149                          bool req,  
     169                Arg( const std::string& flag, 
     170                         const std::string& name, 
     171                         const std::string& desc, 
     172                         bool req, 
    150173                         bool valreq, 
    151174                         Visitor* v = NULL ); 
     
    162185                 */ 
    163186                virtual void addToList( std::list<Arg*>& argList ) const; 
    164                  
     187 
    165188                /** 
    166189                 * Begin ignoring arguments since the "--" argument was specified. 
    167190                 */ 
    168191                static void beginIgnoring() { ignoreRestRef() = true; } 
    169                  
     192 
    170193                /** 
    171194                 * Whether to ignore the rest. 
     
    177200                 * value. 
    178201                 */ 
    179                 static char delimiter() { return delimiterRef(); }  
    180                  
     202                static char delimiter() { return delimiterRef(); } 
     203 
    181204                /** 
    182205                 * The char used as a place holder when SwitchArgs are combined. 
    183                  * Currently set to '*', which shouldn't cause many problems since 
    184                  * *'s are expanded by most shells on the command line.   
    185                  */ 
    186                 static const char blankChar() { return '*'; } 
    187                  
     206                 * Currently set to the bell char (ASCII 7). 
     207                 */ 
     208                static char blankChar() { return (char)7; } 
     209 
    188210                /** 
    189211                 * The char that indicates the beginning of a flag.  Currently '-'. 
    190212                 */ 
    191                 static const char flagStartChar() { return '-'; } 
    192                  
     213                static char flagStartChar() { return '-'; } 
     214 
    193215                /** 
    194216                 * The sting that indicates the beginning of a flag.  Currently "-". 
     
    196218                 */ 
    197219                static const std::string flagStartString() { return "-"; } 
    198                  
     220 
    199221                /** 
    200222                 * The sting that indicates the beginning of a name.  Currently "--". 
     
    216238                /** 
    217239                 * Pure virtual method meant to handle the parsing and value assignment 
    218                  * of the string on the command line.  
     240                 * of the string on the command line. 
    219241                 * \param i - Pointer the the current argument in the list. 
    220                  * \param args - Mutable list of strings. What is  
     242                 * \param args - Mutable list of strings. What is 
    221243                 * passed in from main. 
    222244                 */ 
    223                 virtual bool processArg(int *i, std::vector<std::string>& args) = 0;  
     245                virtual bool processArg(int *i, std::vector<std::string>& args) = 0; 
    224246 
    225247                /** 
     
    228250                 * \param a - The Arg to be compared to this. 
    229251                 */ 
    230                 virtual bool operator==(const Arg& a); 
     252                virtual bool operator==(const Arg& a) const; 
    231253 
    232254                /** 
     
    274296 
    275297                /** 
    276                  * Indicates whether the argument can be ignored, if desired.  
     298                 * Indicates whether the argument can be ignored, if desired. 
    277299                 */ 
    278300                bool isIgnoreable() const; 
     
    281303                 * A method that tests whether a string matches this argument. 
    282304                 * This is generally called by the processArg() method.  This 
    283                  * method could be re-implemented by a child to change how  
     305                 * method could be re-implemented by a child to change how 
    284306                 * arguments are specified on the command line. 
    285307                 * \param s - The string to be compared to the flag/name to determine 
     
    308330                /** 
    309331                 * Trims a value off of the flag. 
    310                  * \param flag - The string from which the flag and value will be  
    311                  * trimmed. Contains the flag once the value has been trimmed.  
     332                 * \param flag - The string from which the flag and value will be 
     333                 * trimmed. Contains the flag once the value has been trimmed. 
    312334                 * \param value - Where the value trimmed from the string will 
    313335                 * be stored. 
     
    330352                void setRequireLabel( const std::string& s ); 
    331353 
     354                /** 
     355                 * Used for MultiArgs and XorHandler to determine whether args 
     356                 * can still be set. 
     357                 */ 
     358                virtual bool allowMore(); 
     359 
     360                /** 
     361                 * Use by output classes to determine whether an Arg accepts 
     362                 * multiple values. 
     363                 */ 
     364                virtual bool acceptsMultipleValues(); 
     365 
     366                /** 
     367                 * Clears the Arg object and allows it to be reused by new 
     368                 * command lines. 
     369                 */ 
     370                 virtual void reset(); 
    332371}; 
    333372 
     
    347386typedef std::list<Visitor*>::iterator VisitorListIterator; 
    348387 
     388/* 
     389 * Extract a value of type T from it's string representation contained 
     390 * in strVal. The ValueLike parameter used to select the correct 
     391 * specialization of ExtractValue depending on the value traits of T. 
     392 * ValueLike traits use operator>> to assign the value from strVal. 
     393 */ 
     394template<typename T> void 
     395ExtractValue(T &destVal, const std::string& strVal, ValueLike vl) 
     396{ 
     397    static_cast<void>(vl); // Avoid warning about unused vl 
     398    std::istringstream is(strVal); 
     399 
     400    int valuesRead = 0; 
     401    while ( is.good() ) { 
     402        if ( is.peek() != EOF ) 
     403#ifdef TCLAP_SETBASE_ZERO 
     404            is >> std::setbase(0) >> destVal; 
     405#else 
     406            is >> destVal; 
     407#endif 
     408        else 
     409            break; 
     410 
     411        valuesRead++; 
     412    } 
     413 
     414    if ( is.fail() ) 
     415        throw( ArgParseException("Couldn't read argument value " 
     416                                 "from string '" + strVal + "'")); 
     417 
     418 
     419    if ( valuesRead > 1 ) 
     420        throw( ArgParseException("More than one valid value parsed from " 
     421                                 "string '" + strVal + "'")); 
     422 
     423} 
     424 
     425/* 
     426 * Extract a value of type T from it's string representation contained 
     427 * in strVal. The ValueLike parameter used to select the correct 
     428 * specialization of ExtractValue depending on the value traits of T. 
     429 * StringLike uses assignment (operator=) to assign from strVal. 
     430 */ 
     431template<typename T> void 
     432ExtractValue(T &destVal, const std::string& strVal, StringLike sl) 
     433{ 
     434    static_cast<void>(sl); // Avoid warning about unused sl 
     435    SetString(destVal, strVal); 
     436} 
    349437 
    350438////////////////////////////////////////////////////////////////////// 
     
    352440////////////////////////////////////////////////////////////////////// 
    353441 
    354 inline Arg::Arg(const std::string& flag,  
    355          const std::string& name,  
    356          const std::string& desc,  
    357          bool req,  
     442inline Arg::Arg(const std::string& flag, 
     443         const std::string& name, 
     444         const std::string& desc, 
     445         bool req, 
    358446         bool valreq, 
    359447         Visitor* v) : 
     
    367455  _visitor( v ), 
    368456  _ignoreable(true), 
    369   _xorSet(false) 
    370 { 
    371         if ( _flag.length() > 1 )  
     457  _xorSet(false), 
     458  _acceptsMultipleValues(false) 
     459{ 
     460        if ( _flag.length() > 1 ) 
    372461                throw(SpecificationException( 
    373462                                "Argument flag can only be one character long", toString() ) ); 
    374463 
    375         if ( _name != ignoreNameString() &&   
    376                  ( _flag == Arg::flagStartString() ||  
    377                    _flag == Arg::nameStartString() ||  
     464        if ( _name != ignoreNameString() && 
     465                 ( _flag == Arg::flagStartString() || 
     466                   _flag == Arg::nameStartString() || 
    378467                   _flag == " " ) ) 
    379                 throw(SpecificationException("Argument flag cannot be either '" +  
    380                                                         Arg::flagStartString() + "' or '" +  
     468                throw(SpecificationException("Argument flag cannot be either '" + 
     469                                                        Arg::flagStartString() + "' or '" + 
    381470                                                        Arg::nameStartString() + "' or a space.", 
    382471                                                        toString() ) ); 
    383472 
    384         if ( ( _name.find( Arg::flagStartString(), 0 ) != std::string::npos ) ||  
    385                  ( _name.find( Arg::nameStartString(), 0 ) != std::string::npos ) || 
     473        if ( ( _name.substr( 0, Arg::flagStartString().length() ) == Arg::flagStartString() ) || 
     474                 ( _name.substr( 0, Arg::nameStartString().length() ) == Arg::nameStartString() ) || 
    386475                 ( _name.find( " ", 0 ) != std::string::npos ) ) 
    387                 throw(SpecificationException("Argument name cannot contain either '" +  
    388                                                         Arg::flagStartString() + "' or '" +  
     476                throw(SpecificationException("Argument name begin with either '" + 
     477                                                        Arg::flagStartString() + "' or '" + 
    389478                                                        Arg::nameStartString() + "' or space.", 
    390479                                                        toString() ) ); 
     
    403492                id = Arg::nameStartString() + _name; 
    404493 
    405         std::string delim = " ";  
    406         delim[0] = Arg::delimiter(); // ugly!!! 
    407          
    408494        if ( _valueRequired ) 
    409                 id += delim + "<" + valueId  + ">"; 
     495                id += std::string( 1, Arg::delimiter() ) + "<" + valueId  + ">"; 
    410496 
    411497        if ( !_required ) 
     
    424510 
    425511                if ( _valueRequired ) 
    426                         id += " <" + valueId + ">"; 
    427                  
     512                        id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">"; 
     513 
    428514                id += ",  "; 
    429515        } 
     
    432518 
    433519        if ( _valueRequired ) 
    434                 id += " <" + valueId + ">"; 
    435                          
     520                id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">"; 
     521 
    436522        return id; 
    437523 
    438524} 
    439525 
    440 inline bool Arg::operator==(const Arg& a) 
    441 { 
    442         if ( ( _flag != "" && _flag == a._flag ) ||  
    443                  _name == a._name ||  
    444                  _description == a._description ) 
     526inline bool Arg::operator==(const Arg& a) const 
     527{ 
     528        if ( ( _flag != "" && _flag == a._flag ) || _name == a._name) 
    445529                return true; 
    446530        else 
     
    448532} 
    449533 
    450 inline std::string Arg::getDescription() const  
     534inline std::string Arg::getDescription() const 
    451535{ 
    452536        std::string desc = ""; 
     
    454538                desc = "(" + _requireLabel + ")  "; 
    455539 
    456         if ( _valueRequired ) 
    457                 desc += "(value required)  "; 
     540//      if ( _valueRequired ) 
     541//              desc += "(value required)  "; 
    458542 
    459543        desc += _description; 
    460         return desc;  
     544        return desc; 
    461545} 
    462546 
    463547inline const std::string& Arg::getFlag() const { return _flag; } 
    464548 
    465 inline const std::string& Arg::getName() const { return _name; }  
     549inline const std::string& Arg::getName() const { return _name; } 
    466550 
    467551inline bool Arg::isRequired() const { return _required; } 
     
    469553inline bool Arg::isValueRequired() const { return _valueRequired; } 
    470554 
    471 inline bool Arg::isSet() const  
    472 {  
     555inline bool Arg::isSet() const 
     556{ 
    473557        if ( _alreadySet && !_xorSet ) 
    474558                return true; 
     
    479563inline bool Arg::isIgnoreable() const { return _ignoreable; } 
    480564 
    481 inline void Arg::setRequireLabel( const std::string& s)  
    482 {  
     565inline void Arg::setRequireLabel( const std::string& s) 
     566{ 
    483567        _requireLabel = s; 
    484568} 
     
    517601{ 
    518602        int stop = 0; 
    519         for ( int i = 0; (unsigned int)i < flag.length(); i++ ) 
     603        for ( int i = 0; static_cast<unsigned int>(i) < flag.length(); i++ ) 
    520604                if ( flag[i] == Arg::delimiter() ) 
    521605                { 
     
    537621inline bool Arg::_hasBlanks( const std::string& s ) const 
    538622{ 
    539         for ( int i = 1; (unsigned int)i < s.length(); i++ ) 
     623        for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ ) 
    540624                if ( s[i] == Arg::blankChar() ) 
    541625                        return true; 
     
    560644inline void Arg::addToList( std::list<Arg*>& argList ) const 
    561645{ 
    562         argList.push_front( (Arg*)this ); 
     646        argList.push_front( const_cast<Arg*>(this) ); 
     647} 
     648 
     649inline bool Arg::allowMore() 
     650{ 
     651        return false; 
     652} 
     653 
     654inline bool Arg::acceptsMultipleValues() 
     655{ 
     656        return _acceptsMultipleValues; 
     657} 
     658 
     659inline void Arg::reset() 
     660{ 
     661        _xorSet = false; 
     662        _alreadySet = false; 
    563663} 
    564664 
Note: See TracChangeset for help on using the changeset viewer.