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 | * Benjamin Knecht <beni_at_orxonox.net> |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | /** |
---|
31 | @file ArgReader.cc |
---|
32 | @brief Argument Reader |
---|
33 | */ |
---|
34 | |
---|
35 | #include "ArgReader.h" |
---|
36 | #include "SubString.h" |
---|
37 | |
---|
38 | std::string ArgReader::parse(int argc, char **argv) |
---|
39 | { |
---|
40 | std::string errorString; |
---|
41 | CmdLineArg arg; |
---|
42 | int i = 1; |
---|
43 | while (i < argc) |
---|
44 | { |
---|
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 | } |
---|
66 | |
---|
67 | if (arguments_.back().value_ != " ") |
---|
68 | arguments_.back().value_ += " " + std::string(argv[i]); |
---|
69 | else |
---|
70 | arguments_.back().value_ = argv[i]; |
---|
71 | } |
---|
72 | ++i; |
---|
73 | } |
---|
74 | return errorString; |
---|
75 | } |
---|
76 | |
---|
77 | const std::string& ArgReader::getArgument(const std::string& option) |
---|
78 | { |
---|
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; |
---|
90 | } |
---|
91 | |
---|
92 | bool ArgReader::allChecked() |
---|
93 | { |
---|
94 | bool argumentsChecked = true; |
---|
95 | for (unsigned int i = 1; i < arguments_.size(); ++i) |
---|
96 | argumentsChecked &= arguments_[i].bChecked_; |
---|
97 | |
---|
98 | return argumentsChecked; |
---|
99 | } |
---|