diff 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
line wrap: on
line diff
--- a/lib/args.c	Mon Jul 29 21:16:55 2013 -0500
+++ b/lib/args.c	Wed Jul 31 03:24:58 2013 -0500
@@ -108,7 +108,7 @@
   struct opts *opts;
   struct longopts *longopts;
   int noerror, nodash_now, stopearly;
-  unsigned excludes;
+  unsigned excludes, requires;
 };
 
 // Use getoptflagstate to parse parse one command line option from argv
@@ -289,7 +289,10 @@
   // (This goes right to left so we need the whole list before we can start.)
   idx = 0;
   for (new = gof->opts; new; new = new->next) {
-    new->dex[1] = 1<<idx++;
+    unsigned u = 1<<idx++;
+
+    new->dex[1] = u;
+    if (new->flags & 1) gof->requires |= u;
     if (new->type) {
       new->arg = (void *)nextarg;
       *(nextarg++) = new->val[2].l;
@@ -442,6 +445,16 @@
       gof.minargs, letters[!(gof.minargs-1)]);
   if (toys.optc>gof.maxargs)
     error_exit("Max %d argument%s", gof.maxargs, letters[!(gof.maxargs-1)]);
+  if (gof.requires && !(gof.requires & toys.optflags)) {
+    struct opts *req;
+    char needs[32], *s = needs;
+
+    for (req = gof.opts; req; req = req->next)
+      if (req->flags & 1) *(s++) = req->c;
+    *s = 0;
+
+    error_exit("Needs %s-%s", s[1] ? "one of " : "", needs);
+  }
   toys.exithelp = 0;
 
   if (CFG_TOYBOX_FREE) {