[945] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1505] | 3 | * > www.orxonox.net < |
---|
[945] | 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: |
---|
[1535] | 23 | * Reto Grieder |
---|
[1638] | 24 | * Benjamin Knecht <beni_at_orxonox.net> |
---|
[945] | 25 | * Co-authors: |
---|
[1638] | 26 | * ... |
---|
[945] | 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | @file ArgReader.cc |
---|
| 32 | @brief Argument Reader |
---|
| 33 | */ |
---|
| 34 | |
---|
[1062] | 35 | #include "ArgReader.h" |
---|
[1535] | 36 | #include "SubString.h" |
---|
[1062] | 37 | |
---|
[1652] | 38 | |
---|
[1638] | 39 | std::string ArgReader::parse(int argc, char **argv) |
---|
[945] | 40 | { |
---|
[1638] | 41 | std::string errorString; |
---|
| 42 | CmdLineArg arg; |
---|
| 43 | int i = 1; |
---|
| 44 | while (i < argc) |
---|
[1535] | 45 | { |
---|
[1638] | 46 | if (argv[i][0] == '-' && argv[i][1] == '-') // name |
---|
| 47 | { |
---|
| 48 | if (argv[i][2] == '\0') |
---|
| 49 | { |
---|
| 50 | errorString += "Expected word after \"--\".\n"; |
---|
| 51 | } |
---|
| 52 | arg.bChecked_ = false; |
---|
| 53 | arg.name_ = argv[i] + 2; |
---|
| 54 | arg.value_ = " "; |
---|
| 55 | arguments_.push_back(arg); |
---|
| 56 | } |
---|
| 57 | else // value |
---|
| 58 | { |
---|
| 59 | if (arguments_.size() == 0) |
---|
| 60 | { |
---|
| 61 | errorString += "Expected \"--\" in command line arguments.\n"; |
---|
| 62 | arg.bChecked_ = false; |
---|
| 63 | arg.name_ = ""; |
---|
| 64 | arg.value_ = " "; |
---|
| 65 | arguments_.push_back(arg); |
---|
| 66 | } |
---|
[945] | 67 | |
---|
[1638] | 68 | if (arguments_.back().value_ != " ") |
---|
| 69 | arguments_.back().value_ += " " + std::string(argv[i]); |
---|
| 70 | else |
---|
| 71 | arguments_.back().value_ = argv[i]; |
---|
| 72 | } |
---|
| 73 | ++i; |
---|
[945] | 74 | } |
---|
[1638] | 75 | return errorString; |
---|
[945] | 76 | } |
---|
| 77 | |
---|
[1638] | 78 | const std::string& ArgReader::getArgument(const std::string& option) |
---|
[945] | 79 | { |
---|
[1638] | 80 | unsigned int iArg = 0; |
---|
| 81 | while (iArg < arguments_.size()) |
---|
| 82 | { |
---|
| 83 | if (arguments_[iArg].name_ == option) |
---|
| 84 | { |
---|
| 85 | arguments_[iArg].bChecked_ = true; |
---|
| 86 | return arguments_[iArg].value_; |
---|
| 87 | } |
---|
| 88 | ++iArg; |
---|
| 89 | } |
---|
| 90 | return blankString; |
---|
[945] | 91 | } |
---|
| 92 | |
---|
[1638] | 93 | bool ArgReader::allChecked() |
---|
[945] | 94 | { |
---|
[1638] | 95 | bool argumentsChecked = true; |
---|
| 96 | for (unsigned int i = 1; i < arguments_.size(); ++i) |
---|
| 97 | argumentsChecked &= arguments_[i].bChecked_; |
---|
| 98 | |
---|
| 99 | return argumentsChecked; |
---|
[945] | 100 | } |
---|