[1663] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[6021] | 29 | #include "CommandLineParser.h" |
---|
[1663] | 30 | |
---|
[3280] | 31 | #include <algorithm> |
---|
| 32 | #include <sstream> |
---|
[3196] | 33 | |
---|
| 34 | #include "util/Convert.h" |
---|
| 35 | #include "util/Debug.h" |
---|
| 36 | #include "util/Exception.h" |
---|
[3280] | 37 | #include "util/StringUtils.h" |
---|
[2103] | 38 | #include "util/SubString.h" |
---|
[5929] | 39 | #include "PathConfig.h" |
---|
[1690] | 40 | |
---|
[1663] | 41 | namespace orxonox |
---|
| 42 | { |
---|
[3280] | 43 | SetCommandLineOnlyArgument(optionsFile, "start.ini").shortcut("o"); |
---|
[2103] | 44 | |
---|
[1664] | 45 | /** |
---|
| 46 | @brief |
---|
[2087] | 47 | Parses a value string for a command line argument. |
---|
| 48 | It simply uses convertValue(Output, Input) to do that. |
---|
| 49 | Bools are treated specially. That is necessary |
---|
| 50 | so that you can have simple command line switches. |
---|
| 51 | */ |
---|
[3280] | 52 | void CommandLineArgument::parse(const std::string& value, bool bParsingFile) |
---|
[2087] | 53 | { |
---|
[3280] | 54 | if (bParsingFile && this->bCommandLineOnly_) |
---|
| 55 | ThrowException(Argument, "Command line argument '" + getName() + "' is not allowed in files."); |
---|
| 56 | if (value_.getType() == MT_Type::Bool) |
---|
[2087] | 57 | { |
---|
| 58 | // simulate command line switch |
---|
| 59 | bool temp; |
---|
| 60 | if (convertValue(&temp, value)) |
---|
| 61 | { |
---|
| 62 | this->bHasDefaultValue_ = false; |
---|
| 63 | this->value_ = temp; |
---|
| 64 | } |
---|
[6417] | 65 | else if (value.empty()) |
---|
[2087] | 66 | { |
---|
| 67 | this->bHasDefaultValue_ = false; |
---|
| 68 | this->value_ = true; |
---|
| 69 | } |
---|
| 70 | else |
---|
| 71 | ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); |
---|
| 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
| 75 | if (!value_.setValue(value)) |
---|
| 76 | { |
---|
| 77 | value_.setValue(defaultValue_); |
---|
| 78 | ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); |
---|
| 79 | } |
---|
| 80 | else |
---|
| 81 | this->bHasDefaultValue_ = false; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | @brief |
---|
[1664] | 88 | Destructor destroys all CommandLineArguments with it. |
---|
| 89 | */ |
---|
[6021] | 90 | CommandLineParser::~CommandLineParser() |
---|
[1663] | 91 | { |
---|
[6021] | 92 | CommandLineParser::destroyAllArguments(); |
---|
[1663] | 93 | } |
---|
[1664] | 94 | |
---|
| 95 | /** |
---|
| 96 | @brief |
---|
| 97 | Returns a unique instance (Meyers Singleton). |
---|
| 98 | */ |
---|
[6021] | 99 | CommandLineParser& CommandLineParser::_getInstance() |
---|
[1663] | 100 | { |
---|
[6021] | 101 | static CommandLineParser instance; |
---|
[1663] | 102 | return instance; |
---|
| 103 | } |
---|
| 104 | |
---|
[1664] | 105 | /** |
---|
| 106 | @brief |
---|
[2662] | 107 | Destroys all command line arguments. This should be called at the end |
---|
| 108 | of main. Do not use before that. |
---|
| 109 | */ |
---|
[6021] | 110 | void CommandLineParser::destroyAllArguments() |
---|
[2662] | 111 | { |
---|
| 112 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.begin(); |
---|
| 113 | it != _getInstance().cmdLineArgs_.end(); ++it) |
---|
| 114 | delete it->second; |
---|
| 115 | _getInstance().cmdLineArgs_.clear(); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | @brief |
---|
[1664] | 120 | Reads the command line parses the values of each argument. |
---|
| 121 | It is then stored in the corresponding CommandLineArgument. |
---|
| 122 | @note |
---|
| 123 | The reason that you have to provide the string to be parsed as |
---|
| 124 | space separted list is because of argc and argv. If you only have |
---|
| 125 | a whole string, simply use getAllStrings() of SubString. |
---|
| 126 | @param arguments |
---|
| 127 | Vector of space separated strings. |
---|
| 128 | */ |
---|
[6021] | 129 | void CommandLineParser::_parse(const std::vector<std::string>& arguments, bool bParsingFile) |
---|
[1663] | 130 | { |
---|
[3280] | 131 | try |
---|
[1663] | 132 | { |
---|
[3280] | 133 | // why this? See bFirstTimeParse_ declaration. |
---|
| 134 | if (bFirstTimeParse_) |
---|
[1663] | 135 | { |
---|
[3280] | 136 | // first shove all the shortcuts in a map |
---|
| 137 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); |
---|
| 138 | it != cmdLineArgs_.end(); ++it) |
---|
| 139 | { |
---|
| 140 | OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(), |
---|
| 141 | "Cannot have two command line shortcut with the same name."); |
---|
[6417] | 142 | if (!it->second->getShortcut().empty()) |
---|
[3280] | 143 | cmdLineArgsShortcut_[it->second->getShortcut()] = it->second; |
---|
| 144 | } |
---|
| 145 | bFirstTimeParse_ = false; |
---|
[1663] | 146 | } |
---|
| 147 | |
---|
[3280] | 148 | std::string name; |
---|
| 149 | std::string shortcut; |
---|
| 150 | std::string value; |
---|
| 151 | for (unsigned int i = 0; i < arguments.size(); ++i) |
---|
[1663] | 152 | { |
---|
[3280] | 153 | if (arguments[i].size() != 0) |
---|
[1663] | 154 | { |
---|
[3280] | 155 | // sure not "" |
---|
| 156 | if (arguments[i][0] == '-') |
---|
[1663] | 157 | { |
---|
[3280] | 158 | // start with "-" |
---|
| 159 | if (arguments[i].size() == 1) |
---|
[1664] | 160 | { |
---|
[3280] | 161 | // argument[i] is "-", probably a minus sign |
---|
| 162 | value += "- "; |
---|
[1664] | 163 | } |
---|
[3280] | 164 | else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) |
---|
[1664] | 165 | { |
---|
[3280] | 166 | // negative number as a value |
---|
[6417] | 167 | value += arguments[i] + ' '; |
---|
[1664] | 168 | } |
---|
[3280] | 169 | else |
---|
| 170 | { |
---|
| 171 | // can be shortcut or full name argument |
---|
[1664] | 172 | |
---|
[3280] | 173 | // save old data first |
---|
| 174 | value = removeTrailingWhitespaces(value); |
---|
[6417] | 175 | if (!name.empty()) |
---|
[3280] | 176 | { |
---|
| 177 | checkFullArgument(name, value, bParsingFile); |
---|
[6417] | 178 | name.clear(); |
---|
| 179 | assert(shortcut.empty()); |
---|
[3280] | 180 | } |
---|
[6417] | 181 | else if (!shortcut.empty()) |
---|
[3280] | 182 | { |
---|
| 183 | checkShortcut(shortcut, value, bParsingFile); |
---|
[6417] | 184 | shortcut.clear(); |
---|
| 185 | assert(name.empty()); |
---|
[3280] | 186 | } |
---|
| 187 | |
---|
| 188 | if (arguments[i][1] == '-') |
---|
| 189 | { |
---|
| 190 | // full name argument with "--name" |
---|
| 191 | name = arguments[i].substr(2); |
---|
| 192 | } |
---|
| 193 | else |
---|
| 194 | { |
---|
| 195 | // shortcut with "-s" |
---|
| 196 | shortcut = arguments[i].substr(1); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | // reset value string |
---|
[6417] | 200 | value.clear(); |
---|
[1664] | 201 | } |
---|
[3280] | 202 | } |
---|
| 203 | else |
---|
| 204 | { |
---|
| 205 | // value string |
---|
| 206 | |
---|
[6417] | 207 | if (name.empty() && shortcut.empty()) |
---|
[1664] | 208 | { |
---|
[3280] | 209 | ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); |
---|
[1664] | 210 | } |
---|
| 211 | |
---|
[3280] | 212 | // Concatenate strings as long as there's no new argument by "-" or "--" |
---|
| 213 | value += arguments[i] + ' '; |
---|
[1663] | 214 | } |
---|
| 215 | } |
---|
[3280] | 216 | } |
---|
[1664] | 217 | |
---|
[3280] | 218 | // parse last argument |
---|
| 219 | value = removeTrailingWhitespaces(value); |
---|
[6417] | 220 | if (!name.empty()) |
---|
[3280] | 221 | { |
---|
| 222 | checkFullArgument(name, value, bParsingFile); |
---|
[6417] | 223 | assert(shortcut.empty()); |
---|
[1663] | 224 | } |
---|
[6417] | 225 | else if (!shortcut.empty()) |
---|
[3280] | 226 | { |
---|
| 227 | checkShortcut(shortcut, value, bParsingFile); |
---|
[6417] | 228 | assert(name.empty()); |
---|
[3280] | 229 | } |
---|
[1663] | 230 | } |
---|
[3280] | 231 | catch (const ArgumentException& ex) |
---|
[1664] | 232 | { |
---|
[3280] | 233 | COUT(0) << "Could not parse command line (including additional files): " << ex.what() << std::endl; |
---|
[6021] | 234 | COUT(0) << CommandLineParser::getUsageInformation() << std::endl; |
---|
[3280] | 235 | throw GeneralException(""); |
---|
[1664] | 236 | } |
---|
[1663] | 237 | } |
---|
| 238 | |
---|
[1664] | 239 | /** |
---|
| 240 | @brief |
---|
| 241 | Parses an argument based on its full name. |
---|
| 242 | @param name |
---|
| 243 | Full name of the argument |
---|
| 244 | @param value |
---|
| 245 | String containing the value |
---|
| 246 | */ |
---|
[6021] | 247 | void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile) |
---|
[1663] | 248 | { |
---|
[2087] | 249 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); |
---|
[1663] | 250 | if (it == cmdLineArgs_.end()) |
---|
| 251 | ThrowException(Argument, "Command line argument '" + name + "' does not exist."); |
---|
| 252 | |
---|
[3280] | 253 | it->second->parse(value, bParsingFile); |
---|
[1663] | 254 | } |
---|
| 255 | |
---|
[1664] | 256 | /** |
---|
| 257 | @brief |
---|
| 258 | Parses an argument based on its shortcut. |
---|
| 259 | @param shortcut |
---|
| 260 | Shotcut to the argument |
---|
| 261 | @param value |
---|
| 262 | String containing the value |
---|
| 263 | */ |
---|
[6021] | 264 | void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile) |
---|
[1663] | 265 | { |
---|
[2087] | 266 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); |
---|
[1663] | 267 | if (it == cmdLineArgsShortcut_.end()) |
---|
| 268 | ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); |
---|
| 269 | |
---|
[3280] | 270 | it->second->parse(value, bParsingFile); |
---|
[1663] | 271 | } |
---|
[1664] | 272 | |
---|
[6021] | 273 | std::string CommandLineParser::getUsageInformation() |
---|
[1664] | 274 | { |
---|
[6021] | 275 | CommandLineParser& inst = _getInstance(); |
---|
[3280] | 276 | std::ostringstream infoStr; |
---|
| 277 | |
---|
| 278 | // determine maximum name size |
---|
| 279 | size_t maxNameSize = 0; |
---|
| 280 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin(); |
---|
| 281 | it != inst.cmdLineArgs_.end(); ++it) |
---|
[1664] | 282 | { |
---|
[3280] | 283 | maxNameSize = std::max(it->second->getName().size(), maxNameSize); |
---|
[1664] | 284 | } |
---|
[3280] | 285 | |
---|
[5929] | 286 | infoStr << std::endl; |
---|
[3280] | 287 | infoStr << "Usage: orxonox [options]" << std::endl; |
---|
| 288 | infoStr << "Available options:" << std::endl; |
---|
| 289 | |
---|
| 290 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin(); |
---|
| 291 | it != inst.cmdLineArgs_.end(); ++it) |
---|
| 292 | { |
---|
[6417] | 293 | if (!it->second->getShortcut().empty()) |
---|
[3280] | 294 | infoStr << " [-" << it->second->getShortcut() << "] "; |
---|
| 295 | else |
---|
| 296 | infoStr << " "; |
---|
[6417] | 297 | infoStr << "--" << it->second->getName() << ' '; |
---|
[3280] | 298 | if (it->second->getValue().getType() != MT_Type::Bool) |
---|
| 299 | infoStr << "ARG "; |
---|
| 300 | else |
---|
| 301 | infoStr << " "; |
---|
| 302 | // fill with the necessary amount of blanks |
---|
| 303 | infoStr << std::string(maxNameSize - it->second->getName().size(), ' '); |
---|
| 304 | infoStr << ": " << it->second->getInformation(); |
---|
| 305 | infoStr << std::endl; |
---|
| 306 | } |
---|
| 307 | return infoStr.str(); |
---|
[1664] | 308 | } |
---|
| 309 | |
---|
[2087] | 310 | /** |
---|
| 311 | @brief |
---|
| 312 | Retrieves a CommandLineArgument. |
---|
| 313 | The method throws an exception if 'name' was not found or the value could not be converted. |
---|
| 314 | @note |
---|
| 315 | You shold of course not call this method before the command line has been parsed. |
---|
| 316 | */ |
---|
[6021] | 317 | const CommandLineArgument* CommandLineParser::getArgument(const std::string& name) |
---|
[2087] | 318 | { |
---|
| 319 | std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); |
---|
| 320 | if (it == _getInstance().cmdLineArgs_.end()) |
---|
| 321 | { |
---|
| 322 | ThrowException(Argument, "Could find command line argument '" + name + "'."); |
---|
| 323 | } |
---|
| 324 | else |
---|
| 325 | { |
---|
| 326 | return it->second; |
---|
| 327 | } |
---|
| 328 | } |
---|
[2103] | 329 | |
---|
| 330 | /** |
---|
| 331 | @brief |
---|
[3280] | 332 | Parses only the command line for CommandLineArguments. |
---|
[2103] | 333 | */ |
---|
[6021] | 334 | void CommandLineParser::_parseCommandLine(const std::string& cmdLine) |
---|
[2103] | 335 | { |
---|
| 336 | std::vector<std::string> args; |
---|
[7284] | 337 | SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false); |
---|
[3323] | 338 | for (unsigned i = 0; i < tokens.size(); ++i) |
---|
| 339 | args.push_back(tokens[i]); |
---|
[3280] | 340 | this->_parse(args, false); |
---|
| 341 | } |
---|
[2103] | 342 | |
---|
[3280] | 343 | /** |
---|
| 344 | @brief |
---|
| 345 | Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments. |
---|
| 346 | */ |
---|
[6021] | 347 | void CommandLineParser::_parseFile() |
---|
[3280] | 348 | { |
---|
[6417] | 349 | const std::string& filename = CommandLineParser::getValue("optionsFile").getString(); |
---|
[2710] | 350 | |
---|
[2103] | 351 | // look for additional arguments in given file or start.ini as default |
---|
| 352 | // They will not overwrite the arguments given directly |
---|
| 353 | std::ifstream file; |
---|
[5929] | 354 | file.open((PathConfig::getConfigPathString() + filename).c_str()); |
---|
[3280] | 355 | std::vector<std::string> args; |
---|
[2103] | 356 | if (file) |
---|
| 357 | { |
---|
| 358 | while (!file.eof()) |
---|
| 359 | { |
---|
| 360 | std::string line; |
---|
| 361 | std::getline(file, line); |
---|
| 362 | line = removeTrailingWhitespaces(line); |
---|
| 363 | //if (!(line[0] == '#' || line[0] == '%')) |
---|
| 364 | //{ |
---|
[7284] | 365 | SubString tokens(line, " ", " ", false, '\\', true, '"', true, '\0', '\0', false, '#'); |
---|
[2103] | 366 | for (unsigned i = 0; i < tokens.size(); ++i) |
---|
| 367 | if (tokens[i][0] != '#') |
---|
| 368 | args.push_back(tokens[i]); |
---|
| 369 | //args.insert(args.end(), tokens.getAllStrings().begin(), tokens.getAllStrings().end()); |
---|
| 370 | //} |
---|
| 371 | } |
---|
| 372 | file.close(); |
---|
| 373 | } |
---|
| 374 | |
---|
[3280] | 375 | _parse(args, true); |
---|
[2103] | 376 | } |
---|
[1663] | 377 | } |
---|