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;
33 for (i = 0; i < opt_count; i++) {
34 if (opt_table[i].cb == (void *)opt_usage_and_exit
35 && opt_table[i].arg) {
36 extra = opt_table[i].arg;
42 /* An overestimate of our length. */
43 len = strlen("Usage: %s ") + strlen(argv0)
44 + strlen("[-%.*s]") + opt_num_short + 1
45 + strlen(" ") + strlen(extra)
48 for (i = 0; i < opt_count; i++) {
49 if (opt_table[i].type == OPT_SUBTABLE) {
50 len += strlen("\n") + strlen(opt_table[i].desc)
52 } else if (opt_table[i].desc != opt_hidden) {
53 len += strlen(opt_table[i].names) + strlen(" <arg>");
54 len += strlen(OPT_SPACE_PAD)
55 + strlen(opt_table[i].desc) + 1;
56 if (opt_table[i].show) {
57 len += strlen("(default: %s)")
58 + OPT_SHOW_LEN + sizeof("...");
64 p = ret = malloc(len);
68 p += sprintf(p, "Usage: %s", argv0);
69 p += sprintf(p, " [-");
70 num = write_short_options(p);
75 /* Remove start of single-entry options */
79 p += sprintf(p, " %s", extra);
80 p += sprintf(p, "\n");
82 for (i = 0; i < opt_count; i++) {
83 if (opt_table[i].desc == opt_hidden)
85 if (opt_table[i].type == OPT_SUBTABLE) {
86 p += sprintf(p, "%s:\n", opt_table[i].desc);
89 len = sprintf(p, "%s", opt_table[i].names);
90 if (opt_table[i].type == OPT_HASARG
91 && !strchr(opt_table[i].names, ' ')
92 && !strchr(opt_table[i].names, '='))
93 len += sprintf(p + len, " <arg>");
94 len += sprintf(p + len, "%.*s",
95 len < strlen(OPT_SPACE_PAD)
96 ? (unsigned)strlen(OPT_SPACE_PAD) - len : 1,
99 len += sprintf(p + len, "%s", opt_table[i].desc);
100 if (opt_table[i].show) {
101 char buf[OPT_SHOW_LEN + sizeof("...")];
102 strcpy(buf + OPT_SHOW_LEN, "...");
103 opt_table[i].show(buf, opt_table[i].arg);
104 len += sprintf(p + len, " (default: %s)", buf);
107 p += sprintf(p, "\n");