Changeset 1535 for code/trunk/src/util/ArgReader.h
- Timestamp:
- Jun 4, 2008, 8:54:43 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/util/ArgReader.h
r1505 r1535 21 21 * 22 22 * Author: 23 * Reto Grieder 24 * Co-authors: 23 25 * Benjamin Knecht <beni_at_orxonox.net> 24 * Co-authors:25 * ...26 26 * 27 27 */ … … 39 39 40 40 #include <string> 41 #include <vector> 42 #include "Convert.h" 43 44 struct _UtilExport CmdLineArg 45 { 46 std::string name_; 47 std::string value_; 48 bool bChecked_; 49 }; 41 50 42 51 class _UtilExport ArgReader … … 44 53 public: 45 54 ArgReader(int argc, char **argv); 46 void checkArgument(std::string option, std::string& string, bool must=false); 47 void checkArgument(std::string option, int& integer, bool must=false); 48 void checkArgument(std::string option, float& floating, bool must=false); 55 template <class T> 56 void checkArgument(std::string option, T* value, bool must = false); 49 57 bool errorHandling(); 50 private: 51 int checkOption(std::string option); 58 const std::string& getErrorString(); 52 59 53 60 private: 54 int counter_; 55 char **arguments_; 56 bool fail_; 57 std::string errorStr_; 61 std::vector<CmdLineArg> arguments_; 62 bool failure_; 63 std::string errorString_; 58 64 }; 59 65 66 template <class T> 67 void ArgReader::checkArgument(std::string option, T* value, bool must) 68 { 69 unsigned int iArg = 0; 70 while (iArg < arguments_.size()) 71 { 72 if (arguments_[iArg].name_ == option) 73 break; 74 ++iArg; 75 } 76 if (iArg == arguments_.size()) 77 { 78 if (must) 79 { 80 failure_ = true; 81 errorString_ += "Cannot find mandatory argument \"" + option + "\"\n"; 82 return; 83 } 84 else 85 return; 86 } 87 88 arguments_[iArg].bChecked_ = true; 89 90 if (!convertValue(value, arguments_[iArg].value_)) 91 { 92 failure_ = true; 93 errorString_ += "Cannot convert argument value for option \"" + option + "\"\n"; 94 } 95 } 96 97 template <> 98 void ArgReader::checkArgument(std::string option, bool* value, bool must) 99 { 100 // for type bool, only check whether the option was set or not 101 unsigned int iArg = 0; 102 while (iArg < arguments_.size()) 103 { 104 if (arguments_[iArg].name_ == option) 105 { 106 arguments_[iArg].bChecked_ = true; 107 *value = true; 108 break; 109 } 110 ++iArg; 111 } 112 if (iArg == arguments_.size()) 113 *value = false; 114 } 115 60 116 #endif /* _ArgReader_H__ */
Note: See TracChangeset
for help on using the changeset viewer.