Mercurial > hg > index.cgi
comparison lwlib/lw_cmdline.c @ 191:ddffceb3c331
Added "documentation only" options to lw_cmdline and also allowed options that do not have a long equivalent
author | lost@l-w.ca |
---|---|
date | Fri, 13 Jan 2012 21:28:34 -0700 |
parents | 3e9057059a43 |
children | 8ae2670377ca |
comparison
equal
deleted
inserted
replaced
190:20ba68be2cd7 | 191:ddffceb3c331 |
---|---|
43 { | 43 { |
44 struct lw_cmdline_options **o1, **o2; | 44 struct lw_cmdline_options **o1, **o2; |
45 o1 = (struct lw_cmdline_options **)e1; | 45 o1 = (struct lw_cmdline_options **)e1; |
46 o2 = (struct lw_cmdline_options **)e2; | 46 o2 = (struct lw_cmdline_options **)e2; |
47 | 47 |
48 return strcmp((*o1) -> name, (*o2) -> name); | 48 return strcmp((*o1) -> name ? (*o1) -> name : "", (*o2) -> name ? (*o2) -> name : ""); |
49 } | 49 } |
50 | 50 |
51 static int cmpr2(const void *e1, const void *e2) | 51 static int cmpr2(const void *e1, const void *e2) |
52 { | 52 { |
53 struct lw_cmdline_options **o1, **o2; | 53 struct lw_cmdline_options **o1, **o2; |
67 int nopt; | 67 int nopt; |
68 int i; | 68 int i; |
69 int t; | 69 int t; |
70 int col; | 70 int col; |
71 | 71 |
72 for (nopt = 0; parser -> options[nopt].name; nopt++) | 72 for (nopt = 0; parser -> options[nopt].name || parser -> options[nopt].key || parser -> options[nopt].doc; nopt++) |
73 /* do nothing */ ; | 73 /* do nothing */ ; |
74 | 74 |
75 slist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3)); | 75 slist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3)); |
76 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3)); | 76 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3)); |
77 | 77 |
155 } | 155 } |
156 | 156 |
157 /* print long options */ | 157 /* print long options */ |
158 for (i = 0; i < nopt + 3; i++) | 158 for (i = 0; i < nopt + 3; i++) |
159 { | 159 { |
160 if (!(llist[i]->name)) | |
161 continue; | |
160 if (llist[i]->arg) | 162 if (llist[i]->arg) |
161 { | 163 { |
162 t = strlen(llist[i] -> name) + 6 + strlen(llist[i] -> arg); | 164 t = strlen(llist[i] -> name) + 6 + strlen(llist[i] -> arg); |
163 if (llist[i] -> flags & lw_cmdline_opt_optional) | 165 if (llist[i] -> flags & lw_cmdline_opt_optional) |
164 t += 2; | 166 t += 2; |
165 if (col + t > LLEN) | 167 if (col + t > LLEN) |
166 { | 168 { |
167 printf("\n "); | 169 printf("\n "); |
168 col = 7; | 170 col = 7; |
169 } | 171 } |
170 printf(" [--%s%s=%s%s]", | 172 if (llist[i] -> flags & lw_cmdline_opt_doc) |
171 llist[i] -> name, | 173 { |
172 (llist[i] -> flags & lw_cmdline_opt_optional) ? "[" : "", | 174 printf(" [%s%s]", llist[i] -> name, llist[i] -> arg); |
173 llist[i] -> arg, | 175 t = strlen(llist[i] -> name) + strlen(llist[i] -> arg) + 3; |
174 (llist[i] -> flags & lw_cmdline_opt_optional) ? "]" : ""); | 176 } |
177 else | |
178 { | |
179 printf(" [--%s%s=%s%s]", | |
180 llist[i] -> name, | |
181 (llist[i] -> flags & lw_cmdline_opt_optional) ? "[" : "", | |
182 llist[i] -> arg, | |
183 (llist[i] -> flags & lw_cmdline_opt_optional) ? "]" : ""); | |
184 } | |
175 col += t; | 185 col += t; |
176 } | 186 } |
177 else | 187 else |
178 { | 188 { |
179 t = strlen(llist[i] -> name) + 5; | 189 t = strlen(llist[i] -> name) + 5; |
180 if (col + t > LLEN) | 190 if (col + t > LLEN) |
181 { | 191 { |
182 printf("\n "); | 192 printf("\n "); |
183 col = 7; | 193 col = 7; |
184 } | 194 } |
185 printf(" [--%s]", llist[i] -> name); | 195 if (llist[i] -> flags & lw_cmdline_opt_doc) |
196 { | |
197 t -= 2; | |
198 printf(" [%s]", llist[i] -> name); | |
199 } | |
200 else | |
201 { | |
202 printf(" [--%s]", llist[i] -> name); | |
203 } | |
186 col += t; | 204 col += t; |
187 } | 205 } |
188 } | 206 } |
189 | 207 |
190 /* print "non option" text */ | 208 /* print "non option" text */ |
208 struct lw_cmdline_options **llist; | 226 struct lw_cmdline_options **llist; |
209 int nopt; | 227 int nopt; |
210 int i; | 228 int i; |
211 char *tstr; | 229 char *tstr; |
212 int col = 0; | 230 int col = 0; |
231 int noequ; | |
213 | 232 |
214 tstr = parser -> doc; | 233 tstr = parser -> doc; |
215 for (nopt = 0; parser -> options[nopt].name; nopt++) | 234 for (nopt = 0; parser -> options[nopt].name || parser -> options[nopt].key || parser -> options[nopt].doc; nopt++) |
216 /* do nothing */ ; | 235 /* do nothing */ ; |
217 | 236 |
218 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3)); | 237 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3)); |
219 | 238 |
220 for (i = 0; i < nopt; i++) | 239 for (i = 0; i < nopt; i++) |
243 fputc('\n', stdout); | 262 fputc('\n', stdout); |
244 | 263 |
245 /* display options - do it the naïve way for now */ | 264 /* display options - do it the naïve way for now */ |
246 for (i = 0; i < (nopt + 3); i++) | 265 for (i = 0; i < (nopt + 3); i++) |
247 { | 266 { |
248 if (llist[i] -> key > 0x20 && llist[i] -> key < 0x7F) | 267 noequ = 0; |
249 { | 268 if (llist[i] -> flags & lw_cmdline_opt_doc) |
250 printf(" -%c, ", llist[i] -> key); | 269 { |
270 col = strlen(llist[i] -> name) + 2; | |
271 printf(" %s", llist[i] -> name); | |
272 noequ = 1; | |
273 } | |
274 else if (llist[i] -> key > 0x20 && llist[i] -> key < 0x7F) | |
275 { | |
276 printf(" -%c", llist[i] -> key); | |
277 col = 5; | |
278 if (llist[i] -> name) | |
279 { | |
280 col++; | |
281 fputc(',', stdout); | |
282 } | |
283 fputc(' ', stdout); | |
251 } | 284 } |
252 else | 285 else |
253 { | 286 { |
254 printf(" "); | 287 printf(" "); |
255 } | 288 col = 6; |
256 col = 8 + strlen(llist[i] -> name); | 289 } |
257 | 290 if (llist[i] -> name && !(llist[i] -> flags & lw_cmdline_opt_doc)) |
258 printf("--%s", llist[i] -> name); | 291 { |
292 col += 2 + strlen(llist[i] -> name); | |
293 printf("--%s", llist[i] -> name); | |
294 } | |
259 if (llist[i] -> arg) | 295 if (llist[i] -> arg) |
260 { | 296 { |
261 if (llist[i] -> flags & lw_cmdline_opt_optional) | 297 if (llist[i] -> flags & lw_cmdline_opt_optional) |
262 { | 298 { |
263 printf("[=%s]", llist[i] -> arg); | 299 col++; |
264 col += 3 + strlen(llist[i] -> arg); | 300 fputc('[', stdout); |
265 } | 301 } |
266 else | 302 if (noequ) |
267 { | 303 { |
268 printf("=%s", llist[i] -> arg); | 304 fputc('=', stdout); |
269 col += 1 + strlen(llist[i] -> arg); | 305 col++; |
306 } | |
307 printf("%s", llist[i] -> arg); | |
308 col += strlen(llist[i] -> arg); | |
309 if (llist[i] -> flags & lw_cmdline_opt_optional) | |
310 { | |
311 col++; | |
312 fputc(']', stdout); | |
270 } | 313 } |
271 } | 314 } |
272 if (llist[i] -> doc) | 315 if (llist[i] -> doc) |
273 { | 316 { |
274 char *s = llist[i] -> doc; | 317 char *s = llist[i] -> doc; |
388 if (argv[i][cch] == '?') | 431 if (argv[i][cch] == '?') |
389 goto do_help; | 432 goto do_help; |
390 if (argv[i][cch] == 'V') | 433 if (argv[i][cch] == 'V') |
391 goto do_version; | 434 goto do_version; |
392 /* look up key */ | 435 /* look up key */ |
393 for (j = 0; parser -> options[j].name; j++) | 436 for (j = 0; parser -> options[j].name || parser -> options[j].key || parser -> options[j].doc; j++) |
394 if (parser -> options[j].key == argv[i][cch]) | 437 if (parser -> options[j].key == argv[i][cch]) |
395 break; | 438 break; |
396 cch++; | 439 cch++; |
397 tstr = argv[i] + cch; | 440 tstr = argv[i] + cch; |
398 if (*tstr == 0) | 441 if (*tstr == 0) |
422 strncpy(tstr, argv[i] + 2, j - 2); | 465 strncpy(tstr, argv[i] + 2, j - 2); |
423 tstr[j - 2] = 0; | 466 tstr[j - 2] = 0; |
424 if (argv[i][j] == '=') | 467 if (argv[i][j] == '=') |
425 j++; | 468 j++; |
426 cch = j; | 469 cch = j; |
427 for (j = 0; parser -> options[j].name; j++) | 470 for (j = 0; parser -> options[j].name || parser -> options[j].key || parser -> options[j].doc; j++) |
428 { | 471 { |
429 if (strcmp(parser -> options[j].name, tstr) == 0) | 472 if (parser -> options[j].name && strcmp(parser -> options[j].name, tstr) == 0) |
430 break; | 473 break; |
431 } | 474 } |
432 lw_free(tstr); | 475 lw_free(tstr); |
433 tstr = argv[i] + cch; | 476 tstr = argv[i] + cch; |
434 if (*tstr == 0) | 477 if (*tstr == 0) |