opt: Put actual options inside names.
[ccan] / ccan / opt / usage.c
index d76c3b03ed317e3306f4ccad739ccdcac2fbdd9d..cbe1231a484fedf27e826a3485a1a333dd7314dd 100644 (file)
@@ -6,21 +6,16 @@
 #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;
+       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;
 }
@@ -35,7 +30,7 @@ char *opt_usage(const char *argv0, const char *extra)
 
        /* 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");
 
@@ -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");
-               } 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;
@@ -78,23 +71,13 @@ char *opt_usage(const char *argv0, const char *extra)
        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].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;
                }
-               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)