[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 | |
---|
| 29 | #include "CommandLine.h" |
---|
| 30 | |
---|
[2710] | 31 | #include <boost/filesystem.hpp> |
---|
[3196] | 32 | |
---|
| 33 | #include "util/Convert.h" |
---|
| 34 | #include "util/Debug.h" |
---|
| 35 | #include "util/Exception.h" |
---|
[3250] | 36 | #include "util/StringUtils.h" |
---|
[2103] | 37 | #include "util/SubString.h" |
---|
[2710] | 38 | #include "Core.h" |
---|
[1690] | 39 | |
---|
[1663] | 40 | namespace orxonox |
---|
| 41 | { |
---|
[3246] | 42 | SetCommandLineOnlyArgument(optionsFile, "start.ini").shortcut("o"); |
---|
[2103] | 43 | |
---|
[1664] | 44 | /** |
---|
| 45 | @brief |
---|
[2087] | 46 | Parses a value string for a command line argument. |
---|
| 47 | It simply uses convertValue(Output, Input) to do that. |
---|
| 48 | Bools are treated specially. That is necessary |
---|
| 49 | so that you can have simple command line switches. |
---|
| 50 | */ |
---|
[3246] | 51 | void CommandLineArgument::parse(const std::string& value, bool bParsingFile) |
---|
[2087] | 52 | { |
---|
[3246] | 53 | if (bParsingFile && this->bCommandLineOnly_) |
---|
| 54 | ThrowException(Argument, "Command line argument '" + getName() + "' is not allowed in files."); |
---|
[2087] | 55 | if (value_.getType() == MT_bool) |
---|
| 56 | { |
---|
| 57 | // simulate command line switch |
---|
| 58 | bool temp; |
---|
| 59 | if (convertValue(&temp, value)) |
---|
| 60 | { |
---|
| 61 | this->bHasDefaultValue_ = false; |
---|
| 62 | this->value_ = temp; |
---|
| 63 | } |
---|
| 64 | else if (value == "") |
---|
| 65 | { |
---|
| 66 | this->bHasDefaultValue_ = false; |
---|
| 67 | this->value_ = true; |
---|
| 68 | } |
---|
| 69 | else |
---|
| 70 | ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); |
---|
| 71 | } |
---|
| 72 | else |
---|
| 73 | { |
---|
| 74 | if (!value_.setValue(value)) |
---|
| 75 | { |
---|
| 76 | value_.setValue(defaultValue_); |
---|
| 77 | ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | this->bHasDefaultValue_ = false; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | @brief |
---|
[1664] | 87 | Destructor destroys all CommandLineArguments with it. |
---|
| 88 | */ |
---|
[1663] | 89 | CommandLine::~CommandLine() |
---|
| 90 | { |
---|
[2662] | 91 | CommandLine::destroyAllArguments(); |
---|
[1663] | 92 | } |
---|
[1664] | 93 | |
---|
| 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Returns a unique instance (Meyers Singleton). |
---|
| 97 | */ |
---|
[1663] | 98 | CommandLine& CommandLine::_getInstance() |
---|
| 99 | { |
---|
| 100 | static CommandLine instance; |
---|
| 101 | return instance; |
---|
| 102 | } |
---|
| 103 | |
---|
[1664] | 104 | /** |
---|
| 105 | @brief |
---|
[2662] | 106 | Destroys all command line arguments. This should be called at the end |
---|
| 107 | of main. Do not use before that. |
---|
| 108 | */ |
---|
| 109 | void CommandLine::destroyAllArguments() |
---|
| 110 | { |
---|
| 111 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.begin(); |
---|
| 112 | it != _getInstance().cmdLineArgs_.end(); ++it) |
---|
| 113 | delete it->second; |
---|
| 114 | _getInstance().cmdLineArgs_.clear(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | @brief |
---|
[1664] | 119 | Reads the command line parses the values of each argument. |
---|
| 120 | It is then stored in the corresponding CommandLineArgument. |
---|
| 121 | @note |
---|
| 122 | The reason that you have to provide the string to be parsed as |
---|
| 123 | space separted list is because of argc and argv. If you only have |
---|
| 124 | a whole string, simply use getAllStrings() of SubString. |
---|
| 125 | @param arguments |
---|
| 126 | Vector of space separated strings. |
---|
| 127 | */ |
---|
[3246] | 128 | void CommandLine::_parse(const std::vector<std::string>& arguments, bool bParsingFile) |
---|
[1663] | 129 | { |
---|
[3246] | 130 | try |
---|
[1663] | 131 | { |
---|
[3246] | 132 | // why this? See bFirstTimeParse_ declaration. |
---|
| 133 | if (bFirstTimeParse_) |
---|
[1663] | 134 | { |
---|
[3246] | 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) |
---|
| 138 | { |
---|
| 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; |
---|
[1663] | 145 | } |
---|
| 146 | |
---|
[3246] | 147 | std::string name; |
---|
| 148 | std::string shortcut; |
---|
| 149 | std::string value; |
---|
| 150 | for (unsigned int i = 0; i < arguments.size(); ++i) |
---|
[1663] | 151 | { |
---|
[3246] | 152 | if (arguments[i].size() != 0) |
---|
[1663] | 153 | { |
---|
[3246] | 154 | // sure not "" |
---|
| 155 | if (arguments[i][0] == '-') |
---|
[1663] | 156 | { |
---|
[3246] | 157 | // start with "-" |
---|
| 158 | if (arguments[i].size() == 1) |
---|
[1664] | 159 | { |
---|
[3246] | 160 | // argument[i] is "-", probably a minus sign |
---|
| 161 | value += "- "; |
---|
[1664] | 162 | } |
---|
[3246] | 163 | else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) |
---|
[1664] | 164 | { |
---|
[3246] | 165 | // negative number as a value |
---|
| 166 | value += arguments[i] + " "; |
---|
[1664] | 167 | } |
---|
[3246] | 168 | else |
---|
| 169 | { |
---|
| 170 | // can be shortcut or full name argument |
---|
[1664] | 171 | |
---|
[3246] | 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 = ""; |
---|
[1664] | 200 | } |
---|
[3246] | 201 | } |
---|
| 202 | else |
---|
| 203 | { |
---|
| 204 | // value string |
---|
| 205 | |
---|
| 206 | if (name == "" && shortcut == "") |
---|
[1664] | 207 | { |
---|
[3246] | 208 | ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); |
---|
[1664] | 209 | } |
---|
| 210 | |
---|
[3246] | 211 | // Concatenate strings as long as there's no new argument by "-" or "--" |
---|
| 212 | value += arguments[i] + ' '; |
---|
[1663] | 213 | } |
---|
| 214 | } |
---|
[3246] | 215 | } |
---|
[1664] | 216 | |
---|
[3246] | 217 | // parse last argument |
---|
| 218 | value = removeTrailingWhitespaces(value); |
---|
| 219 | if (name != "") |
---|
| 220 | { |
---|
| 221 | checkFullArgument(name, value, bParsingFile); |
---|
| 222 | assert(shortcut == ""); |
---|
[1663] | 223 | } |
---|
[3246] | 224 | else if (shortcut != "") |
---|
| 225 | { |
---|
| 226 | checkShortcut(shortcut, value, bParsingFile); |
---|
| 227 | assert(name == ""); |
---|
| 228 | } |
---|
[1663] | 229 | } |
---|
[3246] | 230 | catch (const ArgumentException& ex) |
---|
[1664] | 231 | { |
---|
[3246] | 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(""); |
---|
[1664] | 235 | } |
---|
[1663] | 236 | } |
---|
| 237 | |
---|
[1664] | 238 | /** |
---|
| 239 | @brief |
---|
| 240 | Parses an argument based on its full name. |
---|
| 241 | @param name |
---|
| 242 | Full name of the argument |
---|
| 243 | @param value |
---|
| 244 | String containing the value |
---|
| 245 | */ |
---|
[3246] | 246 | void CommandLine::checkFullArgument(const std::string& name, const std::string& value, bool bParsingFile) |
---|
[1663] | 247 | { |
---|
[2087] | 248 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); |
---|
[1663] | 249 | if (it == cmdLineArgs_.end()) |
---|
| 250 | ThrowException(Argument, "Command line argument '" + name + "' does not exist."); |
---|
| 251 | |
---|
[3246] | 252 | it->second->parse(value, bParsingFile); |
---|
[1663] | 253 | } |
---|
| 254 | |
---|
[1664] | 255 | /** |
---|
| 256 | @brief |
---|
| 257 | Parses an argument based on its shortcut. |
---|
| 258 | @param shortcut |
---|
| 259 | Shotcut to the argument |
---|
| 260 | @param value |
---|
| 261 | String containing the value |
---|
| 262 | */ |
---|
[3246] | 263 | void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value, bool bParsingFile) |
---|
[1663] | 264 | { |
---|
[2087] | 265 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); |
---|
[1663] | 266 | if (it == cmdLineArgsShortcut_.end()) |
---|
| 267 | ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); |
---|
| 268 | |
---|
[3246] | 269 | it->second->parse(value, bParsingFile); |
---|
[1663] | 270 | } |
---|
[1664] | 271 | |
---|
| 272 | std::string CommandLine::getUsageInformation() |
---|
| 273 | { |
---|
| 274 | CommandLine* inst = &_getInstance(); |
---|
| 275 | std::string infoStr; |
---|
[2087] | 276 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin(); |
---|
[1664] | 277 | it != inst->cmdLineArgs_.end(); ++it) |
---|
| 278 | { |
---|
| 279 | infoStr += "[--" + it->second->getName() + " " + it->second->getInformation() + "] "; |
---|
| 280 | } |
---|
| 281 | return infoStr; |
---|
| 282 | } |
---|
| 283 | |
---|
[2087] | 284 | /** |
---|
| 285 | @brief |
---|
| 286 | Retrieves a CommandLineArgument. |
---|
| 287 | The method throws an exception if 'name' was not found or the value could not be converted. |
---|
| 288 | @note |
---|
| 289 | You shold of course not call this method before the command line has been parsed. |
---|
| 290 | */ |
---|
| 291 | const CommandLineArgument* CommandLine::getArgument(const std::string& name) |
---|
| 292 | { |
---|
| 293 | std::map<std::string, CommandLineArgument*>::const_iterator it = _getInstance().cmdLineArgs_.find(name); |
---|
| 294 | if (it == _getInstance().cmdLineArgs_.end()) |
---|
| 295 | { |
---|
| 296 | ThrowException(Argument, "Could find command line argument '" + name + "'."); |
---|
| 297 | } |
---|
| 298 | else |
---|
| 299 | { |
---|
| 300 | return it->second; |
---|
| 301 | } |
---|
| 302 | } |
---|
[2103] | 303 | |
---|
| 304 | /** |
---|
| 305 | @brief |
---|
[3246] | 306 | Parses only the command line for CommandLineArguments. |
---|
[2103] | 307 | */ |
---|
[3246] | 308 | void CommandLine::_parseCommandLine(int argc, char** argv) |
---|
[2103] | 309 | { |
---|
| 310 | std::vector<std::string> args; |
---|
| 311 | for (int i = 1; i < argc; ++i) |
---|
| 312 | args.push_back(argv[i]); |
---|
[3246] | 313 | this->_parse(args, false); |
---|
| 314 | } |
---|
[2103] | 315 | |
---|
[3246] | 316 | /** |
---|
| 317 | @brief |
---|
| 318 | Parses start.ini (or the file specified with --optionsFile) for CommandLineArguments. |
---|
| 319 | */ |
---|
| 320 | void CommandLine::_parseFile() |
---|
| 321 | { |
---|
[2710] | 322 | std::string filename = CommandLine::getValue("optionsFile").getString(); |
---|
| 323 | boost::filesystem::path filepath(Core::getConfigPath() / filename); |
---|
| 324 | |
---|
[2103] | 325 | // look for additional arguments in given file or start.ini as default |
---|
| 326 | // They will not overwrite the arguments given directly |
---|
| 327 | std::ifstream file; |
---|
[2759] | 328 | file.open(filepath.string().c_str()); |
---|
[3246] | 329 | std::vector<std::string> args; |
---|
[2103] | 330 | if (file) |
---|
| 331 | { |
---|
| 332 | while (!file.eof()) |
---|
| 333 | { |
---|
| 334 | std::string line; |
---|
| 335 | std::getline(file, line); |
---|
| 336 | line = removeTrailingWhitespaces(line); |
---|
| 337 | //if (!(line[0] == '#' || line[0] == '%')) |
---|
| 338 | //{ |
---|
| 339 | SubString tokens(line, " ", " ", false, 92, false, 34, false, 40, 41, false, '#'); |
---|
| 340 | for (unsigned i = 0; i < tokens.size(); ++i) |
---|
| 341 | if (tokens[i][0] != '#') |
---|
| 342 | args.push_back(tokens[i]); |
---|
| 343 | //args.insert(args.end(), tokens.getAllStrings().begin(), tokens.getAllStrings().end()); |
---|
| 344 | //} |
---|
| 345 | } |
---|
| 346 | file.close(); |
---|
| 347 | } |
---|
| 348 | |
---|
[3246] | 349 | _parse(args, true); |
---|
[2103] | 350 | } |
---|
[1663] | 351 | } |
---|