Changeset 3246 for code/branches/core4/src/core
- Timestamp:
- Jun 29, 2009, 1:43:41 PM (16 years ago)
- Location:
- code/branches/core4/src/core
- Files:
-
- 3 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 } -
code/branches/core4/src/core/CommandLine.h
r3196 r3246 38 38 #define SetCommandLineArgument(name, defaultValue) \ 39 39 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 40 = orxonox::CommandLine::addArgument(#name, defaultValue) 40 = orxonox::CommandLine::addArgument(#name, defaultValue, false) 41 #define SetCommandLineOnlyArgument(name, defaultValue) \ 42 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 43 = orxonox::CommandLine::addArgument(#name, defaultValue, true) 41 44 #define SetCommandLineSwitch(name) \ 42 45 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 43 = orxonox::CommandLine::addArgument(#name, false) 46 = orxonox::CommandLine::addArgument(#name, false, false) 47 #define SetCommandLineOnlySwitch(name) \ 48 orxonox::CommandLineArgument& CmdArgumentDummyBoolVar##name \ 49 = orxonox::CommandLine::addArgument(#name, false, true) 44 50 45 51 … … 93 99 private: 94 100 //! Constructor initialises both value_ and defaultValue_ with defaultValue. 95 CommandLineArgument(const std::string& name, const MultiType& defaultValue )101 CommandLineArgument(const std::string& name, const MultiType& defaultValue, bool bCommandLineOnly) 96 102 : bHasDefaultValue_(true) 97 103 , name_(name) 98 104 , value_(defaultValue) 99 105 , defaultValue_(defaultValue) 106 , bCommandLineOnly_(bCommandLineOnly) 100 107 { } 101 108 … … 105 112 106 113 //! Parses the value string of a command line argument. 107 void parse(const std::string& value );114 void parse(const std::string& value, bool bParsingFile); 108 115 109 116 //! Tells whether the value has been changed by the command line. … … 115 122 std::string usageInformation_; //!< Tells about the usage of this parameter 116 123 117 MultiType value_; //!< The actual value 118 MultiType defaultValue_; //!< Default value. Should not be changed. 124 MultiType value_; //!< The actual value 125 MultiType defaultValue_; //!< Default value. Should not be changed. 126 bool bCommandLineOnly_; //!< Whether you cannot specify the value in a text file 119 127 }; 120 128 … … 134 142 135 143 //! Parse redirection to internal member method. 136 static void parseAll(int argc, char** argv) { _getInstance()._parseAll(argc, argv); } 144 static void parseCommandLine(int argc, char** argv) { _getInstance()._parseCommandLine(argc, argv); } 145 static void parseFile() { _getInstance()._parseFile(); } 137 146 138 147 static std::string getUsageInformation(); … … 146 155 { return getArgument(name)->getValue(); } 147 156 template <class T> 148 static CommandLineArgument& addArgument(const std::string& name, T defaultValue );157 static CommandLineArgument& addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly); 149 158 150 159 static bool existsArgument(const std::string& name) … … 165 174 static CommandLine& _getInstance(); 166 175 167 void _parseAll(int argc, char** argv); 168 void _parse(const std::vector<std::string>& arguments); 169 void checkFullArgument(const std::string& name, const std::string& value); 170 void checkShortcut(const std::string& shortcut, const std::string& value); 176 void _parseCommandLine(int argc, char** argv); 177 void _parseFile(); 178 void _parse(const std::vector<std::string>& arguments, bool bParsingFile); 179 void checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile); 180 void checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile); 171 181 172 182 /** … … 199 209 */ 200 210 template <class T> 201 CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue )211 CommandLineArgument& CommandLine::addArgument(const std::string& name, T defaultValue, bool bCommandLineOnly) 202 212 { 203 213 OrxAssert(!_getInstance().existsArgument(name), 204 214 "Cannot add a command line argument with name '" + name + "' twice."); 205 215 206 return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue ));216 return *(_getInstance().cmdLineArgs_[name] = new CommandLineArgument(name, defaultValue, bCommandLineOnly)); 207 217 } 208 218 } -
code/branches/core4/src/core/Core.cc
r3214 r3246 88 88 89 89 SetCommandLineArgument(mediaPath, "").information("PATH"); 90 SetCommandLine Argument(writingPathSuffix, "").information("DIR");90 SetCommandLineOnlyArgument(writingPathSuffix, "").information("DIR"); 91 91 SetCommandLineArgument(settingsFile, "orxonox.ini"); 92 92 SetCommandLineArgument(limitToCPU, 0).information("0: off | #cpu"); … … 103 103 { 104 104 // Parse command line arguments fist 105 try 106 { 107 CommandLine::parseAll(argc, argv); 108 } 109 catch (ArgumentException& ex) 110 { 111 COUT(1) << ex.what() << std::endl; 112 COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl; 113 } 105 CommandLine::parseCommandLine(argc, argv); 114 106 115 107 // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump … … 137 129 // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used 138 130 OutputHandler::getOutStream().setLogPath(Core::getLogPathString()); 131 132 CommandLine::parseFile(); 139 133 140 134 // Manage ini files and set the default settings file (usually orxonox.ini)
Note: See TracChangeset
for help on using the changeset viewer.