opt: functions to show integer values with kMGTPE suffixes
[ccan] / ccan / opt / usage.c
index b7381bc7378c6673c742d6dc23b0c42f211e8265..f321ed2949a8bfb7cd5cdbce5955780dba1cd233 100644 (file)
@@ -1,3 +1,4 @@
+/* Licensed under GPLv3+ - see LICENSE file for details */
 #include <ccan/opt/opt.h>
 #include <string.h>
 #include <stdlib.h>
@@ -28,6 +29,17 @@ char *opt_usage(const char *argv0, const char *extra)
        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].u.carg) {
+                               extra = opt_table[i].u.carg;
+                               break;
+                       }
+               }
+       }
+
        /* An overestimate of our length. */
        len = strlen("Usage: %s ") + strlen(argv0)
                + strlen("[-%.*s]") + opt_num_short + 1
@@ -82,14 +94,14 @@ char *opt_usage(const char *argv0, const char *extra)
                        len += sprintf(p + len, " <arg>");
                len += sprintf(p + len, "%.*s",
                               len < strlen(OPT_SPACE_PAD)
-                              ? strlen(OPT_SPACE_PAD) - len : 1,
+                              ? (unsigned)strlen(OPT_SPACE_PAD) - len : 1,
                               OPT_SPACE_PAD);
 
                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);
+                       opt_table[i].show(buf, opt_table[i].u.arg);
                        len += sprintf(p + len, " (default: %s)", buf);
                }
                p += len;