Changeset 8520
- Timestamp:
- May 20, 2011, 5:51:21 AM (14 years ago)
- Location:
- code/branches/unity_build/src/libraries/core
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/unity_build/src/libraries/core/CommandLineParser.cc
r7401 r8520 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 } -
code/branches/unity_build/src/libraries/core/CommandLineParser.h
r7401 r8520 51 51 #define SetCommandLineArgument(name, defaultValue) \ 52 52 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 53 = orxonox::CommandLineParser::addArgument(#name, defaultValue, false) 54 #define SetCommandLineOnlyArgument(name, defaultValue) \ 55 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 56 = orxonox::CommandLineParser::addArgument(#name, defaultValue, true) 53 = orxonox::CommandLineParser::addArgument(#name, defaultValue) 57 54 #define SetCommandLineSwitch(name) \ 58 55 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 59 = orxonox::CommandLineParser::addArgument(#name, false, false) 60 #define SetCommandLineOnlySwitch(name) \ 61 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 62 = orxonox::CommandLineParser::addArgument(#name, false, true) 63 56 = orxonox::CommandLineParser::addArgument(#name, false) 64 57 65 58 namespace orxonox … … 112 105 private: 113 106 //! Constructor initialises both value_ and defaultValue_ with defaultValue. 114 CommandLineArgument(const std::string& name, const MultiType& defaultValue , bool bCommandLineOnly)107 CommandLineArgument(const std::string& name, const MultiType& defaultValue) 115 108 : bHasDefaultValue_(true) 116 109 , name_(name) 117 110 , value_(defaultValue) 118 111 , defaultValue_(defaultValue) 119 , bCommandLineOnly_(bCommandLineOnly)120 112 { } 121 113 … … 125 117 126 118 //! Parses the value string of a command line argument. 127 void parse(const std::string& value , bool bParsingFile);119 void parse(const std::string& value); 128 120 129 121 //! Tells whether the value has been changed by the command line. … … 137 129 MultiType value_; //!< The actual value 138 130 MultiType defaultValue_; //!< Default value. Should not be changed. 139 bool bCommandLineOnly_; //!< Whether you cannot specify the value in a text file140 131 }; 141 132 … … 155 146 156 147 //! Parse redirection to internal member method. 157 static void parse CommandLine(const std::string& cmdLine) { _getInstance()._parseCommandLine(cmdLine); }158 static void parseFile() { _getInstance()._parseFile(); }148 static void parse(const std::string& cmdLine) 149 { _getInstance()._parse(cmdLine); } 159 150 160 151 static std::string getUsageInformation(); … … 168 159 { return getArgument(name)->getValue(); } 169 160 template <class T> 170 static CommandLineArgument& addArgument(const std::string& name, T defaultValue , bool bCommandLineOnly);161 static CommandLineArgument& addArgument(const std::string& name, T defaultValue); 171 162 172 163 static bool existsArgument(const std::string& name) … … 189 180 static CommandLineParser& _getInstance(); 190 181 191 void _parseCommandLine(const std::string& cmdLine); 192 void _parseFile(); 193 void _parse(const std::vector<std::string>& arguments, bool bParsingFile); 194 void checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile); 195 void checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile); 182 void _parse(const std::string& cmdLine); 183 void checkFullArgument(const std::string& name, const std::string& value); 184 void checkShortcut(const std::string& shortcut, const std::string& value); 196 185 197 186 /** … … 222 211 @param defaultValue 223 212 Default value that is used when argument was not given. 224 @param bCommandLineOnly225 Parsing a file or the command line itself226 213 */ 227 214 template <class T> 228 CommandLineArgument& CommandLineParser::addArgument(const std::string& name, T defaultValue , bool bCommandLineOnly)215 CommandLineArgument& CommandLineParser::addArgument(const std::string& name, T defaultValue) 229 216 { 230 217 OrxAssert(!_getInstance().existsArgument(name), … … 234 221 << "Please use SetCommandLineSwitch and adjust your argument: " << name); 235 222 236 return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue , bCommandLineOnly));223 return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue)); 237 224 } 238 225 } -
code/branches/unity_build/src/libraries/core/Core.cc
r8519 r8520 131 131 132 132 // Parse command line arguments AFTER the modules have been loaded (static code!) 133 CommandLineParser::parse CommandLine(cmdLine);133 CommandLineParser::parse(cmdLine); 134 134 135 135 // Set configurable paths like log, config and media … … 143 143 // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used 144 144 OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString()); 145 146 // Parse additional options file now that we know its path147 CommandLineParser::parseFile();148 145 149 146 #ifdef ORXONOX_PLATFORM_WINDOWS -
code/branches/unity_build/src/libraries/core/PathConfig.cc
r8366 r8520 74 74 75 75 SetCommandLineArgument(externalDataPath, "").information("Path to the external data files"); 76 SetCommandLine OnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");76 SetCommandLineArgument(writingPathSuffix, "").information("Additional subfolder for config and log files"); 77 77 78 78 PathConfig::PathConfig()
Note: See TracChangeset
for help on using the changeset viewer.