[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 | |
---|
[1638] | 38 | std::string ArgReader::parse(int argc, char **argv) |
---|
[945] | 39 | { |
---|
[1638] | 40 | std::string errorString; |
---|
| 41 | CmdLineArg arg; |
---|
| 42 | int i = 1; |
---|
| 43 | while (i < argc) |
---|
[1535] | 44 | { |
---|
[1638] | 45 | if (argv[i][0] == '-' && argv[i][1] == '-') // name |
---|
| 46 | { |
---|
| 47 | if (argv[i][2] == '\0') |
---|
| 48 | { |
---|
| 49 | errorString += "Expected word after \"--\".\n"; |
---|
| 50 | } |
---|
| 51 | arg.bChecked_ = false; |
---|
| 52 | arg.name_ = argv[i] + 2; |
---|
| 53 | arg.value_ = " "; |
---|
| 54 | arguments_.push_back(arg); |
---|
| 55 | } |
---|
| 56 | else // value |
---|
| 57 | { |
---|
| 58 | if (arguments_.size() == 0) |
---|
| 59 | { |
---|
| 60 | errorString += "Expected \"--\" in command line arguments.\n"; |
---|
| 61 | arg.bChecked_ = false; |
---|
| 62 | arg.name_ = ""; |
---|
| 63 | arg.value_ = " "; |
---|
| 64 | arguments_.push_back(arg); |
---|
| 65 | } |
---|
[945] | 66 | |
---|
[1638] | 67 | if (arguments_.back().value_ != " ") |
---|
| 68 | arguments_.back().value_ += " " + std::string(argv[i]); |
---|
| 69 | else |
---|
| 70 | arguments_.back().value_ = argv[i]; |
---|
| 71 | } |
---|
| 72 | ++i; |
---|
[945] | 73 | } |
---|
[1638] | 74 | return errorString; |
---|
[945] | 75 | } |
---|
| 76 | |
---|
[1638] | 77 | const std::string& ArgReader::getArgument(const std::string& option) |
---|
[945] | 78 | { |
---|
[1638] | 79 | unsigned int iArg = 0; |
---|
| 80 | while (iArg < arguments_.size()) |
---|
| 81 | { |
---|
| 82 | if (arguments_[iArg].name_ == option) |
---|
| 83 | { |
---|
| 84 | arguments_[iArg].bChecked_ = true; |
---|
| 85 | return arguments_[iArg].value_; |
---|
| 86 | } |
---|
| 87 | ++iArg; |
---|
| 88 | } |
---|
| 89 | return blankString; |
---|
[945] | 90 | } |
---|
| 91 | |
---|
[1638] | 92 | bool ArgReader::allChecked() |
---|
[945] | 93 | { |
---|
[1638] | 94 | bool argumentsChecked = true; |
---|
| 95 | for (unsigned int i = 1; i < arguments_.size(); ++i) |
---|
| 96 | argumentsChecked &= arguments_[i].bChecked_; |
---|
| 97 | |
---|
| 98 | return argumentsChecked; |
---|
[945] | 99 | } |
---|