]> git.ozlabs.org Git - ccan/commitdiff
opt: Put actual options inside names.
authorRusty Russell <rusty@rustcorp.com.au>
Sun, 3 Oct 2010 11:53:29 +0000 (22:23 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Sun, 3 Oct 2010 11:53:29 +0000 (22:23 +1030)
This is more explicit than separate short and long (great for grep!) and
simpler.

ccan/opt/opt.c
ccan/opt/opt.h
ccan/opt/private.h
ccan/opt/test/run-helpers.c
ccan/opt/test/run-iter.c [new file with mode: 0644]
ccan/opt/test/run-no-options.c [new file with mode: 0644]
ccan/opt/test/run-usage.c
ccan/opt/test/run.c
ccan/opt/test/utils.c
ccan/opt/usage.c

index 236d2af00d20027d8f1b505f2b7ad2ec429f4ac4..155d7aaf46b64c93ca712f550521f6e4e6aa423f 100644 (file)
 #include "private.h"
 
 struct opt_table *opt_table;
 #include "private.h"
 
 struct opt_table *opt_table;
-unsigned int opt_count;
+unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long;
 const char *opt_argv0;
 
 const char *opt_argv0;
 
+static const char *first_name(const char *names, unsigned *len)
+{
+       *len = strcspn(names + 1, "/");
+       return names + 1;
+}
+
+static const char *next_name(const char *names, unsigned *len)
+{
+       names += *len;
+       if (!names[0])
+               return NULL;
+       return first_name(names + 1, len);
+}
+
+/* FIXME: Combine with next_opt */
+static const char *first_opt(bool want_long, unsigned *i, unsigned *len)
+{
+       const char *p;
+       for (*i = 0; *i < opt_count; (*i)++) {
+               if (opt_table[*i].flags == OPT_SUBTABLE)
+                       continue;
+               for (p = first_name(opt_table[*i].names, len);
+                    p;
+                    p = next_name(p, len)) {
+                       if ((p[0] == '-') == want_long) {
+                               if (want_long) {
+                                       /* Skip leading "-" */
+                                       (*len)--;
+                                       p++;
+                               }
+                               return p;
+                       }
+               }
+       }
+       return NULL;
+}
+
+static const char *next_opt(const char *names, bool want_long,
+                           unsigned *i, unsigned *len)
+{
+       const char *p = next_name(names, len);
+       for (;;) {
+               while (p) {
+                       if ((p[0] == '-') == want_long) {
+                               if (want_long) {
+                                       /* Skip leading "-" */
+                                       (*len)--;
+                                       p++;
+                               }
+                               return p;
+                       }
+                       p = next_name(p, len);
+               }
+               do {
+                       (*i)++;
+               } while (*i < opt_count && opt_table[*i].flags == OPT_SUBTABLE);
+               if (*i == opt_count)
+                       return NULL;
+               p = first_name(opt_table[*i].names, len);
+       }
+}
+
+static const char *first_lopt(unsigned *i, unsigned *len)
+{
+       return first_opt(true, i, len);
+}
+
+static const char *next_lopt(const char *names, unsigned *i, unsigned *len)
+{
+       return next_opt(names, true, i, len);
+}
+
+const char *first_sopt(unsigned *i)
+{
+       unsigned unused_len;
+       return first_opt(false, i, &unused_len);
+}
+
+const char *next_sopt(const char *names, unsigned *i)
+{
+       unsigned unused_len = 1;
+
+       return next_opt(names, false, i, &unused_len);
+}
+
 static void check_opt(const struct opt_table *entry)
 {
 static void check_opt(const struct opt_table *entry)
 {
+       const char *p;
+       unsigned len;
+
        assert(entry->flags == OPT_HASARG || entry->flags == OPT_NOARG);
        assert(entry->flags == OPT_HASARG || entry->flags == OPT_NOARG);
-       assert(entry->shortopt || entry->longopt);
-       assert(entry->shortopt != ':');
-       assert(entry->shortopt != '?' || entry->flags == OPT_NOARG);
+
+       assert(entry->names[0] == '-');
+       for (p = first_name(entry->names, &len); p; p = next_name(p, &len)) {
+               if (*p == '-') {
+                       assert(len > 1);
+                       opt_num_long++;
+               } else {
+                       assert(len == 1);
+                       assert(*p != ':');
+                       opt_num_short++;
+                       if (entry->flags == OPT_HASARG) {
+                               opt_num_short_arg++;
+                               /* FIXME: -? with ops breaks getopt_long */
+                               assert(*p != '?');
+                       }
+               }
+       }
 }
 
 static void add_opt(const struct opt_table *entry)
 }
 
 static void add_opt(const struct opt_table *entry)
@@ -28,15 +130,14 @@ static void add_opt(const struct opt_table *entry)
        opt_table[opt_count++] = *entry;
 }
 
        opt_table[opt_count++] = *entry;
 }
 
