Changeset 1535 for code/trunk/src/util/ArgReader.cc
- Timestamp:
- Jun 4, 2008, 8:54:43 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/util/ArgReader.cc
r1505 r1535 21 21 * 22 22 * Author: 23 * Benjamin Knecht <beni_at_orxonox.net>, (C) 200723 * Reto Grieder 24 24 * Co-authors: 25 * ...25 * Benjamin Knecht <beni_at_orxonox.net> 26 26 * 27 27 */ … … 33 33 34 34 #include "ArgReader.h" 35 #include "SubString.h" 35 36 36 #include <iostream> 37 ArgReader::ArgReader(int argc, char **argv) 38 { 39 failure_ = false; 40 errorString_ = ""; 41 CmdLineArg arg; 37 42 38 ArgReader::ArgReader(int argc, char** argv) 39 { 40 counter_ = argc; 41 arguments_ = argv; 42 fail_ = false; 43 errorStr_ = ""; 44 } 43 int i = 1; 44 while (i < argc) 45 { 46 if (argv[i][0] == '-' && argv[i][1] == '-') // name 47 { 48 if (argv[i][2] == '\0') 49 { 50 failure_ = true; 51 errorString_ += "Expected word after \"--\".\n"; 52 } 53 arg.bChecked_ = false; 54 arg.name_ = argv[i] + 2; 55 arg.value_ = ""; 56 arguments_.push_back(arg); 57 } 58 else // value 59 { 60 if (arguments_.size() == 0) 61 { 62 failure_ = true; 63 errorString_ += "Expected \"--\" in command line arguments.\n"; 64 arg.bChecked_ = false; 65 arg.name_ = ""; 66 arg.value_ = ""; 67 arguments_.push_back(arg); 68 } 45 69 46 void ArgReader::checkArgument(std::string option, std::string &string, bool must)47 { 48 int argpos = checkOption(option) + 1;49 if(argpos != 0)50 {51 string = arguments_[argpos];70 if (arguments_.back().value_ != "") 71 arguments_.back().value_ += " " + std::string(argv[i]); 72 else 73 arguments_.back().value_ = argv[i]; 74 } 75 ++i; 52 76 } 53 else54 {55 if(must) {56 errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n";57 fail_ = true;58 }59 }60 61 }62 63 void ArgReader::checkArgument(std::string option, int &integer, bool must)64 {65 int argpos = checkOption(option) + 1;66 if(argpos != 0)67 {68 integer = atoi(arguments_[argpos]);69 }70 else71 {72 if(must) {73 errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n";74 fail_ = true;75 }76 }77 }78 79 void ArgReader::checkArgument(std::string option, float &floating, bool must)80 {81 int argpos = checkOption(option) + 1;82 if(argpos != 0)83 {84 floating = (float)atof(arguments_[argpos]);85 }86 else87 {88 if(must) {89 errorStr_ = errorStr_ + "Cannot find mandatory argument \"" + option + "\"\n";90 fail_ = true;91 }92 }93 }94 95 int ArgReader::checkOption(std::string option)96 {97 for(int i = 1; i < counter_; i++)98 {99 if(arguments_[i] == "--" + option)100 return i;101 }102 return -1;103 77 } 104 78 105 79 bool ArgReader::errorHandling() 106 80 { 107 if(fail_) 108 std::cout << errorStr_; 109 return fail_; 81 bool argumentsChecked = true; 82 for (unsigned int i = 1; i < arguments_.size(); ++i) 83 argumentsChecked &= arguments_[i].bChecked_; 84 85 if (!argumentsChecked) 86 errorString_ += "Not all arguments could be matched.\n"; 87 88 return !argumentsChecked || failure_; 110 89 } 90 91 const std::string& ArgReader::getErrorString() 92 { 93 return errorString_; 94 }
Note: See TracChangeset
for help on using the changeset viewer.