1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christoph Renner |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | #include "cmdline_parser.h" |
---|
17 | |
---|
18 | #include <cassert> |
---|
19 | #include "src/lib/util/substring.h" |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * standard constructor |
---|
26 | */ |
---|
27 | CmdLineParser::CmdLineParser () |
---|
28 | { |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * standard deconstructor |
---|
34 | */ |
---|
35 | CmdLineParser::~CmdLineParser () |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | bool CmdLineParser::add( int id, const std::string & longOption, char shortOption, int numArgs, const std::string & argNames, const std::string& help, bool back ) |
---|
41 | { |
---|
42 | ArgTableEntry entry; |
---|
43 | |
---|
44 | entry.id = id; |
---|
45 | entry.longOption = longOption; |
---|
46 | entry.shortOption = shortOption; |
---|
47 | entry.numArgs = numArgs; |
---|
48 | entry.argNames = argNames; |
---|
49 | entry.help = help; |
---|
50 | |
---|
51 | if ( back ) |
---|
52 | argTable.push_back( entry ); |
---|
53 | else |
---|
54 | argTable.push_front( entry ); |
---|
55 | return true; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | bool CmdLineParser::parse( ArgParserCallback cb, void * data, int argc, char ** argv ) |
---|
60 | { |
---|
61 | this->exeName = argv[0]; |
---|
62 | |
---|
63 | //put all args in vector |
---|
64 | std::vector<std::string> args; |
---|
65 | |
---|
66 | for ( int i = 1; i<argc; i++ ) |
---|
67 | { |
---|
68 | std::string s = argv[i]; |
---|
69 | |
---|
70 | if ( s.find( "=" ) == std::string::npos ) |
---|
71 | { |
---|
72 | if ( s.length() > 2 && s[0] == '-' && s[1] != '-' ) |
---|
73 | { |
---|
74 | for (unsigned int j = 1; j < s.length(); j++ ) |
---|
75 | { |
---|
76 | std::string t = "-"; |
---|
77 | t += s[j]; |
---|
78 | args.push_back( t ); |
---|
79 | } |
---|
80 | } |
---|
81 | else |
---|
82 | { |
---|
83 | args.push_back(s); |
---|
84 | } |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | std::string op = s; |
---|
89 | std::string ar = s; |
---|
90 | op.erase( op.find("=") ); |
---|
91 | ar.erase( 0, ar.find("=")+1); |
---|
92 | |
---|
93 | //PRINTF(0)("'%s' '%s'\n", op.c_str(), ar.c_str()); |
---|
94 | args.push_back( op ); |
---|
95 | args.push_back( ar ); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | unsigned int i = 0; |
---|
100 | |
---|
101 | ArgTable::iterator it; |
---|
102 | bool finish; |
---|
103 | bool found; |
---|
104 | |
---|
105 | while ( i < args.size() ) |
---|
106 | { |
---|
107 | found = false; |
---|
108 | for ( it = argTable.begin(); it != argTable.end(); it++ ) |
---|
109 | { |
---|
110 | if ( matches( *it, args[i], finish ) ) |
---|
111 | { |
---|
112 | found = true; |
---|
113 | |
---|
114 | unsigned int posArgs = 1; |
---|
115 | |
---|
116 | while ( i + posArgs < args.size() ) |
---|
117 | { |
---|
118 | if ( args[ i + posArgs ].length() > 0 && args[ i + posArgs ][0] == '-' ) |
---|
119 | break; |
---|
120 | else |
---|
121 | posArgs++; |
---|
122 | } |
---|
123 | |
---|
124 | posArgs--; |
---|
125 | |
---|
126 | if ( it->numArgs > posArgs ) |
---|
127 | { |
---|
128 | PRINTF(1)( "%s needs %d arguments!\n", args[i].c_str(), it->numArgs ); |
---|
129 | return false; |
---|
130 | } |
---|
131 | |
---|
132 | std::vector<MultiType> argArgs; |
---|
133 | |
---|
134 | for (unsigned int j = 1; j <= it->numArgs; j++ ) |
---|
135 | argArgs.push_back( args[i+j] ); |
---|
136 | |
---|
137 | if ( !cb( *it, data, args[i], argArgs ) ) |
---|
138 | return false; |
---|
139 | |
---|
140 | i += it->numArgs; |
---|
141 | |
---|
142 | if ( finish ) |
---|
143 | { |
---|
144 | i++; |
---|
145 | break; |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | assert( it->numArgs == 0 ); |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | if ( !found ) |
---|
155 | { |
---|
156 | PRINTF(1)("%s: illegal option\n", args[i].c_str()); |
---|
157 | return false; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | return true; |
---|
162 | } |
---|
163 | |
---|
164 | bool CmdLineParser::matches( ArgTableEntry entry, std::string arg, bool & finish ) |
---|
165 | { |
---|
166 | finish = true; |
---|
167 | |
---|
168 | if ( arg.length() < 2 ) |
---|
169 | return false; |
---|
170 | |
---|
171 | if ( arg[0] == '-' ) |
---|
172 | { |
---|
173 | if ( arg[1] == '-' ) |
---|
174 | { |
---|
175 | arg.erase( 0, 2 ); |
---|
176 | |
---|
177 | if ( entry.longOption.find('%') != std::string::npos ) |
---|
178 | { |
---|
179 | //TODO implement bether match algo |
---|
180 | assert( entry.longOption.find('%') == entry.longOption.length()-1 ); |
---|
181 | std::string lo = entry.longOption; |
---|
182 | lo.erase( lo.length()-1, 1 ); |
---|
183 | //PRINTF(0)("%s %s\n", arg.c_str(), lo.c_str()); |
---|
184 | return arg.find( lo ) == 0; |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | return arg.find( entry.longOption ) != std::string::npos; |
---|
189 | } |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | if ( arg.find(entry.shortOption) != std::string::npos && arg.length() != 2 && entry.numArgs != 0 ) |
---|
194 | { |
---|
195 | PRINTF(1)("using multiple flags together is only alowed if none needs an arugument. %c needs %d arguments\n", entry.shortOption, entry.numArgs); |
---|
196 | //FIXME find beter solution |
---|
197 | exit(1); |
---|
198 | return false; |
---|
199 | } |
---|
200 | finish = arg.length()==2; |
---|
201 | return arg.find(entry.shortOption) != std::string::npos; |
---|
202 | } |
---|
203 | } |
---|
204 | else |
---|
205 | return false; |
---|
206 | } |
---|
207 | |
---|
208 | void CmdLineParser::showHelp() |
---|
209 | { |
---|
210 | printf("Usage: %s [options]\n", exeName.c_str()); |
---|
211 | printf("\n"); |
---|
212 | |
---|
213 | std::list<std::vector<std::string> > output; |
---|
214 | |
---|
215 | for ( ArgTable::iterator it = argTable.begin(); it != argTable.end(); it++ ) |
---|
216 | { |
---|
217 | output.push_back( std::vector<std::string>() ); |
---|
218 | |
---|
219 | SubString substr( it->argNames ); |
---|
220 | std::string args; |
---|
221 | assert( it->numArgs == substr.size() ); |
---|
222 | |
---|
223 | for (unsigned int i = 0; i<it->numArgs; i++ ) |
---|
224 | { |
---|
225 | args += " [" + substr[i] + "]"; |
---|
226 | } |
---|
227 | |
---|
228 | if ( it->shortOption != '\0' ) |
---|
229 | { |
---|
230 | output.back().push_back( " -" + std::string((char*)&it->shortOption, 1) ); |
---|
231 | output.back().back() += args; |
---|
232 | } |
---|
233 | else |
---|
234 | output.back().push_back( "" ); |
---|
235 | |
---|
236 | if ( it->longOption != "" ) |
---|
237 | { |
---|
238 | output.back().push_back( "--" + it->longOption ); |
---|
239 | |
---|
240 | output.back().back() += args; |
---|
241 | } |
---|
242 | else |
---|
243 | output.back().push_back( "" ); |
---|
244 | |
---|
245 | output.back().push_back( it->help ); |
---|
246 | } |
---|
247 | |
---|
248 | output.push_back( std::vector<std::string>() ); |
---|
249 | output.back().push_back( "Option" ); |
---|
250 | output.back().push_back( "Long option" ); |
---|
251 | output.back().push_back( "Description" ); |
---|
252 | |
---|
253 | output.reverse(); |
---|
254 | |
---|
255 | unsigned int maxShort = 0; |
---|
256 | unsigned int maxLong = 0; |
---|
257 | |
---|
258 | std::list<std::vector<std::string> >::const_iterator it; |
---|
259 | |
---|
260 | for ( it = output.begin(); it != output.end(); it++ ) |
---|
261 | { |
---|
262 | if ( (*it)[0].length() > maxShort ) |
---|
263 | maxShort = (*it)[0].length(); |
---|
264 | |
---|
265 | if ( (*it)[1].length() > maxLong ) |
---|
266 | maxLong = (*it)[1].length(); |
---|
267 | } |
---|
268 | |
---|
269 | for ( it = output.begin(); it != output.end(); it++ ) |
---|
270 | { |
---|
271 | printf("%s ", (*it)[0].c_str()); |
---|
272 | |
---|
273 | for (unsigned int i = 0; i<maxShort-(*it)[0].length(); i++ ) |
---|
274 | printf(" "); |
---|
275 | |
---|
276 | printf("%s ", (*it)[1].c_str()); |
---|
277 | |
---|
278 | for (unsigned int i = 0; i<maxLong-(*it)[1].length(); i++ ) |
---|
279 | printf(" "); |
---|
280 | |
---|
281 | printf("%s\n", (*it)[2].c_str()); |
---|
282 | } |
---|
283 | |
---|
284 | exit(0); |
---|
285 | } |
---|