Changeset 1664 for code/branches/gui/src/core/CommandLine.cc
- Timestamp:
- Aug 21, 2008, 9:23:11 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/gui/src/core/CommandLine.cc
r1663 r1664 31 31 namespace orxonox 32 32 { 33 /** 34 @brief 35 Destructor destroys all CommandLineArguments with it. 36 */ 33 37 CommandLine::~CommandLine() 34 38 { … … 39 43 } 40 44 } 41 45 46 /** 47 @brief 48 Returns a unique instance (Meyers Singleton). 49 */ 42 50 CommandLine& CommandLine::_getInstance() 43 51 { … … 46 54 } 47 55 48 void CommandLine::_parse(int argc, char** argv) 49 { 56 /** 57 @brief 58 Reads the command line parses the values of each argument. 59 It is then stored in the corresponding CommandLineArgument. 60 @note 61 The reason that you have to provide the string to be parsed as 62 space separted list is because of argc and argv. If you only have 63 a whole string, simply use getAllStrings() of SubString. 64 @param arguments 65 Vector of space separated strings. 66 */ 67 void CommandLine::_parse(const std::vector<std::string>& arguments) 68 { 69 // why this? See bFirstTimeParse_ declaration. 50 70 if (bFirstTimeParse_) 51 71 { … … 64 84 std::string name; 65 85 std::string shortcut; 66 std::string val ;67 for ( int i = 1; i < argc; ++i)68 { 69 if (arg v[i][0] == '-')86 std::string value; 87 for (unsigned int i = 0; i < arguments.size(); ++i) 88 { 89 if (arguments[i].size() != 0) 70 90 { 71 if (argv[i][1] <= 57 && argv[i][1] >= 48) 91 // sure not "" 92 if (arguments[i][0] == '-') 72 93 { 73 // negative number as a value 74 val += std::string(argv[i]) + " "; 94 // start with "-" 95 if (arguments[i].size() == 1) 96 { 97 // argument[i] is "-", probably a minus sign 98 value += "- "; 99 } 100 else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) 101 { 102 // negative number as a value 103 value += arguments[i] + " "; 104 } 105 else 106 { 107 // can be shortcut or full name argument 108 109 // save old data first 110 if (name != "") 111 { 112 checkFullArgument(name, value); 113 name = ""; 114 assert(shortcut == ""); 115 } 116 else if (shortcut != "") 117 { 118 checkShortcut(shortcut, value); 119 shortcut = ""; 120 assert(name == ""); 121 } 122 123 if (arguments[i][1] == '-') 124 { 125 // full name argument with "--name" 126 name = arguments[i].substr(2); 127 } 128 else 129 { 130 // shortcut with "-s" 131 shortcut = arguments[i].substr(1); 132 } 133 134 // reset value string 135 value = ""; 136 } 75 137 } 76 138 else 77 139 { 78 // save old data first 79 if (name != "") 80 { 81 checkFullArgument(name, val); 82 name = ""; 83 assert(shortcut == ""); 84 } 85 else if (shortcut != "") 86 { 87 checkShortcut(shortcut, val); 88 shortcut = ""; 89 assert(name == ""); 90 } 91 92 if (argv[i][1] == '-') 93 { 94 // full name argument 95 name = argv[i] + 2; 96 } 97 else 98 { 99 // short cut 100 shortcut = argv[i] + 1; 101 } 102 val = ""; 140 // value string 141 142 if (name == "" && shortcut == "") 143 { 144 ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); 145 } 146 147 // Concatenate strings as long as there's no new argument by "-" or "--" 148 value += arguments[i] + ' '; 103 149 } 104 150 } 105 else // value 106 { 107 if (name == "" && shortcut == "") 108 { 109 ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); 110 } 111 112 val += argv[i]; 113 } 114 } 115 } 116 151 } 152 153 // parse last argument 154 if (name != "") 155 { 156 checkFullArgument(name, value); 157 assert(shortcut == ""); 158 } 159 else if (shortcut != "") 160 { 161 checkShortcut(shortcut, value); 162 assert(name == ""); 163 } 164 } 165 166 /** 167 @brief 168 Parses an argument based on its full name. 169 @param name 170 Full name of the argument 171 @param value 172 String containing the value 173 */ 117 174 void CommandLine::checkFullArgument(const std::string& name, const std::string& value) 118 175 { … … 124 181 } 125 182 183 /** 184 @brief 185 Parses an argument based on its shortcut. 186 @param shortcut 187 Shotcut to the argument 188 @param value 189 String containing the value 190 */ 126 191 void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value) 127 192 { … … 132 197 it->second->parse(value); 133 198 } 199 200 std::string CommandLine::getUsageInformation() 201 { 202 CommandLine* inst = &_getInstance(); 203 std::string infoStr; 204 for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin(); 205 it != inst->cmdLineArgs_.end(); ++it) 206 { 207 infoStr += "[--" + it->second->getName() + " " + it->second->getInformation() + "] "; 208 } 209 return infoStr; 210 } 211 134 212 }
Note: See TracChangeset
for help on using the changeset viewer.