]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/opt.c
opt: fix reporting of arguments when multiple arguments exist.
[ccan] / ccan / opt / opt.c
index 155d7aaf46b64c93ca712f550521f6e4e6aa423f..ea27dfa7679f2c8a6e8353558ceec5eff3297e49 100644 (file)
@@ -280,7 +280,6 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
               != -1) {
                char *problem;
                const char *name;
-               bool missing = false;
 
                /* optopt is 0 if it's an unknown long option, *or* if
                 * -? is a valid short option. */
@@ -291,8 +290,10 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
                                break;
                        }
                } else if (ret == ':') {
-                       missing = true;
-                       ret = optopt;
+                       /* Missing argument: longidx not updated :( */
+                       parse_fail(errlog, optopt, argv[optind-1]+2,
+                                  "option requires an argument");
+                       break;
                }
 
                if (ret != 0)
@@ -300,13 +301,6 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
                else
                        e = find_long(longidx, &name);
 
-               /* Missing argument */
-               if (missing) {
-                       parse_fail(errlog, ret, name,
-                                  "option requires an argument");
-                       break;
-               }
-
                if (e->flags == OPT_HASARG)
                        problem = e->cb_arg(optarg, e->arg);
                else