]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/opt.c
opt: allow parameter names in arguments.
[ccan] / ccan / opt / opt.c
index aa85325dd339c6b10f166a825aeec2ba4848400b..e7ebc3dd0550e2181b13c41438ddeb9e8f244dff 100644 (file)
@@ -17,14 +17,14 @@ const char *opt_argv0;
 /* Returns string after first '-'. */
 static const char *first_name(const char *names, unsigned *len)
 {
-       *len = strcspn(names + 1, "/");
+       *len = strcspn(names + 1, "/");
        return names + 1;
 }
 
 static const char *next_name(const char *names, unsigned *len)
 {
        names += *len;
-       if (!names[0])
+       if (names[0] == ' ' || names[0] == '=' || names[0] == '\0')
                return NULL;
        return first_name(names + 1, len);
 }
@@ -41,8 +41,6 @@ static const char *first_opt(unsigned *i, unsigned *len)
 
 static const char *next_opt(const char *p, unsigned *i, unsigned *len)
 {
-       if (!p)
-               (*i)++;
        for (; *i < opt_count; (*i)++) {
                if (opt_table[*i].flags == OPT_SUBTABLE)
                        continue;
@@ -126,6 +124,9 @@ static void check_opt(const struct opt_table *entry)
                                assert(*p != '?');
                        }
                }
+               /* Don't document args unless there are some. */
+               if (entry->flags == OPT_NOARG)
+                       assert(p[len] != ' ' && p[len] != '=');
        }
 }
 
@@ -259,13 +260,6 @@ static void parse_fail(void (*errlog)(const char *fmt, ...),
                       strcspn(longopt, "/"), longopt, problem);
 }
 
-void dump_optstate(void);
-void dump_optstate(void)
-{
-       printf("opterr = %i, optind = %i, optopt = %i, optarg = %s\n",
-              opterr, optind, optopt, optarg);
-}
-
 /* Parse your arguments. */
 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
 {