-void _opt_register(const char *longopt, char shortopt, enum opt_flags flags,
+void _opt_register(const char *names, enum opt_flags flags,
                   char *(*cb)(void *arg),
                   char *(*cb_arg)(const char *optarg, void *arg),
                   void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
                   void *arg, const char *desc)
 {
        struct opt_table opt;
                   char *(*cb)(void *arg),
                   char *(*cb_arg)(const char *optarg, void *arg),
                   void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
                   void *arg, const char *desc)
 {
        struct opt_table opt;
-       opt.longopt = longopt;
-       opt.shortopt = shortopt;
+       opt.names = names;
        opt.flags = flags;
        opt.cb = cb;
        opt.cb_arg = cb_arg;
        opt.flags = flags;
        opt.cb = cb;
        opt.cb_arg = cb_arg;
@@ -71,61 +172,66 @@ void opt_register_table(const struct opt_table entry[], const char *desc)
 
 static char *make_optstring(void)
 {
 
 static char *make_optstring(void)
 {
-       /* Worst case, each one is ":x:", plus nul term. */
-       char *str = malloc(1 + opt_count * 2 + 1);
-       unsigned int num, i;
+       char *str = malloc(1 + opt_num_short + opt_num_short_arg + 1);
+       const char *p;
+       unsigned int i, num = 0;
 
        /* This tells getopt_long we want a ':' returned for missing arg. */
 
        /* This tells getopt_long we want a ':' returned for missing arg. */
-       str[0] = ':';
-       num = 1;
-       for (i = 0; i < opt_count; i++) {
-               if (!opt_table[i].shortopt)
-                       continue;
-               str[num++] = opt_table[i].shortopt;
+       str[num++] = ':';
+       for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
+               str[num++] = *p;
                if (opt_table[i].flags == OPT_HASARG)
                        str[num++] = ':';
        }
                if (opt_table[i].flags == OPT_HASARG)
                        str[num++] = ':';
        }
-       str[num] = '\0';
+       str[num++] = '\0';
+       assert(num == 1 + opt_num_short + opt_num_short_arg + 1);
        return str;
 }
 
 static struct option *make_options(void)
 {
        return str;
 }
 
 static struct option *make_options(void)
 {
-       struct option *options = malloc(sizeof(*options) * (opt_count + 1));
-       unsigned int i, num;
+       struct option *options = malloc(sizeof(*options) * (opt_num_long + 1));
+       unsigned int i, num = 0, len;
+       const char *p;
 
 
-       for (num = i = 0; i < opt_count; i++) {
-               if (!opt_table[i].longopt)
-                       continue;
-               options[num].name = opt_table[i].longopt;
+       for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
+               char *buf = malloc(len + 1);
+               memcpy(buf, p, len);
+               buf[len] = 0;
+               options[num].name = buf;
                options[num].has_arg = (opt_table[i].flags == OPT_HASARG);
                options[num].flag = NULL;
                options[num].val = 0;
                num++;
        }
        memset(&options[num], 0, sizeof(options[num]));
                options[num].has_arg = (opt_table[i].flags == OPT_HASARG);
                options[num].flag = NULL;
                options[num].val = 0;
                num++;
        }
        memset(&options[num], 0, sizeof(options[num]));
+       assert(num == opt_num_long);
        return options;
 }
 
 static struct opt_table *find_short(char shortopt)
 {
        unsigned int i;
        return options;
 }
 
 static struct opt_table *find_short(char shortopt)
 {
        unsigned int i;
-       for (i = 0; i < opt_count; i++) {
-               if (opt_table[i].shortopt == shortopt)
+       const char *p;
+
+       for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
+               if (*p == shortopt)
                        return &opt_table[i];
        }
        abort();
 }
 
 /* We want the index'th long entry. */
                        return &opt_table[i];
        }
        abort();
 }
 
 /* We want the index'th long entry. */
-static struct opt_table *find_long(int index)
+static struct opt_table *find_long(int index, const char **name)
 {
 {
-       unsigned int i;
-       for (i = 0; i < opt_count; i++) {
-               if (!opt_table[i].longopt)
-                       continue;
-               if (index == 0)
+       unsigned int i, len;
+       const char *p;
+
+       for (p = first_lopt(&i, &len); p; p = next_lopt(p, &i, &len)) {
+               if (index == 0) {
+                       *name = p;
                        return &opt_table[i];
                        return &opt_table[i];
+               }
                index--;
        }
        abort();
                index--;
        }
        abort();
@@ -144,7 +250,8 @@ static void parse_fail(void (*errlog)(const char *fmt, ...),
        if (shortopt)
                errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
        else
        if (shortopt)
                errlog("%s: -%c: %s", opt_argv0, shortopt, problem);
        else
-               errlog("%s: --%s: %s", opt_argv0, longopt, problem);
+               errlog("%s: --%.*s: %s", opt_argv0,
+                      strcspn(longopt, "/"), longopt, problem);
 }
 
 void dump_optstate(void);
 }
 
 void dump_optstate(void);
@@ -168,10 +275,11 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
 
        /* Reset in case we're called more than once. */
        optopt = 0;
 
        /* Reset in case we're called more than once. */
        optopt = 0;
-       optind = 1;
+       optind = 0;
        while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
               != -1) {
                char *problem;
        while ((ret = getopt_long(*argc, argv, optstring, options, &longidx))
               != -1) {
                char *problem;
+               const char *name;
                bool missing = false;
 
                /* optopt is 0 if it's an unknown long option, *or* if
                bool missing = false;
 
                /* optopt is 0 if it's an unknown long option, *or* if
@@ -190,11 +298,11 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
                if (ret != 0)
                        e = find_short(ret);
                else
                if (ret != 0)
                        e = find_short(ret);
                else
-                       e = find_long(longidx);
+                       e = find_long(longidx, &name);
 
                /* Missing argument */
                if (missing) {
 
                /* Missing argument */
                if (missing) {
-                       parse_fail(errlog, e->shortopt, e->longopt,
+                       parse_fail(errlog, ret, name,
                                   "option requires an argument");
                        break;
                }
                                   "option requires an argument");
                        break;
                }
@@ -205,8 +313,7 @@ bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...))
                        problem = e->cb(e->arg);
 
                if (problem) {
                        problem = e->cb(e->arg);
 
                if (problem) {
-                       parse_fail(errlog, e->shortopt, e->longopt,
-                                  problem);
+                       parse_fail(errlog, ret, name, problem);
                        free(problem);
                        break;
                }
                        free(problem);
                        break;
                }
