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