Changeset 8729 for code/trunk/src/libraries/core/CommandLineParser.cc
- Timestamp:
- Jul 4, 2011, 2:47:44 AM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
-
code/trunk/src/libraries/core/CommandLineParser.cc
r7401 r8729 41 41 namespace orxonox 42 42 { 43 SetCommandLineOnlyArgument(optionsFile, "start.ini").shortcut("o");44 45 43 /** 46 44 @brief … … 50 48 so that you can have simple command line switches. 51 49 */ 52 void CommandLineArgument::parse(const std::string& value, bool bParsingFile) 53 { 54 if (bParsingFile && this->bCommandLineOnly_) 55 ThrowException(Argument, "Command line argument '" + getName() + "' is not allowed in files."); 50 void CommandLineArgument::parse(const std::string& value) 51 { 56 52 if (value_.getType() == MT_Type::Bool) 57 53 { … … 116 112 } 117 113 118 /** 119 @brief 120 Reads the command line parses the values of each argument. 121 It is then stored in the corresponding CommandLineArgument. 114 /** Parses the command line string for arguments and stores these. 122 115 @note 123 116 The reason that you have to provide the string to be parsed as 124 space separ ted list is because of argc and argv. If you only have117 space separated list is because of argc and argv. If you only have 125 118 a whole string, simply use getAllStrings() of SubString. 126 @param arguments 127 Vector of space separated strings. 128 @param bParsingFile 129 Parsing a file or the command line itself 130 */ 131 void CommandLineParser::_parse(const std::vector<std::string>& arguments, bool bParsingFile) 132 { 119 @param cmdLine 120 Command line string WITHOUT the execution path. 121 */ 122 void CommandLineParser::_parse(const std::string& cmdLine) 123 { 124 std::vector<std::string> arguments; 125 SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false); 126 for (unsigned i = 0; i < tokens.size(); ++i) 127 arguments.push_back(tokens[i]); 128 133 129 try 134 130 { … … 177 173 if (!name.empty()) 178 174 { 179 checkFullArgument(name, value , bParsingFile);175 checkFullArgument(name, value); 180 176 name.clear(); 181 177 assert(shortcut.empty()); … … 183 179 else if (!shortcut.empty()) 184 180 { 185 checkShortcut(shortcut, value , bParsingFile);181 checkShortcut(shortcut, value); 186 182 shortcut.clear(); 187 183 assert(name.empty()); … … 222 218 if (!name.empty()) 223 219 { 224 checkFullArgument(name, value , bParsingFile);220 checkFullArgument(name, value); 225 221 assert(shortcut.empty()); 226 222 } 227 223 else if (!shortcut.empty()) 228 224 { 229 checkShortcut(shortcut, value , bParsingFile);225 checkShortcut(shortcut, value); 230 226 assert(name.empty()); 231 227 } … … 233 229 catch (const ArgumentException& ex) 234 230 { 235 COUT(0) << "Could not parse command line (including additional files): " << ex.what() << std::endl;231 COUT(0) << "Could not parse command line: " << ex.what() << std::endl; 236 232 COUT(0) << CommandLineParser::getUsageInformation() << std::endl; 237 233 throw GeneralException(""); … … 249 245 Parsing a file or the command line itself 250 246 */ 251 void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value , bool bParsingFile)247 void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value) 252 248 { 253 249 std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); … … 255 251 ThrowException(Argument, "Command line argument '" + name + "' does not exist."); 256 252 257 it->second->parse(value , bParsingFile);253 it->second->parse(value); 258 254 } 259 255 … … 268 264 Parsing a file or the command line itself 269 265 */ 270 void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value , bool bParsingFile)266 void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value) 271 267 { 272 268 std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); … … 274 270 ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); 275 271 276 it->second->parse(value , bParsingFile);272 it->second->parse(value); 277 273 } 278 274 … … 342 338 } 343 339 } 344 345 /**346 @brief347 Parses only the command line for CommandLineArguments.348 */349 void CommandLineParser::_parseCommandLine(const std::string& cmdLine)350 {351 std::vector<std::string> args;352 SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false);353 for (unsigned i = 0; i < tokens.size(); ++i)354 args.push_back(tokens[i]);355 this->_parse(args, false);356 }357 358 /**359 @brief360 Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments.361 */362 void CommandLineParser::_parseFile()363 {364 const std::string& filename = CommandLineParser::getValue("optionsFile").getString();365 366 // look for additional arguments in given file or start.ini as default367 // They will not overwrite the arguments given directly368 std::ifstream file;369 file.open((PathConfig::getConfigPathString() + filename).c_str());370 std::vector<std::string> args;371 if (file)372 {373 while (!file.eof())374 {375 std::string line;376 std::getline(file, line);377 line = removeTrailingWhitespaces(line);378 //if (!(line[0] == '#' || line[0] == '%'))379 //{380 SubString tokens(line, " ", " ", false, '\\', true, '"', true, '\0', '\0', false, '#');381 for (unsigned i = 0; i < tokens.size(); ++i)382 if (tokens[i][0] != '#')383 args.push_back(tokens[i]);384 //args.insert(args.end(), tokens.getAllStrings().begin(), tokens.getAllStrings().end());385 //}386 }387 file.close();388 }389 390 _parse(args, true);391 }392 340 }
Note: See TracChangeset
for help on using the changeset viewer.