| [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. |
|---|
| [7401] | 128 | @param bParsingFile |
|---|
| 129 | Parsing a file or the command line itself |
|---|
| [1664] | 130 | */ |
|---|
| [6021] | 131 | void CommandLineParser::_parse(const std::vector<std::string>& arguments, bool bParsingFile) |
|---|
| [1663] | 132 | { |
|---|
| [3280] | 133 | try |
|---|
| [1663] | 134 | { |
|---|
| [3280] | 135 | // why this? See bFirstTimeParse_ declaration. |
|---|
| 136 | if (bFirstTimeParse_) |
|---|
| [1663] | 137 | { |
|---|
| [3280] | 138 | // first shove all the shortcuts in a map |
|---|
| 139 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); |
|---|
| 140 | it != cmdLineArgs_.end(); ++it) |
|---|
| 141 | { |
|---|
| 142 | OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(), |
|---|
| 143 | "Cannot have two command line shortcut with the same name."); |
|---|
| [6417] | 144 | if (!it->second->getShortcut().empty()) |
|---|
| [3280] | 145 | cmdLineArgsShortcut_[it->second->getShortcut()] = it->second; |
|---|
| 146 | } |
|---|
| 147 | bFirstTimeParse_ = false; |
|---|
| [1663] | 148 | } |
|---|
| 149 | |
|---|
| [3280] | 150 | std::string name; |
|---|
| 151 | std::string shortcut; |
|---|
| 152 | std::string value; |
|---|
| 153 | for (unsigned int i = 0; i < arguments.size(); ++i) |
|---|
| [1663] | 154 | { |
|---|
| [3280] | 155 | if (arguments[i].size() != 0) |
|---|
| [1663] | 156 | { |
|---|
| [3280] | 157 | // sure not "" |
|---|
| 158 | if (arguments[i][0] == '-') |
|---|
| [1663] | 159 | { |
|---|
| [3280] | 160 | // start with "-" |
|---|
| 161 | if (arguments[i].size() == 1) |
|---|
| [1664] | 162 | { |
|---|
| [3280] | 163 | // argument[i] is "-", probably a minus sign |
|---|
| 164 | value += "- "; |
|---|
| [1664] | 165 | } |
|---|
| [3280] | 166 | else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) |
|---|
| [1664] | 167 | { |
|---|
| [3280] | 168 | // negative number as a value |
|---|
| [6417] | 169 | value += arguments[i] + ' '; |
|---|
| [1664] | 170 | } |
|---|
| [3280] | 171 | else |
|---|
| 172 | { |
|---|
| 173 | // can be shortcut or full name argument |
|---|
| [1664] | 174 | |
|---|
| [3280] | 175 | // save old data first |
|---|
| 176 | value = removeTrailingWhitespaces(value); |
|---|
| [6417] | 177 | if (!name.empty()) |
|---|
| [3280] | 178 | { |
|---|
| 179 | checkFullArgument(name, value, bParsingFile); |
|---|
| [6417] | 180 | name.clear(); |
|---|
| 181 | assert(shortcut.empty()); |
|---|
| [3280] | 182 | } |
|---|
| [6417] | 183 | else if (!shortcut.empty()) |
|---|
| [3280] | 184 | { |
|---|
| 185 | checkShortcut(shortcut, value, bParsingFile); |
|---|
| [6417] | 186 | shortcut.clear(); |
|---|
| 187 | assert(name.empty()); |
|---|
| [3280] | 188 | } |
|---|
| 189 | |
|---|
| 190 | if (arguments[i][1] == '-') |
|---|
| 191 | { |
|---|
| 192 | // full name argument with "--name" |
|---|
| 193 | name = arguments[i].substr(2); |
|---|
| 194 | } |
|---|
| 195 | else |
|---|
| 196 | { |
|---|
| 197 | // shortcut with "-s" |
|---|
| 198 | shortcut = arguments[i].substr(1); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | // reset value string |
|---|
| [6417] | 202 | value.clear(); |
|---|
| [1664] | 203 | } |
|---|
| [3280] | 204 | } |
|---|
| 205 | else |
|---|
| 206 | { |
|---|
| 207 | // value string |
|---|
| 208 | |
|---|
| [6417] | 209 | if (name.empty() && shortcut.empty()) |
|---|
| [1664] | 210 | { |
|---|
| [3280] | 211 | ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); |
|---|
| [1664] | 212 | } |
|---|
| 213 | |
|---|
| [3280] | 214 | // Concatenate strings as long as there's no new argument by "-" or "--" |
|---|
| 215 | value += arguments[i] + ' '; |
|---|
| [1663] | 216 | } |
|---|
| 217 | } |
|---|
| [3280] | 218 | } |
|---|
| [1664] | 219 | |
|---|
| [3280] | 220 | // parse last argument |
|---|
| 221 | value = removeTrailingWhitespaces(value); |
|---|
| [6417] | 222 | if (!name.empty()) |
|---|
| [3280] | 223 | { |
|---|
| 224 | checkFullArgument(name, value, bParsingFile); |
|---|
| [6417] | 225 | assert(shortcut.empty()); |
|---|
| [1663] | 226 | } |
|---|
| [6417] | 227 | else if (!shortcut.empty()) |
|---|
| [3280] | 228 | { |
|---|
| 229 | checkShortcut(shortcut, value, bParsingFile); |
|---|
| [6417] | 230 | assert(name.empty()); |
|---|
| [3280] | 231 | } |
|---|
| [1663] | 232 | } |
|---|
| [3280] | 233 | catch (const ArgumentException& ex) |
|---|
| [1664] | 234 | { |
|---|
| [3280] | 235 | COUT(0) << "Could not parse command line (including additional files): " << ex.what() << std::endl; |
|---|
| [6021] | 236 | COUT(0) << CommandLineParser::getUsageInformation() << std::endl; |
|---|
| [3280] | 237 | throw GeneralException(""); |
|---|
| [1664] | 238 | } |
|---|
| [1663] | 239 | } |
|---|
| 240 | |
|---|
| [1664] | 241 | /** |
|---|
| 242 | @brief |
|---|
| 243 | Parses an argument based on its full name. |
|---|
| 244 | @param name |
|---|
| 245 | Full name of the argument |
|---|
| 246 | @param value |
|---|
| 247 | String containing the value |
|---|
| [7401] | 248 | @param bParsingFile |
|---|
| 249 | Parsing a file or the command line itself |
|---|
| [1664] | 250 | */ |
|---|
| [6021] | 251 | void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile) |
|---|
| [1663] | 252 | { |
|---|
| [2087] | 253 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); |
|---|
| [1663] | 254 | if (it == cmdLineArgs_.end()) |
|---|
| 255 | ThrowException(Argument, "Command line argument '" + name + "' does not exist."); |
|---|
| 256 | |
|---|
| [3280] | 257 | it->second->parse(value, bParsingFile); |
|---|
| [1663] | 258 | } |
|---|
| 259 | |
|---|
| [1664] | 260 | /** |
|---|
| 261 | @brief |
|---|
| 262 | Parses an argument based on its shortcut. |
|---|
| 263 | @param shortcut |
|---|
| [7401] | 264 | Shortcut to the argument |
|---|
| [1664] | 265 | @param value |
|---|
| 266 | String containing the value |
|---|
| [7401] | 267 | @param bParsingFile |
|---|
| 268 | Parsing a file or the command line itself |
|---|
| [1664] | 269 | */ |
|---|
| [6021] | 270 | void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile) |
|---|
| [1663] | 271 | { |
|---|
| [2087] | 272 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); |
|---|
| [1663] | 273 | if (it == cmdLineArgsShortcut_.end()) |
|---|
| 274 | ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); |
|---|
| 275 | |
|---|
| [3280] | 276 | it->second->parse(value, bParsingFile); |
|---|
| [1663] | 277 | } |
|---|
| [1664] | 278 | |
|---|
| [6021] | 279 | std::string CommandLineParser::getUsageInformation() |
|---|
| [1664] | 280 | { |
|---|
| [6021] | 281 | CommandLineParser& inst = _getInstance(); |
|---|
| [3280] | 282 | std::ostringstream infoStr; |
|---|
| 283 | |
|---|
| 284 | // determine maximum name size |
|---|
| 285 | size_t maxNameSize = 0; |
|---|
| 286 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin(); |
|---|
| 287 | it != inst.cmdLineArgs_.end(); ++it) |
|---|
| [1664] | 288 | { |
|---|
| [3280] | 289 | maxNameSize = std::max(it->second->getName().size(), maxNameSize); |
|---|
| [1664] | 290 | } |
|---|
| [3280] | 291 | |
|---|
| [5929] | 292 | infoStr << std::endl; |
|---|
| [3280] | 293 | infoStr << "Usage: orxonox [options]" << std::endl; |
|---|
| 294 | infoStr << "Available options:" << std::endl; |
|---|
| 295 | |
|---|
| 296 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin(); |
|---|
| 297 | it != inst.cmdLineArgs_.end(); ++it) |
|---|
| 298 | { |
|---|
| [6417] | 299 | if (!it->second->getShortcut().empty()) |
|---|
| [3280] | 300 | infoStr << " [-" << it->second->getShortcut() << "] "; |
|---|
| 301 | else |
|---|
| 302 | infoStr << " "; |
|---|
| [6417] | 303 | infoStr << "--" << it->second->getName() << ' '; |
|---|
| [3280] | 304 | if (it->second->getValue().getType() != MT_Type::Bool) |
|---|
| 305 | infoStr << "ARG "; |
|---|
| 306 | else |
|---|
| 307 | infoStr << " "; |
|---|
| 308 | // fill with the necessary amount of blanks |
|---|
| 309 | infoStr << std::string(maxNameSize - it->second->getName().size(), ' '); |
|---|
| 310 | infoStr << ": " << it->second->getInformation(); |
|---|
| 311 | infoStr << std::endl; |
|---|
| 312 | } |
|---|
| 313 | return infoStr.str(); |
|---|
| [1664] | 314 | } |
|---|
| 315 | |
|---|
| [7401] | 316 | void CommandLineParser::generateDoc(std::ofstream& file) |
|---|
| 317 | { |
|---|
| 318 | file << "/** @page cmdargspage Command Line Arguments Reference" << endl; |
|---|
| 319 | file << " @verbatim"; /*no endl*/ |
|---|
| 320 | file << getUsageInformation(); /*no endl*/ |
|---|
| 321 | file << " @endverbatim" << endl; |
|---|
| 322 | file << "*/" << endl; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| [2087] | 325 | /** |
|---|
| 326 | @brief |
|---|
| 327 | Retrieves a CommandLineArgument. |
|---|
| 328 | The method throws an exception if 'name' was not found or the value could not be converted. |
|---|
| 329 | @note |
|---|
| [7401] | 330 | You should of course not call this method before the command line has been parsed. |
|---|
| [2087] | 331 | */ |
|---|
| [6021] | 332 | const CommandLineArgument* CommandLineParser::getArgument(const std::string& name) |
|---|
| [2087] | 333 | { |
|---|
| 334 | std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); |
|---|
| 335 | if (it == _getInstance().cmdLineArgs_.end()) |
|---|
| 336 | { |
|---|
| 337 | ThrowException(Argument, "Could find command line argument '" + name + "'."); |
|---|
| 338 | } |
|---|
| 339 | else |
|---|
| 340 | { |
|---|
| 341 | return it->second; |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| [2103] | 344 | |
|---|
| 345 | /** |
|---|
| 346 | @brief |
|---|
| [3280] | 347 | Parses only the command line for CommandLineArguments. |
|---|
| [2103] | 348 | */ |
|---|
| [6021] | 349 | void CommandLineParser::_parseCommandLine(const std::string& cmdLine) |
|---|
| [2103] | 350 | { |
|---|
| 351 | std::vector<std::string> args; |
|---|
| [7284] | 352 | SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false); |
|---|
| [3323] | 353 | for (unsigned i = 0; i < tokens.size(); ++i) |
|---|
| 354 | args.push_back(tokens[i]); |
|---|
| [3280] | 355 | this->_parse(args, false); |
|---|
| 356 | } |
|---|
| [2103] | 357 | |
|---|
| [3280] | 358 | /** |
|---|
| 359 | @brief |
|---|
| 360 | Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments. |
|---|
| 361 | */ |
|---|
| [6021] | 362 | void CommandLineParser::_parseFile() |
|---|
| [3280] | 363 | { |
|---|
| [6417] | 364 | const std::string& filename = CommandLineParser::getValue("optionsFile").getString(); |
|---|
| [2710] | 365 | |
|---|
| [2103] | 366 | // look for additional arguments in given file or start.ini as default |
|---|
| 367 | // They will not overwrite the arguments given directly |
|---|
| 368 | std::ifstream file; |
|---|
| [5929] | 369 | file.open((PathConfig::getConfigPathString() + filename).c_str()); |
|---|
| [3280] | 370 | std::vector<std::string> args; |
|---|
| [2103] | 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 | //{ |
|---|
| [7284] | 380 | SubString tokens(line, " ", " ", false, '\\', true, '"', true, '\0', '\0', false, '#'); |
|---|
| [2103] | 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 | |
|---|
| [3280] | 390 | _parse(args, true); |
|---|
| [2103] | 391 | } |
|---|
| [1663] | 392 | } |
|---|