[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 | |
---|
| 31 | namespace orxonox |
---|
| 32 | { |
---|
| 33 | CommandLine::~CommandLine() |
---|
| 34 | { |
---|
| 35 | for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); |
---|
| 36 | it != cmdLineArgs_.end(); ++it) |
---|
| 37 | { |
---|
| 38 | delete it->second; |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | CommandLine& CommandLine::_getInstance() |
---|
| 43 | { |
---|
| 44 | static CommandLine instance; |
---|
| 45 | return instance; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | void CommandLine::_parse(int argc, char** argv) |
---|
| 49 | { |
---|
| 50 | if (bFirstTimeParse_) |
---|
| 51 | { |
---|
| 52 | // first shove all the shortcuts in a map |
---|
| 53 | for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin(); |
---|
| 54 | it != cmdLineArgs_.end(); ++it) |
---|
| 55 | { |
---|
| 56 | OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(), |
---|
| 57 | "Cannot have two command line shortcut with the same name."); |
---|
| 58 | if (it->second->getShortcut() != "") |
---|
| 59 | cmdLineArgsShortcut_[it->second->getShortcut()] = it->second; |
---|
| 60 | } |
---|
| 61 | bFirstTimeParse_ = false; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | std::string name; |
---|
| 65 | std::string shortcut; |
---|
| 66 | std::string val; |
---|
| 67 | for (int i = 1; i < argc; ++i) |
---|
| 68 | { |
---|
| 69 | if (argv[i][0] == '-') |
---|
| 70 | { |
---|
| 71 | if (argv[i][1] <= 57 && argv[i][1] >= 48) |
---|
| 72 | { |
---|
| 73 | // negative number as a value |
---|
| 74 | val += std::string(argv[i]) + " "; |
---|
| 75 | } |
---|
| 76 | else |
---|
| 77 | { |
---|
| 78 | // save old data first |
---|
| 79 | if (name != "") |
---|
| 80 | { |
---|
| 81 | checkFullArgument(name, val); |
---|
| 82 | name = ""; |
---|
| 83 | assert(shortcut == ""); |
---|
| 84 | } |
---|
| 85 | else if (shortcut != "") |
---|
| 86 | { |
---|
| 87 | checkShortcut(shortcut, val); |
---|
| 88 | shortcut = ""; |
---|
| 89 | assert(name == ""); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if (argv[i][1] == '-') |
---|
| 93 | { |
---|
| 94 | // full name argument |
---|
| 95 | name = argv[i] + 2; |
---|
| 96 | } |
---|
| 97 | else |
---|
| 98 | { |
---|
| 99 | // short cut |
---|
| 100 | shortcut = argv[i] + 1; |
---|
| 101 | } |
---|
| 102 | val = ""; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | else // value |
---|
| 106 | { |
---|
| 107 | if (name == "" && shortcut == "") |
---|
| 108 | { |
---|
| 109 | ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n"); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | val += argv[i]; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void CommandLine::checkFullArgument(const std::string& name, const std::string& value) |
---|
| 118 | { |
---|
| 119 | std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name); |
---|
| 120 | if (it == cmdLineArgs_.end()) |
---|
| 121 | ThrowException(Argument, "Command line argument '" + name + "' does not exist."); |
---|
| 122 | |
---|
| 123 | it->second->parse(value); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value) |
---|
| 127 | { |
---|
| 128 | std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut); |
---|
| 129 | if (it == cmdLineArgsShortcut_.end()) |
---|
| 130 | ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist."); |
---|
| 131 | |
---|
| 132 | it->second->parse(value); |
---|
| 133 | } |
---|
| 134 | } |
---|