Mercurial > hg > toybox
comparison lib/args.c @ 978:6d3c39cb8a9d
Cleanup renice and implement '|' (required option) in argument parsing.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 31 Jul 2013 03:24:58 -0500 |
parents | 011df43e35d5 |
children | a25239480e7a |
comparison
equal
deleted
inserted
replaced
977:9a96527bea94 | 978:6d3c39cb8a9d |
---|---|
106 int argc, minargs, maxargs, nodash; | 106 int argc, minargs, maxargs, nodash; |
107 char *arg; | 107 char *arg; |
108 struct opts *opts; | 108 struct opts *opts; |
109 struct longopts *longopts; | 109 struct longopts *longopts; |
110 int noerror, nodash_now, stopearly; | 110 int noerror, nodash_now, stopearly; |
111 unsigned excludes; | 111 unsigned excludes, requires; |
112 }; | 112 }; |
113 | 113 |
114 // Use getoptflagstate to parse parse one command line option from argv | 114 // Use getoptflagstate to parse parse one command line option from argv |
115 static int gotflag(struct getoptflagstate *gof, struct opts *opt) | 115 static int gotflag(struct getoptflagstate *gof, struct opts *opt) |
116 { | 116 { |
287 | 287 |
288 // Initialize enable/disable/exclude masks and pointers to store arguments. | 288 // Initialize enable/disable/exclude masks and pointers to store arguments. |
289 // (This goes right to left so we need the whole list before we can start.) | 289 // (This goes right to left so we need the whole list before we can start.) |
290 idx = 0; | 290 idx = 0; |
291 for (new = gof->opts; new; new = new->next) { | 291 for (new = gof->opts; new; new = new->next) { |
292 new->dex[1] = 1<<idx++; | 292 unsigned u = 1<<idx++; |
293 | |
294 new->dex[1] = u; | |
295 if (new->flags & 1) gof->requires |= u; | |
293 if (new->type) { | 296 if (new->type) { |
294 new->arg = (void *)nextarg; | 297 new->arg = (void *)nextarg; |
295 *(nextarg++) = new->val[2].l; | 298 *(nextarg++) = new->val[2].l; |
296 } | 299 } |
297 } | 300 } |
440 if (toys.optc<gof.minargs) | 443 if (toys.optc<gof.minargs) |
441 error_exit("Need%s %d argument%s", letters[!!(gof.minargs-1)], | 444 error_exit("Need%s %d argument%s", letters[!!(gof.minargs-1)], |
442 gof.minargs, letters[!(gof.minargs-1)]); | 445 gof.minargs, letters[!(gof.minargs-1)]); |
443 if (toys.optc>gof.maxargs) | 446 if (toys.optc>gof.maxargs) |
444 error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]); | 447 error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]); |
448 if (gof.requires && !(gof.requires & toys.optflags)) { | |
449 struct opts *req; | |
450 char needs[32], *s = needs; | |
451 | |
452 for (req = gof.opts; req; req = req->next) | |
453 if (req->flags & 1) *(s++) = req->c; | |
454 *s = 0; | |
455 | |
456 error_exit("Needs %s-%s", s[1] ? "one of " : "", needs); | |
457 } | |
445 toys.exithelp = 0; | 458 toys.exithelp = 0; |
446 | 459 |
447 if (CFG_TOYBOX_FREE) { | 460 if (CFG_TOYBOX_FREE) { |
448 llist_traverse(gof.opts, free); | 461 llist_traverse(gof.opts, free); |
449 llist_traverse(gof.longopts, free); | 462 llist_traverse(gof.longopts, free); |