[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 | |
---|
[1690] | 31 | #include "util/String.h" |
---|
| 32 | |
---|
[1663] | 33 | namespace orxonox |
---|
| 34 | { |
---|
[1664] | 35 | /** |
---|
| 36 | @brief |
---|
| 37 | Destructor destroys all CommandLineArguments with it. |
---|
| 38 | */ |
---|
[1663] | 39 | CommandLine::~CommandLine() |
---|
| 40 | { |
---|
| 41 | for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); |
---|
| 42 | it != cmdLineArgs_.end(); ++it) |
---|
| 43 | { |
---|
| 44 | delete it->second; |
---|
| 45 | } |
---|
| 46 | } |
---|
[1664] | 47 | |
---|
| 48 | /** |
---|
| 49 | @brief |
---|
| 50 | Returns a unique instance (Meyers Singleton). |
---|
| 51 | */ |
---|
[1663] | 52 | CommandLine& CommandLine::_getInstance() |
---|
| 53 | { |
---|
| 54 | static CommandLine instance; |
---|
| 55 | return instance; |
---|
| 56 | } |
---|
| 57 | |
---|
[1664] | 58 | /** |
---|
| 59 | @brief |
---|
| 60 | Reads the command line parses the values of each argument. |
---|
| 61 | It is then stored in the corresponding CommandLineArgument. |
---|
| 62 | @note |
---|
| 63 | The reason that you have to provide the string to be parsed as |
---|
| 64 | space separted list is because of argc and argv. If you only have |
---|
| 65 | a whole string, simply use getAllStrings() of SubString. |
---|
| 66 | @param arguments |
---|
| 67 | Vector of space separated strings. |
---|
| 68 | */ |
---|
| 69 | void CommandLine::_parse(const std::vector<std::string>& arguments) |
---|
[1663] | 70 | { |
---|
[1664] | 71 | // why this? See bFirstTimeParse_ declaration. |
---|
[1663] | 72 | if (bFirstTimeParse_) |
---|
| 73 | { |
---|
| 74 | // first shove all the shortcuts in a map |
---|
| 75 | for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); |
---|
| 76 | it != cmdLineArgs_.end(); ++it) |
---|
| 77 | { |
---|
| 78 | OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(), |
---|
| 79 | "Cannot have two command line shortcut with the same name."); |
---|
| 80 | if (it->second->getShortcut() != "") |
---|
| 81 | cmdLineArgsShortcut_[it->second->getShortcut()] = it->second; |
---|
| 82 | } |
---|
| 83 | bFirstTimeParse_ = false; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | std::string name; |
---|
| 87 | std::string shortcut; |
---|
[1664] | 88 | std::string value; |
---|
| 89 | for (unsigned int i = 0; i < arguments.size(); ++i) |
---|
[1663] | 90 | { |
---|
[1664] | 91 | if (arguments[i].size() != 0) |
---|
[1663] | 92 | { |
---|
[1664] | 93 | // sure not "" |
---|
| 94 | if (arguments[i][0] == '-') |
---|
[1663] | 95 | { |
---|
[1664] | 96 | // start with "-" |
---|
| 97 | if (arguments[i].size() == 1) |
---|
[1663] | 98 | { |
---|
[1664] | 99 | // argument[i] is "-", probably a minus sign |
---|
| 100 | value += "- "; |
---|
[1663] | 101 | } |
---|
[1664] | 102 | else if (arguments[i][1] <= 57 && arguments[i][1] >= 48) |
---|
[1663] | 103 | { |
---|
[1664] | 104 | // negative number as a value |
---|
| 105 | value += arguments[i] + " "; |
---|
[1663] | 106 | } |
---|
| 107 | else |
---|
| 108 | { |
---|
[1664] | 109 | // can be shortcut or full name argument |
---|
| 110 | |
---|
| 111 | // save old data first |
---|
[1690] | 112 | value = removeTrailingWhitespaces(value); |
---|
[1664] | 113 | if (name != "") |
---|
| 114 | { |
---|
| 115 | checkFullArgument(name, value); |
---|
| 116 | name = ""; |
---|
| 117 | assert(shortcut == ""); |
---|
| 118 | } |
---|
| 119 | else if (shortcut != "") |
---|
| 120 | { |
---|
| 121 | checkShortcut(shortcut, value); |
---|
| 122 | shortcut = ""; |
---|
| 123 | assert(name == ""); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | if (arguments[i][1] == '-') |
---|
| 127 | { |
---|
| 128 | // full name argument with "--name" |
---|
| 129 | name = arguments[i].substr(2); |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | // shortcut with "-s" |
---|
| 134 | shortcut = arguments[i].substr(1); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // reset value string |
---|
| 138 | value = ""; |
---|
[1663] | 139 | } |
---|
| 140 | } |
---|
[1664] | 141 | else |
---|
[1663] | 142 | { |
---|
[1664] | 143 | // value string |
---|
| 144 | |
---|
| 145 | if (name == "" && shortcut == "") |
---|
| 146 | { |
---|
| 147 | ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | // Concatenate strings as long as there's no new argument by "-" or "--" |
---|
| 151 | value += arguments[i] + ' '; |
---|
[1663] | 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|
[1664] | 155 | |
---|
| 156 | // parse last argument |
---|
[1690] | 157 | value = removeTrailingWhitespaces(value); |
---|
[1664] | 158 | if (name != "") |
---|
| 159 | { |
---|
| 160 | checkFullArgument(name, value); |
---|
| 161 | assert(shortcut == ""); |
---|
| 162 | } |
---|
| 163 | else if (shortcut != "") |
---|
| 164 | { |
---|
| 165 | checkShortcut(shortcut, value); |
---|
| 166 | assert(name == ""); |
---|
| 167 | } |
---|
[1663] | 168 | } |
---|
| 169 | |
---|
[1664] | 170 | /** |
---|
| 171 | @brief |
---|
| 172 | Parses an argument based on its full name. |
---|
| 173 | @param name |
---|
| 174 | Full name of the argument |
---|
| 175 | @param value |
---|
| 176 | String containing the value |
---|
| 177 | */ |
---|
[1663] | 178 | void CommandLine::checkFullArgument(const std::string& name, const std::string& value) |
---|
| 179 | { |
---|
| 180 | std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); |
---|
| 181 | if (it == cmdLineArgs_.end()) |
---|
| 182 | ThrowException(Argument, "Command line argument '" + name + "' does not exist."); |
---|
| 183 | |
---|
| 184 | it->second->parse(value); |
---|
| 185 | } |
---|
| 186 | |
---|
[1664] | 187 | /** |
---|
| 188 | @brief |
---|
| 189 | Parses an argument based on its shortcut. |
---|
| 190 | @param shortcut |
---|
| 191 | Shotcut to the argument |
---|
| 192 | @param value |
---|
| 193 | String containing the value |
---|
| 194 | */ |
---|
[1663] | 195 | void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value) |
---|
| 196 | { |
---|
| 197 | std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); |
---|
| 198 | if (it == cmdLineArgsShortcut_.end()) |
---|
| 199 | ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); |
---|
| 200 | |
---|
| 201 | it->second->parse(value); |
---|
| 202 | } |
---|
[1664] | 203 | |
---|
| 204 | std::string CommandLine::getUsageInformation() |
---|
| 205 | { |
---|
| 206 | CommandLine* inst = &_getInstance(); |
---|
| 207 | std::string infoStr; |
---|
| 208 | for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin(); |
---|
| 209 | it != inst->cmdLineArgs_.end(); ++it) |
---|
| 210 | { |
---|
| 211 | infoStr += "[--" + it->second->getName() + " " + it->second->getInformation() + "] "; |
---|
| 212 | } |
---|
| 213 | return infoStr; |
---|
| 214 | } |
---|
| 215 | |
---|
[1663] | 216 | } |
---|