Changeset 3246 for code/branches/core4/src/core/CommandLine.cc
- Timestamp:
- Jun 29, 2009, 1:43:41 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core4/src/core/CommandLine.cc
r3196 r3246 40 40 namespace orxonox 41 41 { 42 SetCommandLine Argument(optionsFile, "start.ini").shortcut("o");42 SetCommandLineOnlyArgument(optionsFile, "start.ini").shortcut("o"); 43 43 44 44 /** … … 49 49 so that you can have simple command line switches. 50 50 */ 51 void CommandLineArgument::parse(const std::string& value) 52 { 51 void CommandLineArgument::parse(const std::string& value, bool bParsingFile) 52 { 53 if (bParsingFile && this->bCommandLineOnly_) 54 ThrowException(Argument, "Command line argument '" + getName() + "' is not allowed in files."); 53 55 if (value_.getType() == MT_bool) 54 56 { … … 66 68 } 67 69 else 68 {69 70 ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); 70 }71 71 } 72 72 else … … 126 126 Vector of space separated strings. 127 127 */ 128 void CommandLine::_parse(const std::vector<std::string>& arguments) 129 { 130 // why this? See bFirstTimeParse_ declaration. 131 if (bFirstTimeParse_) 132 { 133 // first shove all the shortcuts in a map 134 for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); 135 it != cmdLineArgs_.end(); ++it) 136 { 137 OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(), 138 "Cannot have two command line shortcut with the same name."); 139 if (it->second->getShortcut() != "") 140 cmdLineArgsShortcut_[it->second->getShortcut()] = it->second; 141 } 142 bFirstTimeParse_ = false; 143 } 144 145 std::string name; 146 std::string shortcut; 147 std::string value; 148 for (unsigned int i = 0; i < arguments.size(); ++i) 149 { 150 if (arguments[i].size() != 0) 151 { 152 // sure not "" 153 if (arguments[i][0] == '-') 128 void CommandLine::_parse(const std::vector<std::string>& arguments, bool bParsingFile) 129 { 130 try 131 { 132 // why this? See bFirstTimeParse_ declaration. 133 if (bFirstTimeParse_) 134 { 135 // first shove all the shortcuts in a map 136 for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); 137 it != cmdLineArgs_.end(); ++it) 154 138 { 155 // start with "-" 156 if (arguments[i].size() == 1) 139 OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(), 140 "Cannot have two command line shortcut with the same name."); 141 if (it->second->getShortcut() != "") 142 cmdLineArgsShortcut_[it->second->getShortcut()] = it->second; 143 } 144 bFirstTimeParse_ = false; 145 } 146 147 std::string name; 148 std::string shortcut; 149 std::string value; 150 for (unsigned int i = 0; i < arguments.size(); ++i) 151 { 152 if (arguments[i].size() != 0) 153 { 154 // sure not "" 155 if (arguments[i][0] == '-') 157 156 { 158 // argument[i] is "-", probably a minus sign 159 value += "- "; 160 } 161 else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) 162 { 163 // negative number as a value 164 value += arguments[i] + " "; 157 // start with "-" 158 if (arguments[i].size() == 1) 159 { 160 // argument[i] is "-", probably a minus sign 161 value += "- "; 162 } 163 else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) 164 { 165 // negative number as a value 166 value += arguments[i] + " "; 167 } 168 else 169 { 170 // can be shortcut or full name argument 171 172 // save old data first 173 value = removeTrailingWhitespaces(value); 174 if (name != "") 175 { 176 checkFullArgument(name, value, bParsingFile); 177 name = ""; 178 assert(shortcut == ""); 179 } 180 else if (shortcut != "") 181 { 182 checkShortcut(shortcut, value, bParsingFile); 183 shortcut = ""; 184 assert(name == ""); 185 } 186 187 if (arguments[i][1] == '-') 188 { 189 // full name argument with "--name" 190 name = arguments[i].substr(2); 191 } 192 else 193 { 194 // shortcut with "-s" 195 shortcut = arguments[i].substr(1); 196 } 197 198 // reset value string 199 value = ""; 200 } 165 201 } 166 202 else 167 203 { 168 // can be shortcut or full name argument 169 170 // save old data first 171 value = removeTrailingWhitespaces(value); 172 if (name != "") 204 // value string 205 206 if (name == "" && shortcut == "") 173 207 { 174 checkFullArgument(name, value); 175 name = ""; 176 assert(shortcut == ""); 208 ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); 177 209 } 178 else if (shortcut != "") 179 { 180 checkShortcut(shortcut, value); 181 shortcut = ""; 182 assert(name == ""); 183 } 184 185 if (arguments[i][1] == '-') 186 { 187 // full name argument with "--name" 188 name = arguments[i].substr(2); 189 } 190 else 191 { 192 // shortcut with "-s" 193 shortcut = arguments[i].substr(1); 194 } 195 196 // reset value string 197 value = ""; 210 211 // Concatenate strings as long as there's no new argument by "-" or "--" 212 value += arguments[i] + ' '; 198 213 } 199 214 } 200 else 201 { 202 // value string 203 204 if (name == "" && shortcut == "") 205 { 206 ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); 207 } 208 209 // Concatenate strings as long as there's no new argument by "-" or "--" 210 value += arguments[i] + ' '; 211 } 212 } 213 } 214 215 // parse last argument 216 value = removeTrailingWhitespaces(value); 217 if (name != "") 218 { 219 checkFullArgument(name, value); 220 assert(shortcut == ""); 221 } 222 else if (shortcut != "") 223 { 224 checkShortcut(shortcut, value); 225 assert(name == ""); 215 } 216 217 // parse last argument 218 value = removeTrailingWhitespaces(value); 219 if (name != "") 220 { 221 checkFullArgument(name, value, bParsingFile); 222 assert(shortcut == ""); 223 } 224 else if (shortcut != "") 225 { 226 checkShortcut(shortcut, value, bParsingFile); 227 assert(name == ""); 228 } 229 } 230 catch (const ArgumentException& ex) 231 { 232 COUT(0) << "Could not parse command line (including additional files): " << ex.what() << std::endl; 233 COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl; 234 throw GeneralException(""); 226 235 } 227 236 } … … 235 244 String containing the value 236 245 */ 237 void CommandLine::checkFullArgument(const std::string& name, const std::string& value )246 void CommandLine::checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile) 238 247 { 239 248 std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); … … 241 250 ThrowException(Argument, "Command line argument '" + name + "' does not exist."); 242 251 243 it->second->parse(value );252 it->second->parse(value, bParsingFile); 244 253 } 245 254 … … 252 261 String containing the value 253 262 */ 254 void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value )263 void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile) 255 264 { 256 265 std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); … … 258 267 ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); 259 268 260 it->second->parse(value );269 it->second->parse(value, bParsingFile); 261 270 } 262 271 … … 295 304 /** 296 305 @brief 297 Parses both command line and start.ini for CommandLineArguments. 298 */ 299 void CommandLine::_parseAll(int argc, char** argv) 300 { 301 // parse command line first 306 Parses only the command line for CommandLineArguments. 307 */ 308 void CommandLine::_parseCommandLine(int argc, char** argv) 309 { 302 310 std::vector<std::string> args; 303 311 for (int i = 1; i < argc; ++i) 304 312 args.push_back(argv[i]); 305 this->_parse(args); 306 313 this->_parse(args, false); 314 } 315 316 /** 317 @brief 318 Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments. 319 */ 320 void CommandLine::_parseFile() 321 { 307 322 std::string filename = CommandLine::getValue("optionsFile").getString(); 308 323 boost::filesystem::path filepath(Core::getConfigPath() / filename); … … 312 327 std::ifstream file; 313 328 file.open(filepath.string().c_str()); 314 args.clear();329 std::vector<std::string> args; 315 330 if (file) 316 331 { … … 332 347 } 333 348 334 try 335 { 336 _parse(args); 337 } 338 catch (orxonox::ArgumentException& ex) 339 { 340 COUT(1) << "An Exception occured while parsing " << filename << std::endl; 341 throw(ex); 342 } 349 _parse(args, true); 343 350 } 344 351 }
Note: See TracChangeset
for help on using the changeset viewer.