]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/usage.c
opt: Fix -Wmissing-field-initializers warning
[ccan] / ccan / opt / usage.c
index d76c3b03ed317e3306f4ccad739ccdcac2fbdd9d..60e1586861adc0981a5bf0eaf00bc63bcff450a4 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;
 }
@@ -33,24 +28,31 @@ char *opt_usage(const char *argv0, const char *extra)
        unsigned int i, num, len;
        char *ret, *p;
 
        unsigned int i, num, len;
        char *ret, *p;
 
+       if (!extra) {
+               extra = "";
+               for (i = 0; i < opt_count; i++) {
+                       if (opt_table[i].cb == (void *)opt_usage_and_exit
+                           && opt_table[i].arg) {
+                               extra = opt_table[i].arg;
+                               break;
+                       }
+               }
+       }
+
        /* 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");
 
        for (i = 0; i < opt_count; i++) {
                + strlen(" ") + strlen(extra)
                + strlen("\n");
 
        for (i = 0; i < opt_count; i++) {
-               if (opt_table[i].flags == OPT_SUBTABLE) {
+               if (opt_table[i].type == OPT_SUBTABLE) {
                        len += strlen("\n") + strlen(opt_table[i].desc)
                                + strlen(":\n");
                        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);
-                       if (opt_table[i].desc) {
-                               len += strlen(OPT_SPACE_PAD)
-                                       + strlen(opt_table[i].desc) + 1;
-                       }
+               } else if (opt_table[i].desc != opt_hidden) {
+                       len += strlen(opt_table[i].names) + strlen(" <arg>");
+                       len += strlen(OPT_SPACE_PAD)
+                               + strlen(opt_table[i].desc) + 1;
                        if (opt_table[i].show) {
                                len += strlen("(default: %s)")
                                        + OPT_SHOW_LEN + sizeof("...");
                        if (opt_table[i].show) {
                                len += strlen("(default: %s)")
                                        + OPT_SHOW_LEN + sizeof("...");
@@ -78,39 +80,28 @@ 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].flags == OPT_SUBTABLE) {
-                       if (opt_table[i].desc == opt_table_hidden) {
-                               /* Skip these options. */
-                               i += (intptr_t)opt_table[i].arg - 1;
-                               continue;
-                       }
+               if (opt_table[i].desc == opt_hidden)
+                       continue;
+               if (opt_table[i].type == OPT_SUBTABLE) {
                        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);
-               if (opt_table[i].flags == OPT_HASARG)
+               len = sprintf(p, "%s", opt_table[i].names);
+               if (opt_table[i].type == OPT_HASARG
+                   && !strchr(opt_table[i].names, ' ')
+                   && !strchr(opt_table[i].names, '='))
                        len += sprintf(p + len, " <arg>");
                        len += sprintf(p + len, " <arg>");
-               if (opt_table[i].desc || opt_table[i].show)
-                       len += sprintf(p + len, "%.*s",
-                                      len < strlen(OPT_SPACE_PAD)
-                                      ? strlen(OPT_SPACE_PAD) - len : 1,
-                                      OPT_SPACE_PAD);
+               len += sprintf(p + len, "%.*s",
+                              len < strlen(OPT_SPACE_PAD)
+                              ? (unsigned)strlen(OPT_SPACE_PAD) - len : 1,
+                              OPT_SPACE_PAD);
 
 
-               if (opt_table[i].desc)
-                       len += sprintf(p + len, "%s", opt_table[i].desc);
+               len += sprintf(p + len, "%s", opt_table[i].desc);
                if (opt_table[i].show) {
                        char buf[OPT_SHOW_LEN + sizeof("...")];
                        strcpy(buf + OPT_SHOW_LEN, "...");
                        opt_table[i].show(buf, opt_table[i].arg);
                if (opt_table[i].show) {
                        char buf[OPT_SHOW_LEN + sizeof("...")];
                        strcpy(buf + OPT_SHOW_LEN, "...");
                        opt_table[i].show(buf, opt_table[i].arg);
-                       len += sprintf(p + len, "%s(default: %s)",
-                                      opt_table[i].desc ? " " : "", buf);
+                       len += sprintf(p + len, " (default: %s)", buf);
                }
                p += len;
                p += sprintf(p, "\n");
                }
                p += len;
                p += sprintf(p, "\n");