1 #include <ccan/opt/opt.h>
8 /* We only use this for pointer comparisons. */
9 const char opt_hidden[1];
11 static unsigned write_short_options(char *str)
13 unsigned int i, num = 0;
16 for (p = first_sopt(&i); p; p = next_sopt(p, &i)) {
17 if (opt_table[i].desc != opt_hidden)
23 #define OPT_SPACE_PAD " "
25 /* FIXME: Get all purdy. */
26 char *opt_usage(const char *argv0, const char *extra)
28 unsigned int i, num, len;
31 /* An overestimate of our length. */
32 len = strlen("Usage: %s ") + strlen(argv0)
33 + strlen("[-%.*s]") + opt_num_short + 1
34 + strlen(" ") + strlen(extra)
37 for (i = 0; i < opt_count; i++) {
38 if (opt_table[i].type == OPT_SUBTABLE) {
39 len += strlen("\n") + strlen(opt_table[i].desc)
41 } else if (opt_table[i].desc != opt_hidden) {
42 len += strlen(opt_table[i].names) + strlen(" <arg>");
43 len += strlen(OPT_SPACE_PAD)
44 + strlen(opt_table[i].desc) + 1;
45 if (opt_table[i].show) {
46 len += strlen("(default: %s)")
47 + OPT_SHOW_LEN + sizeof("...");
53 p = ret = malloc(len);
57 p += sprintf(p, "Usage: %s", argv0);
58 p += sprintf(p, " [-");
59 num = write_short_options(p);
64 /* Remove start of single-entry options */
68 p += sprintf(p, " %s", extra);
69 p += sprintf(p, "\n");
71 for (i = 0; i < opt_count; i++) {
72 if (opt_table[i].desc == opt_hidden)
74 if (opt_table[i].type == OPT_SUBTABLE) {
75 p += sprintf(p, "%s:\n", opt_table[i].desc);
78 len = sprintf(p, "%s", opt_table[i].names);
79 if (opt_table[i].type == OPT_HASARG
80 && !strchr(opt_table[i].names, ' ')
81 && !strchr(opt_table[i].names, '='))
82 len += sprintf(p + len, " <arg>");
83 len += sprintf(p + len, "%.*s",
84 len < strlen(OPT_SPACE_PAD)
85 ? strlen(OPT_SPACE_PAD) - len : 1,
88 len += sprintf(p + len, "%s", opt_table[i].desc);
89 if (opt_table[i].show) {
90 char buf[OPT_SHOW_LEN + sizeof("...")];
91 strcpy(buf + OPT_SHOW_LEN, "...");
92 opt_table[i].show(buf, opt_table[i].arg);
93 len += sprintf(p + len, " (default: %s)", buf);
96 p += sprintf(p, "\n");