1 #include <ccan/opt/opt.h>
8 /* We only use this for pointer comparisons. */
9 const char opt_table_hidden[1];
11 static unsigned write_short_options(char *str)
13 unsigned int i, num = 0;
15 for (i = 0; i < opt_count; i++) {
16 if (opt_table[i].flags == OPT_SUBTABLE) {
17 if (opt_table[i].desc == opt_table_hidden) {
18 /* Skip these options. */
19 i += (intptr_t)opt_table[i].arg - 1;
22 } else if (opt_table[i].shortopt)
23 str[num++] = opt_table[i].shortopt;
28 /* FIXME: Get all purdy. */
29 char *opt_usage(const char *argv0, const char *extra)
31 unsigned int i, num, len;
34 /* An overestimate of our length. */
35 len = strlen("Usage: %s ") + strlen(argv0)
36 + strlen("[-%.*s]") + opt_count + 1
37 + strlen(" ") + strlen(extra)
40 for (i = 0; i < opt_count; i++) {
41 if (opt_table[i].flags == OPT_SUBTABLE) {
42 len += strlen("\n") + strlen(opt_table[i].desc)
45 len += strlen("--%s/-%c") + strlen(" <arg>");
46 if (opt_table[i].longopt)
47 len += strlen(opt_table[i].longopt);
48 if (opt_table[i].desc)
49 len += 20 + strlen(opt_table[i].desc);
54 p = ret = malloc(len);
58 p += sprintf(p, "Usage: %s", argv0);
59 p += sprintf(p, " [-");
60 num = write_short_options(p);
65 /* Remove start of single-entry options */
69 p += sprintf(p, " %s", extra);
70 p += sprintf(p, "\n");
72 for (i = 0; i < opt_count; i++) {
73 if (opt_table[i].flags == OPT_SUBTABLE) {
74 if (opt_table[i].desc == opt_table_hidden) {
75 /* Skip these options. */
76 i += (intptr_t)opt_table[i].arg - 1;
79 p += sprintf(p, "%s:\n", opt_table[i].desc);
82 if (opt_table[i].shortopt && opt_table[i].longopt)
83 len = sprintf(p, "--%s/-%c",
85 opt_table[i].shortopt);
86 else if (opt_table[i].shortopt)
87 len = sprintf(p, "-%c", opt_table[i].shortopt);
89 len = sprintf(p, "--%s", opt_table[i].longopt);
90 if (opt_table[i].flags == OPT_HASARG)
91 len += sprintf(p + len, " <arg>");
92 if (opt_table[i].desc) {
93 len += sprintf(p + len, "%.*s",
94 len < 20 ? 20 - len : 1,
96 len += sprintf(p + len, "%s", opt_table[i].desc);
99 p += sprintf(p, "\n");