[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" |
---|
[8858] | 35 | #include "util/Output.h" |
---|
[3196] | 36 | #include "util/Exception.h" |
---|
[3280] | 37 | #include "util/StringUtils.h" |
---|
[2103] | 38 | #include "util/SubString.h" |
---|
[1690] | 39 | |
---|
[1663] | 40 | namespace orxonox |
---|
| 41 | { |
---|
[10542] | 42 | CommandLineParser* CommandLineParser::singletonPtr_s = 0; |
---|
| 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 | */ |
---|
[8729] | 51 | void CommandLineArgument::parse(const std::string& value) |
---|
[2087] | 52 | { |
---|
[9550] | 53 | if (value_.isType<bool>()) |
---|
[2087] | 54 | { |
---|
| 55 | // simulate command line switch |
---|
| 56 | bool temp; |
---|
| 57 | if (convertValue(&temp, value)) |
---|
| 58 | { |
---|
| 59 | this->bHasDefaultValue_ = false; |
---|
| 60 | this->value_ = temp; |
---|
| 61 | } |
---|
[6417] | 62 | else if (value.empty()) |
---|
[2087] | 63 | { |
---|
| 64 | this->bHasDefaultValue_ = false; |
---|
| 65 | this->value_ = true; |
---|
| 66 | } |
---|
| 67 | else |
---|
| 68 | ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); |
---|
| 69 | } |
---|
| 70 | else |
---|
| 71 | { |
---|
[9550] | 72 | if (!value_.set(value)) |
---|
[2087] | 73 | { |
---|
[9550] | 74 | value_.set(defaultValue_); |
---|
[2087] | 75 | ThrowException(Argument, "Could not read command line argument '" + getName() + "'."); |
---|
| 76 | } |
---|
| 77 | else |
---|
| 78 | this->bHasDefaultValue_ = false; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | @brief |
---|
[1664] | 85 | Destructor destroys all CommandLineArguments with it. |
---|
| 86 | */ |
---|
[6021] | 87 | CommandLineParser::~CommandLineParser() |
---|
[1663] | 88 | { |
---|
| 89 | } |
---|
[1664] | 90 | |
---|
[8729] | 91 | /** Parses the command line string for arguments and stores these. |
---|
[1664] | 92 | @note |
---|
| 93 | The reason that you have to provide the string to be parsed as |
---|
[8729] | 94 | space separated list is because of argc and argv. If you only have |
---|
[1664] | 95 | a whole string, simply use getAllStrings() of SubString. |
---|
[8729] | 96 | @param cmdLine |
---|
| 97 | Command line string WITHOUT the execution path. |
---|
[1664] | 98 | */ |
---|
[8729] | 99 | void CommandLineParser::_parse(const std::string& cmdLine) |
---|
[1663] | 100 | { |
---|
[8729] | 101 | std::vector<std::string> arguments; |
---|
| 102 | SubString tokens(cmdLine, " ", " ", false, '\\', true, '"', true, '\0', '\0', false); |
---|
| 103 | for (unsigned i = 0; i < tokens.size(); ++i) |
---|
| 104 | arguments.push_back(tokens[i]); |
---|
| 105 | |
---|
[3280] | 106 | try |
---|
[1663] | 107 | { |
---|
[3280] | 108 | // why this? See bFirstTimeParse_ declaration. |
---|
| 109 | if (bFirstTimeParse_) |
---|
[1663] | 110 | { |
---|
[3280] | 111 | // first shove all the shortcuts in a map |
---|
| 112 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); |
---|
| 113 | it != cmdLineArgs_.end(); ++it) |
---|
| 114 | { |
---|
| 115 | OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(), |
---|
| 116 | "Cannot have two command line shortcut with the same name."); |
---|
[6417] | 117 | if (!it->second->getShortcut().empty()) |
---|
[3280] | 118 | cmdLineArgsShortcut_[it->second->getShortcut()] = it->second; |
---|
| 119 | } |
---|
| 120 | bFirstTimeParse_ = false; |
---|
[1663] | 121 | } |
---|
| 122 | |
---|
[3280] | 123 | std::string name; |
---|
| 124 | std::string shortcut; |
---|
| 125 | std::string value; |
---|
| 126 | for (unsigned int i = 0; i < arguments.size(); ++i) |
---|
[1663] | 127 | { |
---|
[3280] | 128 | if (arguments[i].size() != 0) |
---|
[1663] | 129 | { |
---|
[3280] | 130 | // sure not "" |
---|
| 131 | if (arguments[i][0] == '-') |
---|
[1663] | 132 | { |
---|
[3280] | 133 | // start with "-" |
---|
| 134 | if (arguments[i].size() == 1) |
---|
[1664] | 135 | { |
---|
[3280] | 136 | // argument[i] is "-", probably a minus sign |
---|
| 137 | value += "- "; |
---|
[1664] | 138 | } |
---|
[3280] | 139 | else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) |
---|
[1664] | 140 | { |
---|
[3280] | 141 | // negative number as a value |
---|
[6417] | 142 | value += arguments[i] + ' '; |
---|
[1664] | 143 | } |
---|
[3280] | 144 | else |
---|
| 145 | { |
---|
| 146 | // can be shortcut or full name argument |
---|
[1664] | 147 | |
---|
[3280] | 148 | // save old data first |
---|
| 149 | value = removeTrailingWhitespaces(value); |
---|
[6417] | 150 | if (!name.empty()) |
---|
[3280] | 151 | { |
---|
[8729] | 152 | checkFullArgument(name, value); |
---|
[6417] | 153 | name.clear(); |
---|
| 154 | assert(shortcut.empty()); |
---|
[3280] | 155 | } |
---|
[6417] | 156 | else if (!shortcut.empty()) |
---|
[3280] | 157 | { |
---|
[8729] | 158 | checkShortcut(shortcut, value); |
---|
[6417] | 159 | shortcut.clear(); |
---|
| 160 | assert(name.empty()); |
---|
[3280] | 161 | } |
---|
| 162 | |
---|
| 163 | if (arguments[i][1] == '-') |
---|
| 164 | { |
---|
| 165 | // full name argument with "--name" |
---|
| 166 | name = arguments[i].substr(2); |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | // shortcut with "-s" |
---|
| 171 | shortcut = arguments[i].substr(1); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | // reset value string |
---|
[6417] | 175 | value.clear(); |
---|
[1664] | 176 | } |
---|
[3280] | 177 | } |
---|
| 178 | else |
---|
| 179 | { |
---|
| 180 | // value string |
---|
| 181 | |
---|
[6417] | 182 | if (name.empty() && shortcut.empty()) |
---|
[1664] | 183 | { |
---|
[3280] | 184 | ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); |
---|
[1664] | 185 | } |
---|
| 186 | |
---|
[3280] | 187 | // Concatenate strings as long as there's no new argument by "-" or "--" |
---|
| 188 | value += arguments[i] + ' '; |
---|
[1663] | 189 | } |
---|
| 190 | } |
---|
[3280] | 191 | } |
---|
[1664] | 192 | |
---|
[3280] | 193 | // parse last argument |
---|
| 194 | value = removeTrailingWhitespaces(value); |
---|
[6417] | 195 | if (!name.empty()) |
---|
[3280] | 196 | { |
---|
[8729] | 197 | checkFullArgument(name, value); |
---|
[6417] | 198 | assert(shortcut.empty()); |
---|
[1663] | 199 | } |
---|
[6417] | 200 | else if (!shortcut.empty()) |
---|
[3280] | 201 | { |
---|
[8729] | 202 | checkShortcut(shortcut, value); |
---|
[6417] | 203 | assert(name.empty()); |
---|
[3280] | 204 | } |
---|
[1663] | 205 | } |
---|
[3280] | 206 | catch (const ArgumentException& ex) |
---|
[1664] | 207 | { |
---|
[8858] | 208 | orxout(user_error) << "Could not parse command line: " << ex.what() << endl; |
---|
| 209 | orxout(user_error) << CommandLineParser::getUsageInformation() << endl; |
---|
[3280] | 210 | throw GeneralException(""); |
---|
[1664] | 211 | } |
---|
[1663] | 212 | } |
---|
| 213 | |
---|
[1664] | 214 | /** |
---|
| 215 | @brief |
---|
| 216 | Parses an argument based on its full name. |
---|
| 217 | @param name |
---|
| 218 | Full name of the argument |
---|
| 219 | @param value |
---|
| 220 | String containing the value |
---|
[7401] | 221 | @param bParsingFile |
---|
| 222 | Parsing a file or the command line itself |
---|
[1664] | 223 | */ |
---|
[8729] | 224 | void CommandLineParser::checkFullArgument(const std::string& name, const std::string& value) |
---|
[1663] | 225 | { |
---|
[2087] | 226 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); |
---|
[1663] | 227 | if (it == cmdLineArgs_.end()) |
---|
| 228 | ThrowException(Argument, "Command line argument '" + name + "' does not exist."); |
---|
| 229 | |
---|
[8729] | 230 | it->second->parse(value); |
---|
[1663] | 231 | } |
---|
| 232 | |
---|
[1664] | 233 | /** |
---|
| 234 | @brief |
---|
| 235 | Parses an argument based on its shortcut. |
---|
| 236 | @param shortcut |
---|
[7401] | 237 | Shortcut to the argument |
---|
[1664] | 238 | @param value |
---|
| 239 | String containing the value |
---|
[7401] | 240 | @param bParsingFile |
---|
| 241 | Parsing a file or the command line itself |
---|
[1664] | 242 | */ |
---|
[8729] | 243 | void CommandLineParser::checkShortcut(const std::string& shortcut, const std::string& value) |
---|
[1663] | 244 | { |
---|
[2087] | 245 | std::map<std::string, CommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); |
---|
[1663] | 246 | if (it == cmdLineArgsShortcut_.end()) |
---|
| 247 | ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); |
---|
| 248 | |
---|
[8729] | 249 | it->second->parse(value); |
---|
[1663] | 250 | } |
---|
[1664] | 251 | |
---|
[6021] | 252 | std::string CommandLineParser::getUsageInformation() |
---|
[1664] | 253 | { |
---|
[10542] | 254 | CommandLineParser& inst = getInstance(); |
---|
[3280] | 255 | std::ostringstream infoStr; |
---|
| 256 | |
---|
| 257 | // determine maximum name size |
---|
| 258 | size_t maxNameSize = 0; |
---|
| 259 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin(); |
---|
| 260 | it != inst.cmdLineArgs_.end(); ++it) |
---|
[1664] | 261 | { |
---|
[3280] | 262 | maxNameSize = std::max(it->second->getName().size(), maxNameSize); |
---|
[1664] | 263 | } |
---|
[3280] | 264 | |
---|
[8858] | 265 | infoStr << endl; |
---|
| 266 | infoStr << "Usage: orxonox [options]" << endl; |
---|
| 267 | infoStr << "Available options:" << endl; |
---|
[3280] | 268 | |
---|
| 269 | for (std::map<std::string, CommandLineArgument*>::const_iterator it = inst.cmdLineArgs_.begin(); |
---|
| 270 | it != inst.cmdLineArgs_.end(); ++it) |
---|
| 271 | { |
---|
[6417] | 272 | if (!it->second->getShortcut().empty()) |
---|
[3280] | 273 | infoStr << " [-" << it->second->getShortcut() << "] "; |
---|
| 274 | else |
---|
| 275 | infoStr << " "; |
---|
[6417] | 276 | infoStr << "--" << it->second->getName() << ' '; |
---|
[9550] | 277 | if (it->second->getValue().isType<bool>()) |
---|
| 278 | infoStr << " "; |
---|
| 279 | else |
---|
[3280] | 280 | infoStr << "ARG "; |
---|
| 281 | // fill with the necessary amount of blanks |
---|
| 282 | infoStr << std::string(maxNameSize - it->second->getName().size(), ' '); |
---|
| 283 | infoStr << ": " << it->second->getInformation(); |
---|
[8858] | 284 | infoStr << endl; |
---|
[3280] | 285 | } |
---|
| 286 | return infoStr.str(); |
---|
[1664] | 287 | } |
---|
| 288 | |
---|
[7401] | 289 | void CommandLineParser::generateDoc(std::ofstream& file) |
---|
| 290 | { |
---|
| 291 | file << "/** @page cmdargspage Command Line Arguments Reference" << endl; |
---|
| 292 | file << " @verbatim"; /*no endl*/ |
---|
| 293 | file << getUsageInformation(); /*no endl*/ |
---|
| 294 | file << " @endverbatim" << endl; |
---|
| 295 | file << "*/" << endl; |
---|
| 296 | } |
---|
| 297 | |
---|
[2087] | 298 | /** |
---|
| 299 | @brief |
---|
| 300 | Retrieves a CommandLineArgument. |
---|
| 301 | The method throws an exception if 'name' was not found or the value could not be converted. |
---|
| 302 | @note |
---|
[7401] | 303 | You should of course not call this method before the command line has been parsed. |
---|
[2087] | 304 | */ |
---|
[6021] | 305 | const CommandLineArgument* CommandLineParser::getArgument(const std::string& name) |
---|
[2087] | 306 | { |
---|
[10542] | 307 | std::map<std::string, CommandLineArgument*>::const_iterator it = getInstance().cmdLineArgs_.find(name); |
---|
| 308 | if (it == getInstance().cmdLineArgs_.end()) |
---|
[2087] | 309 | { |
---|
| 310 | ThrowException(Argument, "Could find command line argument '" + name + "'."); |
---|
| 311 | } |
---|
| 312 | else |
---|
| 313 | { |
---|
| 314 | return it->second; |
---|
| 315 | } |
---|
| 316 | } |
---|
[10345] | 317 | |
---|
| 318 | /** |
---|
| 319 | @brief |
---|
| 320 | Adds a new CommandLineArgument to the internal map. |
---|
| 321 | Note that only such arguments are actually valid. |
---|
| 322 | */ |
---|
| 323 | void CommandLineParser::addArgument(CommandLineArgument* argument) |
---|
| 324 | { |
---|
[10542] | 325 | OrxAssert(!getInstance().existsArgument(argument->getName()), |
---|
[10345] | 326 | "Cannot add a command line argument with name '" + argument->getName() + "' twice."); |
---|
| 327 | OrxAssert(!argument->getDefaultValue().isType<bool>() || argument->getDefaultValue().get<bool>() != true, |
---|
| 328 | "Boolean command line arguments with positive default values are not supported." << endl |
---|
| 329 | << "Please use SetCommandLineSwitch and adjust your argument: " << argument->getName()); |
---|
| 330 | |
---|
[10542] | 331 | getInstance().cmdLineArgs_[argument->getName()] = argument; |
---|
[10345] | 332 | } |
---|
[10404] | 333 | |
---|
| 334 | /** |
---|
| 335 | * @brief Removes a CommandLineArgument from the internal map. |
---|
| 336 | */ |
---|
| 337 | void CommandLineParser::removeArgument(CommandLineArgument* argument) |
---|
| 338 | { |
---|
[10542] | 339 | getInstance().cmdLineArgs_.erase(argument->getName()); |
---|
[10404] | 340 | } |
---|
[1663] | 341 | } |
---|