Changeset 1515 for trunk/third_party/tclap/Arg.h
- Timestamp:
- 24/08/10 11:23:06 (21 months ago)
- File:
-
- 1 edited
-
trunk/third_party/tclap/Arg.h (modified) (31 diffs)
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 * 4 5 * file: Arg.h 5 * 6 * 6 7 * Copyright (c) 2003, Michael E. Smoot . 7 8 * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno . 8 9 * All rights reverved. 9 * 10 * 10 11 * See the file COPYING in the top directory of this distribution for 11 12 * 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 *****************************************************************************/ 22 23 23 24 24 25 #ifndef TCLAP_ARGUMENT_H 25 26 #define TCLAP_ARGUMENT_H 27 28 #ifdef ___HAVE_CONFIG_H 29 #include <config.h> 30 #else 31 #define HAVE_SSTREAM 32 #endif 26 33 27 34 #include <string> … … 29 36 #include <list> 30 37 #include <iostream> 38 #include <iomanip> 39 #include <cstdio> 40 41 #if defined(HAVE_SSTREAM) 42 #include <sstream> 43 typedef std::istringstream istringstream; 44 #elif defined(HAVE_STRSTREAM) 45 #include <strstream> 46 typedef std::istrstream istringstream; 47 #else 48 #error "Need a stringstream (sstream or strstream) to compile!" 49 #endif 31 50 32 51 #include <tclap/ArgException.h> 33 52 #include <tclap/Visitor.h> 34 53 #include <tclap/CmdLineInterface.h> 54 #include <tclap/ArgTraits.h> 55 #include <tclap/StandardTraits.h> 35 56 36 57 namespace TCLAP { 37 58 38 /** 59 /** 39 60 * A virtual base class that defines the essential data for all arguments. 40 61 * This class, or one of its existing children, must be subclassed to do 41 * anything. 62 * anything. 42 63 */ 43 64 class Arg 44 65 { 45 private: 66 private: 46 67 47 68 /** … … 54 75 * value. 55 76 */ 56 static char& delimiterRef() { static char delim = ' '; return delim; } 77 static char& delimiterRef() { static char delim = ' '; return delim; } 57 78 58 79 protected: 59 80 60 /** 81 /** 61 82 * 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, 64 85 * 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 66 87 * that the _flag does NOT include the dash as part of the flag. 67 88 */ … … 70 91 /** 71 92 * 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 73 94 * to identify an argument on the command line. Note that the 74 95 * _name does NOT include the two dashes as part of the _name. The … … 78 99 79 100 /** 80 * Description of the argument. 101 * Description of the argument. 81 102 */ 82 103 std::string _description; 83 104 84 /** 105 /** 85 106 * Indicating whether the argument is required. 86 107 */ … … 88 109 89 110 /** 90 * Label to be used in usage description. Normally set to 111 * Label to be used in usage description. Normally set to 91 112 * "required", but can be changed when necessary. 92 113 */ … … 126 147 bool _xorSet; 127 148 149 bool _acceptsMultipleValues; 150 128 151 /** 129 152 * Performs the special handling described by the Vistitor. … … 132 155 133 156 /** 134 * Primary constructor. YOU (yes you) should NEVER construct an Arg 157 * Primary constructor. YOU (yes you) should NEVER construct an Arg 135 158 * 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, 137 160 * UnlabeledValueArg, or UnlabeledMultiArg instead. 138 161 * … … 144 167 * \param v - The visitor checked by the argument. Defaults to NULL. 145 168 */ 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, 150 173 bool valreq, 151 174 Visitor* v = NULL ); … … 162 185 */ 163 186 virtual void addToList( std::list<Arg*>& argList ) const; 164 187 165 188 /** 166 189 * Begin ignoring arguments since the "--" argument was specified. 167 190 */ 168 191 static void beginIgnoring() { ignoreRestRef() = true; } 169 192 170 193 /** 171 194 * Whether to ignore the rest. … … 177 200 * value. 178 201 */ 179 static char delimiter() { return delimiterRef(); } 180 202 static char delimiter() { return delimiterRef(); } 203 181 204 /** 182 205 * 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 188 210 /** 189 211 * The char that indicates the beginning of a flag. Currently '-'. 190 212 */ 191 static c onst char flagStartChar() { return '-'; }192 213 static char flagStartChar() { return '-'; } 214 193 215 /** 194 216 * The sting that indicates the beginning of a flag. Currently "-". … … 196 218 */ 197 219 static const std::string flagStartString() { return "-"; } 198 220 199 221 /** 200 222 * The sting that indicates the beginning of a name. Currently "--". … … 216 238 /** 217 239 * 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. 219 241 * \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 221 243 * passed in from main. 222 244 */ 223 virtual bool processArg(int *i, std::vector<std::string>& args) = 0; 245 virtual bool processArg(int *i, std::vector<std::string>& args) = 0; 224 246 225 247 /** … … 228 250 * \param a - The Arg to be compared to this. 229 251 */ 230 virtual bool operator==(const Arg& a) ;252 virtual bool operator==(const Arg& a) const; 231 253 232 254 /** … … 274 296 275 297 /** 276 * Indicates whether the argument can be ignored, if desired. 298 * Indicates whether the argument can be ignored, if desired. 277 299 */ 278 300 bool isIgnoreable() const; … … 281 303 * A method that tests whether a string matches this argument. 282 304 * 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 284 306 * arguments are specified on the command line. 285 307 * \param s - The string to be compared to the flag/name to determine … … 308 330 /** 309 331 * 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. 312 334 * \param value - Where the value trimmed from the string will 313 335 * be stored. … … 330 352 void setRequireLabel( const std::string& s ); 331 353 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(); 332 371 }; 333 372 … … 347 386 typedef std::list<Visitor*>::iterator VisitorListIterator; 348 387 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 */ 394 template<typename T> void 395 ExtractValue(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 */ 431 template<typename T> void 432 ExtractValue(T &destVal, const std::string& strVal, StringLike sl) 433 { 434 static_cast<void>(sl); // Avoid warning about unused sl 435 SetString(destVal, strVal); 436 } 349 437 350 438 ////////////////////////////////////////////////////////////////////// … … 352 440 ////////////////////////////////////////////////////////////////////// 353 441 354 inline Arg::Arg(const std::string& flag, 355 const std::string& name, 356 const std::string& desc, 357 bool req, 442 inline Arg::Arg(const std::string& flag, 443 const std::string& name, 444 const std::string& desc, 445 bool req, 358 446 bool valreq, 359 447 Visitor* v) : … … 367 455 _visitor( v ), 368 456 _ignoreable(true), 369 _xorSet(false) 370 { 371 if ( _flag.length() > 1 ) 457 _xorSet(false), 458 _acceptsMultipleValues(false) 459 { 460 if ( _flag.length() > 1 ) 372 461 throw(SpecificationException( 373 462 "Argument flag can only be one character long", toString() ) ); 374 463 375 if ( _name != ignoreNameString() && 376 ( _flag == Arg::flagStartString() || 377 _flag == Arg::nameStartString() || 464 if ( _name != ignoreNameString() && 465 ( _flag == Arg::flagStartString() || 466 _flag == Arg::nameStartString() || 378 467 _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 '" + 381 470 Arg::nameStartString() + "' or a space.", 382 471 toString() ) ); 383 472 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() ) || 386 475 ( _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 '" + 389 478 Arg::nameStartString() + "' or space.", 390 479 toString() ) ); … … 403 492 id = Arg::nameStartString() + _name; 404 493 405 std::string delim = " ";406 delim[0] = Arg::delimiter(); // ugly!!!407 408 494 if ( _valueRequired ) 409 id += delim+ "<" + valueId + ">";495 id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">"; 410 496 411 497 if ( !_required ) … … 424 510 425 511 if ( _valueRequired ) 426 id += "<" + valueId + ">";427 512 id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">"; 513 428 514 id += ", "; 429 515 } … … 432 518 433 519 if ( _valueRequired ) 434 id += "<" + valueId + ">";435 520 id += std::string( 1, Arg::delimiter() ) + "<" + valueId + ">"; 521 436 522 return id; 437 523 438 524 } 439 525 440 inline bool Arg::operator==(const Arg& a) 441 { 442 if ( ( _flag != "" && _flag == a._flag ) || 443 _name == a._name || 444 _description == a._description ) 526 inline bool Arg::operator==(const Arg& a) const 527 { 528 if ( ( _flag != "" && _flag == a._flag ) || _name == a._name) 445 529 return true; 446 530 else … … 448 532 } 449 533 450 inline std::string Arg::getDescription() const 534 inline std::string Arg::getDescription() const 451 535 { 452 536 std::string desc = ""; … … 454 538 desc = "(" + _requireLabel + ") "; 455 539 456 if ( _valueRequired )457 desc += "(value required) ";540 // if ( _valueRequired ) 541 // desc += "(value required) "; 458 542 459 543 desc += _description; 460 return desc; 544 return desc; 461 545 } 462 546 463 547 inline const std::string& Arg::getFlag() const { return _flag; } 464 548 465 inline const std::string& Arg::getName() const { return _name; } 549 inline const std::string& Arg::getName() const { return _name; } 466 550 467 551 inline bool Arg::isRequired() const { return _required; } … … 469 553 inline bool Arg::isValueRequired() const { return _valueRequired; } 470 554 471 inline bool Arg::isSet() const 472 { 555 inline bool Arg::isSet() const 556 { 473 557 if ( _alreadySet && !_xorSet ) 474 558 return true; … … 479 563 inline bool Arg::isIgnoreable() const { return _ignoreable; } 480 564 481 inline void Arg::setRequireLabel( const std::string& s) 482 { 565 inline void Arg::setRequireLabel( const std::string& s) 566 { 483 567 _requireLabel = s; 484 568 } … … 517 601 { 518 602 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++ ) 520 604 if ( flag[i] == Arg::delimiter() ) 521 605 { … … 537 621 inline bool Arg::_hasBlanks( const std::string& s ) const 538 622 { 539 for ( int i = 1; (unsigned int)i< s.length(); i++ )623 for ( int i = 1; static_cast<unsigned int>(i) < s.length(); i++ ) 540 624 if ( s[i] == Arg::blankChar() ) 541 625 return true; … … 560 644 inline void Arg::addToList( std::list<Arg*>& argList ) const 561 645 { 562 argList.push_front( (Arg*)this ); 646 argList.push_front( const_cast<Arg*>(this) ); 647 } 648 649 inline bool Arg::allowMore() 650 { 651 return false; 652 } 653 654 inline bool Arg::acceptsMultipleValues() 655 { 656 return _acceptsMultipleValues; 657 } 658 659 inline void Arg::reset() 660 { 661 _xorSet = false; 662 _alreadySet = false; 563 663 } 564 664
Note: See TracChangeset
for help on using the changeset viewer.