index 74bda85bf348b1fc22c5f5cf7a5b81939f6ffb07..253d03c4098691988a8edf4ef4352d3b72931be5 100644 (file)
@@ -16,8 +16,7 @@ enum opt_flags {
 #define OPT_SHOW_LEN 80
 
 struct opt_table {
 #define OPT_SHOW_LEN 80
 
 struct opt_table {
-       const char *longopt; /* --longopt, or NULL */
-       char shortopt; /* -s, or 0 */
+       const char *names; /* slash-separated names, --longopt or -s */
        enum opt_flags flags;
        char *(*cb)(void *arg); /* OPT_NOARG */
        char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
        enum opt_flags flags;
        char *(*cb)(void *arg); /* OPT_NOARG */
        char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
@@ -28,8 +27,7 @@ struct opt_table {
 
 /**
  * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
 
 /**
  * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
- * @longopt: the name of the argument (eg. "foo" for "--foo"), or NULL.
- * @shortopt: the character of the argument (eg. 'f' for "-f"), or 0.
+ * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
  * @cb: the callback when the option is found.
  * @arg: the argument to hand to @cb.
  *
  * @cb: the callback when the option is found.
  * @arg: the argument to hand to @cb.
  *
@@ -37,20 +35,19 @@ struct opt_table {
  * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)",
  * where "type" is the type of the @arg argument.
  *
  * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)",
  * where "type" is the type of the @arg argument.
  *
- * At least one of @longopt and @shortopt must be non-zero.  If the
- * @cb returns non-NULL, opt_parse() will stop parsing, use the returned
- * string to form an error message, free() the string and return false.
+ * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
+ * returned string to form an error message for errlog(), free() the
+ * string and return false.
  *
  * See Also:
  *     OPT_WITH_ARG()
  */
  *
  * See Also:
  *     OPT_WITH_ARG()
  */
-#define OPT_WITHOUT_ARG(longopt, shortopt, cb, arg)    \
-       (longopt), (shortopt), OPT_CB_NOARG((cb), (arg))
+#define OPT_WITHOUT_ARG(names, cb, arg) \
+       (names), OPT_CB_NOARG((cb), (arg))
 
 /**
  * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
 
 /**
  * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
- * @longopt: the name of the argument (eg. "foo" for "--foo <arg>"), or NULL.
- * @shortopt: the character of the argument (eg. 'f' for "-f <arg>"), or 0.
+ * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
  * @cb: the callback when the option is found (along with <arg>).
  * @show: the callback to print the value in get_usage (or NULL)
  * @arg: the argument to hand to @cb and @show
  * @cb: the callback when the option is found (along with <arg>).
  * @show: the callback to print the value in get_usage (or NULL)
  * @arg: the argument to hand to @cb and @show
@@ -66,41 +63,31 @@ struct opt_table {
  * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
  * nul-terminate that buffer.
  *
  * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
  * nul-terminate that buffer.
  *
- * At least one of @longopt and @shortopt must be non-zero.  If the
- * @cb returns false, opt_parse() will stop parsing and return false.
+ * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
+ * returned string to form an error message for errlog(), free() the
+ * string and return false.
  *
  * See Also:
  *     OPT_WITHOUT_ARG()
  */
  *
  * See Also:
  *     OPT_WITHOUT_ARG()
  */
-#define OPT_WITH_ARG(longopt, shortopt, cb, show, arg) \
-       (longopt), (shortopt), OPT_CB_ARG((cb), (show), (arg))
+#define OPT_WITH_ARG(name, cb, show, arg) \
+       (name), OPT_CB_ARG((cb), (show), (arg))
 
 /**
  * OPT_SUBTABLE() - macro for including another table inside a table.
  * @table: the table to include in this table.
  * @desc: description of this subtable (for opt_usage()) or NULL.
 
 /**
  * OPT_SUBTABLE() - macro for including another table inside a table.
  * @table: the table to include in this table.
  * @desc: description of this subtable (for opt_usage()) or NULL.
- *
- * The @desc field can be opt_table_hidden to hide the options from opt_usage().
  */
 #define OPT_SUBTABLE(table, desc)                                      \
  */
 #define OPT_SUBTABLE(table, desc)                                      \
-       { (const char *)(table), sizeof(_check_is_entry(table)),        \
-       OPT_SUBTABLE,   NULL, NULL, NULL, NULL, (desc) }
-
-/**
- * opt_table_hidden - string for undocumented option tables.
- *
- * This can be used as the desc option to OPT_SUBTABLE or passed to
- * opt_register_table() if you want the options not to be shown by
- * opt_usage().
- */
-extern const char opt_table_hidden[];
+       { (const char *)(table), OPT_SUBTABLE,                          \
+       sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, NULL, (desc) }
 
 /**
  * OPT_ENDTABLE - macro to create final entry in table.
  *
  * This must be the final element in the opt_table array.
  */
 
 /**
  * OPT_ENDTABLE - macro to create final entry in table.
  *
  * This must be the final element in the opt_table array.
  */
-#define OPT_ENDTABLE { NULL, 0, OPT_END }
+#define OPT_ENDTABLE { NULL, OPT_END }
 
 /**
  * opt_register_table - register a table of options
 
 /**
  * opt_register_table - register a table of options
@@ -111,9 +98,11 @@ extern const char opt_table_hidden[];
  *
  * Example:
  * static struct opt_table opts[] = {
  *
  * Example:
  * static struct opt_table opts[] = {
- *     { OPT_WITHOUT_ARG("verbose", 'v', opt_inc_intval, &verbose),
+ *     { OPT_WITHOUT_ARG("--verbose", opt_inc_intval, &verbose),
  *       "Verbose mode (can be specified more than once)" },
  *       "Verbose mode (can be specified more than once)" },
- *     { OPT_WITHOUT_ARG("usage", 0, opt_usage_and_exit,
+ *     { OPT_WITHOUT_ARG("-v", opt_inc_intval, &verbose),
+ *       "Verbose mode (can be specified more than once)" },
+ *     { OPT_WITHOUT_ARG("--usage", opt_usage_and_exit,
  *                       "args...\nA silly test program."),
  *       "Print this message." },
  *     OPT_ENDTABLE
  *                       "args...\nA silly test program."),
  *       "Print this message." },
  *     OPT_ENDTABLE
@@ -126,8 +115,7 @@ void opt_register_table(const struct opt_table table[], const char *desc);
 
 /**
  * opt_register_noarg - register an option with no arguments
 
 /**
  * opt_register_noarg - register an option with no arguments
- * @longopt: the name of the argument (eg. "foo" for "--foo"), or NULL.
- * @shortopt: the character of the argument (eg. 'f' for "-f"), or 0.
+ * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
  * @cb: the callback when the option is found.
  * @arg: the argument to hand to @cb.
  * @desc: the verbose desction of the option (for opt_usage()), or NULL.
  * @cb: the callback when the option is found.
  * @arg: the argument to hand to @cb.
  * @desc: the verbose desction of the option (for opt_usage()), or NULL.
@@ -139,16 +127,16 @@ void opt_register_table(const struct opt_table table[], const char *desc);
  * or "char *cb(void *)", where "type" is the type of the @arg
  * argument.
  *
  * or "char *cb(void *)", where "type" is the type of the @arg
  * argument.
  *
- * At least one of @longopt and @shortopt must be non-zero.  If the
- * @cb returns false, opt_parse() will stop parsing and return false.
+ * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
+ * returned string to form an error message for errlog(), free() the
+ * string and return false.
  */
  */
-#define opt_register_noarg(longopt, shortopt, cb, arg, desc)   \
-       _opt_register((longopt), (shortopt), OPT_CB_NOARG((cb), (arg)), (desc))
+#define opt_register_noarg(names, cb, arg, desc)                       \
+       _opt_register((names), OPT_CB_NOARG((cb), (arg)), (desc))
 
 /**
  * opt_register_arg - register an option with an arguments
 
 /**
  * opt_register_arg - register an option with an arguments
- * @longopt: the name of the argument (eg. "foo" for "--foo"), or NULL.
- * @shortopt: the character of the argument (eg. 'f' for "-f"), or 0.
+ * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
  * @cb: the callback when the option is found.
  * @show: the callback when the option is found.
  * @arg: the argument to hand to @cb.
  * @cb: the callback when the option is found.
  * @show: the callback when the option is found.
  * @arg: the argument to hand to @cb.
@@ -166,11 +154,11 @@ void opt_register_table(const struct opt_table table[], const char *desc);
  * @cb returns false, opt_parse() will stop parsing and return false.
  *
  * Example:
  * @cb returns false, opt_parse() will stop parsing and return false.
  *
  * Example:
- *     opt_register_arg("explode", 'e', explode_cb, NULL,
+ *     opt_register_arg("--explode", explode_cb, NULL,
  *                      "Make the machine explode (developers only)");
  */
  *                      "Make the machine explode (developers only)");
  */
-#define opt_register_arg(longopt, shortopt, cb, show, arg, desc)       \
-   _opt_register((longopt), (shortopt), OPT_CB_ARG((cb), (show), (arg)), (desc))
+#define opt_register_arg(names, cb, show, arg, desc)                   \
+       _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (desc))
 
 /**
  * opt_parse - parse arguments.
 
 /**
  * opt_parse - parse arguments.
@@ -216,12 +204,21 @@ char *opt_invalid_argument(const char *arg);
  * @extra: extra details to print after the initial command, or NULL.
  *
  * Creates a usage message, with the program name, arguments, some extra details
  * @extra: extra details to print after the initial command, or NULL.
  *
  * Creates a usage message, with the program name, arguments, some extra details
- * and a table of all the options with their descriptions.
+ * and a table of all the options with their descriptions.  If an option has
+ * description opt_hidden, it is not shown here.
  *
  * The result should be passed to free().
  */
 char *opt_usage(const char *argv0, const char *extra);
 
  *
  * The result should be passed to free().
  */
 char *opt_usage(const char *argv0, const char *extra);
 
+/**
+ * opt_hidden - string for undocumented options.
+ *
+ * This can be used as the desc parameter if you want an option not to be
+ * shown by opt_usage().
+ */
+extern const char opt_hidden[];
+
 /* Standard helpers.  You can write your own: */
 /* Sets the @b to true. */
 char *opt_set_bool(bool *b);
 /* Standard helpers.  You can write your own: */
 /* Sets the @b to true. */
 char *opt_set_bool(bool *b);
@@ -279,7 +276,7 @@ char *opt_usage_and_exit(const char *extra);
        (arg)
 
 /* Non-typesafe register function. */
        (arg)
 
 /* Non-typesafe register function. */
-void _opt_register(const char *longopt, char shortopt, enum opt_flags flags,
+void _opt_register(const char *names, enum opt_flags flags,
                   char *(*cb)(void *arg),
                   char *(*cb_arg)(const char *optarg, void *arg),
                   void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
                   char *(*cb)(void *arg),
                   char *(*cb_arg)(const char *optarg, void *arg),
                   void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
index b1a189234974bb8250102cd3a073f00b6bb503e2..5d9eca23a0ddf55b81f812009b2b09e0fed104d7 100644 (file)
@@ -2,10 +2,13 @@
 #define CCAN_OPT_PRIVATE_H
 
 extern struct opt_table *opt_table;
 #define CCAN_OPT_PRIVATE_H
 
 extern struct opt_table *opt_table;
-extern unsigned int opt_count;
+extern unsigned int opt_count, opt_num_short, opt_num_short_arg, opt_num_long;
 
 extern const char *opt_argv0;
 
 
 extern const char *opt_argv0;
 
-#define subtable_of(entry) ((struct opt_table *)((entry)->longopt))
+#define subtable_of(entry) ((struct opt_table *)((entry)->names))
+
+const char *first_sopt(unsigned *i);
+const char *next_sopt(const char *names, unsigned *i);
 
 #endif /* CCAN_OPT_PRIVATE_H */
 
 #endif /* CCAN_OPT_PRIVATE_H */
index 8e1a751fc2da88b3a651f9e51f65747408a6b624..f8041ead39d1f73c3b095e0fb890db2ec886453a 100644 (file)
@@ -20,7 +20,7 @@ static void reset_options(void)
 {
        free(opt_table);
        opt_table = NULL;
 {
        free(opt_table);
        opt_table = NULL;
-       opt_count = 0;
+       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
 }
 
 static char *output = NULL;
 }
 
 static char *output = NULL;
@@ -54,10 +54,10 @@ int main(int argc, char *argv[])
        {
                bool arg = false;
                reset_options();
        {
                bool arg = false;
                reset_options();
-               opt_register_noarg(NULL, 'a', opt_set_bool, &arg, NULL);
+               opt_register_noarg("-a", opt_set_bool, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(arg);
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(arg);
-               opt_register_arg(NULL, 'b', opt_set_bool_arg, NULL, &arg, NULL);
+               opt_register_arg("-b", opt_set_bool_arg, NULL, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-b", "no", NULL));
                ok1(!arg);
                ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
                ok1(parse_args(&argc, &argv, "-b", "no", NULL));
                ok1(!arg);
                ok1(parse_args(&argc, &argv, "-b", "yes", NULL));
@@ -71,10 +71,10 @@ int main(int argc, char *argv[])
        {
                bool arg = true;
                reset_options();
        {
                bool arg = true;
                reset_options();
-               opt_register_noarg(NULL, 'a', opt_set_invbool, &arg, NULL);
+               opt_register_noarg("-a", opt_set_invbool, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(!arg);
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(!arg);
-               opt_register_arg(NULL, 'b', opt_set_invbool_arg, NULL,
+               opt_register_arg("-b", opt_set_invbool_arg, NULL,
                                 &arg, NULL);
                ok1(parse_args(&argc, &argv, "-b", "no", NULL));
                ok1(arg);
                                 &arg, NULL);
                ok1(parse_args(&argc, &argv, "-b", "no", NULL));
                ok1(arg);
@@ -89,7 +89,7 @@ int main(int argc, char *argv[])
        {
                char *arg = (char *)"wrong";
                reset_options();
        {
                char *arg = (char *)"wrong";
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_charp, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_charp, NULL, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", "string", NULL));
                ok1(strcmp(arg, "string") == 0);
        }
                ok1(parse_args(&argc, &argv, "-a", "string", NULL));
                ok1(strcmp(arg, "string") == 0);
        }
@@ -97,7 +97,7 @@ int main(int argc, char *argv[])
        {
                int arg = 1000;
                reset_options();
        {
                int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_intval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_intval, NULL, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -114,7 +114,7 @@ int main(int argc, char *argv[])
        {
                unsigned int arg = 1000;
                reset_options();
        {
                unsigned int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_uintval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_uintval, NULL, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -127,7 +127,7 @@ int main(int argc, char *argv[])
        {
                long int arg = 1000;
                reset_options();
        {
                long int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_longval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_longval, NULL, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -146,7 +146,7 @@ int main(int argc, char *argv[])
        {
                unsigned long int arg = 1000;
                reset_options();
        {
                unsigned long int arg = 1000;
                reset_options();
-               opt_register_arg(NULL, 'a', opt_set_ulongval, NULL, &arg, NULL);
+               opt_register_arg("-a", opt_set_ulongval, NULL, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
                ok1(parse_args(&argc, &argv, "-a", "9999", NULL));
                ok1(arg == 9999);
                ok1(!parse_args(&argc, &argv, "-a", "-9999", NULL));
@@ -164,7 +164,7 @@ int main(int argc, char *argv[])
        {
                int arg = 1000;
                reset_options();
        {
                int arg = 1000;
                reset_options();
-               opt_register_noarg(NULL, 'a', opt_inc_intval, &arg, NULL);
+               opt_register_noarg("-a", opt_inc_intval, &arg, NULL);
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(arg == 1001);
                ok1(parse_args(&argc, &argv, "-a", "-a", NULL));
                ok1(parse_args(&argc, &argv, "-a", NULL));
                ok1(arg == 1001);
                ok1(parse_args(&argc, &argv, "-a", "-a", NULL));
@@ -177,7 +177,7 @@ int main(int argc, char *argv[])
        {
                int exitval;
                reset_options();
        {
                int exitval;
                reset_options();
-               opt_register_noarg(NULL, 'a',
+               opt_register_noarg("-a",
                                   opt_version_and_exit, "1.2.3", NULL);
                exitval = setjmp(exited);
                if (exitval == 0) {
                                   opt_version_and_exit, "1.2.3", NULL);
                exitval = setjmp(exited);
                if (exitval == 0) {
@@ -195,7 +195,7 @@ int main(int argc, char *argv[])
        {
                int exitval;
                reset_options();
        {
                int exitval;
                reset_options();
-               opt_register_noarg(NULL, 'a',
+               opt_register_noarg("-a",
                                   opt_usage_and_exit, "[args]", NULL);
                exitval = setjmp(exited);
                if (exitval == 0) {
                                   opt_usage_and_exit, "[args]", NULL);
                exitval = setjmp(exited);
                if (exitval == 0) {
diff --git a/ccan/opt/test/run-iter.c b/ccan/opt/test/run-iter.c
new file mode 100644 (file)
index 0000000..147470f
--- /dev/null
@@ -0,0 +1,76 @@
+#define _GNU_SOURCE
+#include <ccan/tap/tap.h>
+#include <stdarg.h>
+#include <setjmp.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include "utils.h"
+#include <ccan/opt/opt.c>
+#include <ccan/opt/usage.c>
+
+/* Test iterators. */
+int main(int argc, char *argv[])
+{
+       unsigned i, len;
+       const char *p;
+
+       plan_tests(37);
+       opt_register_table(subtables, NULL);
+
+       p = first_lopt(&i, &len);
+       ok1(i == 0);
+       ok1(len == 3);
+       ok1(strncmp(p, "jjj", len) == 0);
+       p = next_lopt(p, &i, &len);
+       ok1(i == 0);
+       ok1(len == 3);
+       ok1(strncmp(p, "lll", len) == 0);
+       p = next_lopt(p, &i, &len);
+       ok1(i == 1);
+       ok1(len == 3);
+       ok1(strncmp(p, "mmm", len) == 0);
+       p = next_lopt(p, &i, &len);
+       ok1(i == 5);
+       ok1(len == 3);
+       ok1(strncmp(p, "ddd", len) == 0);
+       p = next_lopt(p, &i, &len);
+       ok1(i == 6);
+       ok1(len == 3);
+       ok1(strncmp(p, "eee", len) == 0);
+       p = next_lopt(p, &i, &len);
+       ok1(i == 7);
+       ok1(len == 3);
+       ok1(strncmp(p, "ggg", len) == 0);
+       p = next_lopt(p, &i, &len);
+       ok1(i == 8);
+       ok1(len == 3);
+       ok1(strncmp(p, "hhh", len) == 0);
+       p = next_lopt(p, &i, &len);
+       ok1(!p);
+
+       p = first_sopt(&i);
+       ok1(i == 0);
+       ok1(*p == 'j');
+       p = next_sopt(p, &i);
+       ok1(i == 0);
+       ok1(*p == 'l');
+       p = next_sopt(p, &i);
+       ok1(i == 1);
+       ok1(*p == 'm');
+       p = next_sopt(p, &i);
+       ok1(i == 2);
+       ok1(*p == 'a');
+       p = next_sopt(p, &i);
+       ok1(i == 3);
+       ok1(*p == 'b');
+       p = next_sopt(p, &i);
+       ok1(i == 7);
+       ok1(*p == 'g');
+       p = next_sopt(p, &i);
+       ok1(i == 8);
+       ok1(*p == 'h');
+       p = next_sopt(p, &i);
+       ok1(!p);
+
+       return exit_status();
+}
diff --git a/ccan/opt/test/run-no-options.c b/ccan/opt/test/run-no-options.c
new file mode 100644 (file)
index 0000000..98ab8d9
--- /dev/null
@@ -0,0 +1,28 @@
+/* Make sure we still work with no options registered */
+#include <ccan/tap/tap.h>
+#include <stdlib.h>
+#include <ccan/opt/opt.c>
+#include <ccan/opt/usage.c>
+#include "utils.h"
+
+int main(int argc, char *argv[])
+{
+       const char *myname = argv[0];
+
+       plan_tests(7);
+
+       /* Simple short arg.*/
+       ok1(!parse_args(&argc, &argv, "-a", NULL));
+       /* Simple long arg.*/
+       ok1(!parse_args(&argc, &argv, "--aaa", NULL));
+
+       /* Extra arguments preserved. */
+       ok1(parse_args(&argc, &argv, "extra", "args", NULL));
+       ok1(argc == 3);
+       ok1(argv[0] == myname);
+       ok1(strcmp(argv[1], "extra") == 0);
+       ok1(strcmp(argv[2], "args") == 0);
+
+       return exit_status();
+}
+
index 2e4ce40061e55b8425cdd44aa69fc6faaac71c64..821403444df7da6e207260ed54b3049265cfed25 100644 (file)
@@ -17,13 +17,14 @@ static char *my_cb(void *p)
 int main(int argc, char *argv[])
 {
        char *output;
 int main(int argc, char *argv[])
 {
        char *output;
-       plan_tests(18);
+
+       plan_tests(19);
        opt_register_table(subtables, NULL);
        opt_register_table(subtables, NULL);
-       opt_register_noarg("kkk", 'k', my_cb, NULL, "magic kkk option");
+       opt_register_noarg("--kkk/-k", my_cb, NULL, "magic kkk option");
        output = opt_usage("my name", "ExTrA Args");
        diag("%s", output);
        ok1(strstr(output, "Usage: my name"));
        output = opt_usage("my name", "ExTrA Args");
        diag("%s", output);
        ok1(strstr(output, "Usage: my name"));
-       ok1(strstr(output, "--jjj/-j <arg>"));
+       ok1(strstr(output, "--jjj/-j/--lll/-l <arg>"));
        ok1(strstr(output, "ExTrA Args"));
        ok1(strstr(output, "-a "));
        ok1(strstr(output, " Description of a\n"));
        ok1(strstr(output, "ExTrA Args"));
        ok1(strstr(output, "-a "));
        ok1(strstr(output, " Description of a\n"));
@@ -34,13 +35,15 @@ int main(int argc, char *argv[])
        ok1(strstr(output, "--eee <arg> "));
        ok1(strstr(output, " (default: eee)\n"));
        ok1(strstr(output, "long table options:\n"));
        ok1(strstr(output, "--eee <arg> "));
        ok1(strstr(output, " (default: eee)\n"));
        ok1(strstr(output, "long table options:\n"));
-       /* This table is hidden. */
-       ok1(!strstr(output, "--ggg/-g "));
-       ok1(!strstr(output, " Description of ggg\n"));
-       ok1(!strstr(output, "--hhh/-h <arg>"));
-       ok1(!strstr(output, " Description of hhh\n"));
+       ok1(strstr(output, "--ggg/-g "));
+       ok1(strstr(output, " Description of ggg\n"));
+       ok1(strstr(output, "-h/--hhh <arg>"));
+       ok1(strstr(output, " Description of hhh\n"));
        ok1(strstr(output, "--kkk/-k"));
        ok1(strstr(output, "magic kkk option"));
        ok1(strstr(output, "--kkk/-k"));
        ok1(strstr(output, "magic kkk option"));
+       /* This entry is hidden. */
+       ok1(!strstr(output, "--mmm/-m"));
+
        free(output);
 
        return exit_status();
        free(output);
 
        return exit_status();
index 26c69dff628e918443296e34e5aac01896b01002..4eb78c936a44eeb4cdcad26a634c4f4dd3c52615 100644 (file)
@@ -8,7 +8,7 @@ static void reset_options(void)
 {
        free(opt_table);
        opt_table = NULL;
 {
        free(opt_table);
        opt_table = NULL;
-       opt_count = 0;
+       opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
        free(err_output);
        err_output = NULL;
 }
        free(err_output);
        err_output = NULL;
 }
@@ -20,7 +20,7 @@ int main(int argc, char *argv[])
        plan_tests(148);
 
        /* Simple short arg.*/
        plan_tests(148);
 
        /* Simple short arg.*/
-       opt_register_noarg(NULL, 'a', test_noarg, NULL, NULL);
+       opt_register_noarg("-a", test_noarg, NULL, NULL);
        ok1(parse_args(&argc, &argv, "-a", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
        ok1(parse_args(&argc, &argv, "-a", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
@@ -28,7 +28,7 @@ int main(int argc, char *argv[])
        ok1(test_cb_called == 1);
 
        /* Simple long arg. */
        ok1(test_cb_called == 1);
 
        /* Simple long arg. */
-       opt_register_noarg("aaa", 0, test_noarg, NULL, NULL);
+       opt_register_noarg("--aaa", test_noarg, NULL, NULL);
        ok1(parse_args(&argc, &argv, "--aaa", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
        ok1(parse_args(&argc, &argv, "--aaa", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
@@ -36,7 +36,7 @@ int main(int argc, char *argv[])
        ok1(test_cb_called == 2);
 
        /* Both long and short args. */
        ok1(test_cb_called == 2);
 
        /* Both long and short args. */
-       opt_register_noarg("aaa", 'a', test_noarg, NULL, NULL);
+       opt_register_noarg("--aaa/-a", test_noarg, NULL, NULL);
        ok1(parse_args(&argc, &argv, "--aaa", "-a", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
        ok1(parse_args(&argc, &argv, "--aaa", "-a", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
@@ -54,7 +54,7 @@ int main(int argc, char *argv[])
        /* Argument variants. */
        reset_options();
        test_cb_called = 0;
        /* Argument variants. */
        reset_options();
        test_cb_called = 0;
-       opt_register_arg("aaa", 'a', test_arg, NULL, "aaa", NULL);
+       opt_register_arg("-a/--aaa", test_arg, NULL, "aaa", NULL);
        ok1(parse_args(&argc, &argv, "--aaa", "aaa", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
        ok1(parse_args(&argc, &argv, "--aaa", "aaa", NULL));
        ok1(argc == 1);
        ok1(argv[0] == myname);
@@ -201,7 +201,7 @@ int main(int argc, char *argv[])
        reset_options();
 
        /* glibc's getopt does not handle ? with arguments. */
        reset_options();
 
        /* glibc's getopt does not handle ? with arguments. */
-       opt_register_noarg(NULL, '?', test_noarg, NULL, NULL);
+       opt_register_noarg("-?", test_noarg, NULL, NULL);
        ok1(parse_args(&argc, &argv, "-?", NULL));
        ok1(test_cb_called == 1);
        ok1(parse_args(&argc, &argv, "-a", NULL) == false);
        ok1(parse_args(&argc, &argv, "-?", NULL));
        ok1(test_cb_called == 1);
        ok1(parse_args(&argc, &argv, "-a", NULL) == false);
index a012d40b8e5958456a415f414a78b6514228c357..6870af741b188034a5a5337ae3de66c880189d71 100644 (file)
@@ -70,33 +70,35 @@ bool parse_args(int *argc, char ***argv, ...)
 
 struct opt_table short_table[] = {
        /* Short opts, different args. */
 
 struct opt_table short_table[] = {
        /* Short opts, different args. */
-       { OPT_WITHOUT_ARG(NULL, 'a', test_noarg, "a"), "Description of a" },
-       { OPT_WITH_ARG(NULL, 'b', test_arg, show_arg, "b"), "Description of b" },
+       { OPT_WITHOUT_ARG("-a", test_noarg, "a"), "Description of a" },
+       { OPT_WITH_ARG("-b", test_arg, show_arg, "b"), "Description of b" },
        OPT_ENDTABLE
 };
 
 struct opt_table long_table[] = {
        /* Long opts, different args. */
        OPT_ENDTABLE
 };
 
 struct opt_table long_table[] = {
        /* Long opts, different args. */
-       { OPT_WITHOUT_ARG("ddd", 0, test_noarg, "ddd"), "Description of ddd" },
-       { OPT_WITH_ARG("eee", 0, test_arg, show_arg, "eee"), },
+       { OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd"), "Description of ddd" },
+       { OPT_WITH_ARG("--eee", test_arg, show_arg, "eee"), },
        OPT_ENDTABLE
 };
 
 struct opt_table long_and_short_table[] = {
        /* Short and long, different args. */
        OPT_ENDTABLE
 };
 
 struct opt_table long_and_short_table[] = {
        /* Short and long, different args. */
-       { OPT_WITHOUT_ARG("ggg", 'g', test_noarg, "ggg"),
+       { OPT_WITHOUT_ARG("--ggg/-g", test_noarg, "ggg"),
          "Description of ggg" },
          "Description of ggg" },
-       { OPT_WITH_ARG("hhh", 'h', test_arg, NULL, "hhh"),
+       { OPT_WITH_ARG("-h/--hhh", test_arg, NULL, "hhh"),
          "Description of hhh"},
        OPT_ENDTABLE
 };
 
 /* Sub-table test. */
 struct opt_table subtables[] = {
          "Description of hhh"},
        OPT_ENDTABLE
 };
 
 /* Sub-table test. */
 struct opt_table subtables[] = {
-       /* Short and long, no description */
-       { OPT_WITH_ARG("jjj", 'j', test_arg, show_arg, "jjj") },
+       /* Two short, and two long long, no description */
+       { OPT_WITH_ARG("--jjj/-j/--lll/-l", test_arg, show_arg, "jjj") },
+       /* Hidden option */
+       { OPT_WITH_ARG("--mmm/-m", test_arg, show_arg, "mmm"), opt_hidden },
        OPT_SUBTABLE(short_table, NULL),
        OPT_SUBTABLE(long_table, "long table options"),
        OPT_SUBTABLE(short_table, NULL),
        OPT_SUBTABLE(long_table, "long table options"),
-       OPT_SUBTABLE(long_and_short_table, opt_table_hidden),
+       OPT_SUBTABLE(long_and_short_table, NULL),
        OPT_ENDTABLE
 };
        OPT_ENDTABLE
 };
index d76c3b03ed317e3306f4ccad739ccdcac2fbdd9d..cbe1231a484fedf27e826a3485a1a333dd7314dd 100644 (file)
@@ -6,21 +6,16 @@
 #include "private.h"
 
 /* We only use this for pointer comparisons. */
 #include "private.h"
 
 /* We only use this for pointer comparisons. */
-const char opt_table_hidden[1];
+const char opt_hidden[1];
 
 static unsigned write_short_options(char *str)
 {
        unsigned int i, num = 0;
 
 static unsigned write_short_options(char *str)
 {
        unsigned int i, num = 0;
+       const char *p;
 
 
-       for (i = 0; i < opt_count; i++) {
-               if (opt_table[i].flags == OPT_SUBTABLE) {
-                       if (opt_table[i].desc == opt_table_hidden) {
-                               /* Skip these options. */
-                               i += (intptr_t)opt_table[i].arg - 1;
-                               continue;
-                       }
-               } else if (opt_table[i].shortopt)
-                       str[num++] = opt_table[i].shortopt;
+       for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
+               if (opt_table[i].desc != opt_hidden)
+                       str[num++] = *p;
        }
        return num;
 }
        }
        return num;
 }
@@ -35,7 +30,7 @@ char *opt_usage(const char *argv0, const char *extra)
 
        /* An overestimate of our length. */
        len = strlen("Usage: %s ") + strlen(argv0)
 
        /* An overestimate of our length. */
        len = strlen("Usage: %s ") + strlen(argv0)
-               + strlen("[-%.*s]") + opt_count + 1
+               + strlen("[-%.*s]") + opt_num_short + 1
                + strlen(" ") + strlen(extra)
                + strlen("\n");
 
                + strlen(" ") + strlen(extra)
                + strlen("\n");
 
@@ -43,10 +38,8 @@ char *opt_usage(const char *argv0, const char *extra)
                if (opt_table[i].flags == OPT_SUBTABLE) {
                        len += strlen("\n") + strlen(opt_table[i].desc)
                                + strlen(":\n");
                if (opt_table[i].flags == OPT_SUBTABLE) {
                        len += strlen("\n") + strlen(opt_table[i].desc)
                                + strlen(":\n");
-               } else {
-                       len += strlen("--%s/-%c") + strlen(" <arg>");
-                       if (opt_table[i].longopt)
-                               len += strlen(opt_table[i].longopt);
+               } else if (opt_table[i].desc != opt_hidden) {
+                       len += strlen(opt_table[i].names) + strlen(" <arg>");
                        if (opt_table[i].desc) {
                                len += strlen(OPT_SPACE_PAD)
                                        + strlen(opt_table[i].desc) + 1;
                        if (opt_table[i].desc) {
                                len += strlen(OPT_SPACE_PAD)
                                        + strlen(opt_table[i].desc) + 1;
@@ -78,23 +71,13 @@ char *opt_usage(const char *argv0, const char *extra)
        p += sprintf(p, "\n");
 
        for (i = 0; i < opt_count; i++) {
        p += sprintf(p, "\n");
 
        for (i = 0; i < opt_count; i++) {
+               if (opt_table[i].desc == opt_hidden)
+                       continue;
                if (opt_table[i].flags == OPT_SUBTABLE) {
                if (opt_table[i].flags == OPT_SUBTABLE) {
-                       if (opt_table[i].desc == opt_table_hidden) {
-                               /* Skip these options. */
-                               i += (intptr_t)opt_table[i].arg - 1;
-                               continue;
-                       }
                        p += sprintf(p, "%s:\n", opt_table[i].desc);
                        continue;
                }
                        p += sprintf(p, "%s:\n", opt_table[i].desc);
                        continue;
                }
-               if (opt_table[i].shortopt && opt_table[i].longopt)
-                       len = sprintf(p, "--%s/-%c",
-                                    opt_table[i].longopt,
-                                     opt_table[i].shortopt);
-               else if (opt_table[i].shortopt)
-                       len = sprintf(p, "-%c", opt_table[i].shortopt);
-               else
-                       len = sprintf(p, "--%s", opt_table[i].longopt);
+               len = sprintf(p, "%s", opt_table[i].names);
                if (opt_table[i].flags == OPT_HASARG)
                        len += sprintf(p + len, " <arg>");
                if (opt_table[i].desc || opt_table[i].show)
                if (opt_table[i].flags == OPT_HASARG)
                        len += sprintf(p + len, " <arg>");
                if (opt_table[i].desc || opt_table[i].show)