]> mj.ucw.cz Git - libucw.git/blob - ucw/opt-help.c
tableprinter: update of documentation
[libucw.git] / ucw / opt-help.c
1 /*
2  *      UCW Library -- Formatting of command-line option help
3  *
4  *      (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
5  *      (c) 2014 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include <ucw/lib.h>
12 #include <ucw/opt.h>
13 #include <ucw/opt-internal.h>
14 #include <ucw/mempool.h>
15 #include <ucw/gary.h>
16
17 #include <string.h>
18
19 struct help {
20   struct mempool *pool;
21   struct help_line *lines;                      // A growing array of lines
22 };
23
24 struct help_line {
25   const char *extra;
26   char *fields[3];
27 };
28
29 static void opt_help_scan_item(struct help *h, struct opt_precomputed *opt)
30 {
31   struct opt_item *item = opt->item;
32
33   if (!item->help)
34     return;
35
36   if (item->cls == OPT_CL_HELP) {
37     struct help_line *l = GARY_PUSH(h->lines);
38     l->extra = item->help;
39     return;
40   }
41
42   if (item->letter >= OPT_POSITIONAL_TAIL)
43     return;
44
45   struct help_line *first = GARY_PUSH(h->lines);
46   char *text = mp_strdup(h->pool, item->help);
47   struct help_line *l = first;
48   while (text) {
49     char *eol = strchr(text, '\n');
50     if (eol)
51       *eol++ = 0;
52
53     int field = (l == first ? 1 : 0);
54     char *f = text;
55     while (f) {
56       char *tab = strchr(f, '\t');
57       if (tab)
58         *tab++ = 0;
59       if (field < 3)
60         l->fields[field++] = f;
61       f = tab;
62     }
63
64     text = eol;
65     if (text)
66       l = GARY_PUSH(h->lines);
67   }
68
69   if (item->name) {
70     char *val = first->fields[1] ? : "";
71     if (opt->flags & OPT_REQUIRED_VALUE)
72       val = mp_printf(h->pool, "=%s", val);
73     else if (!(opt->flags & OPT_NO_VALUE))
74       val = mp_printf(h->pool, "[=%s]", val);
75     first->fields[1] = mp_printf(h->pool, "--%s%s", item->name, val);
76   }
77
78   if (item->letter) {
79     if (item->name)
80       first->fields[0] = mp_printf(h->pool, "-%c, ", item->letter);
81     else {
82       char *val = first->fields[1] ? : "";
83       if (!(opt->flags & OPT_REQUIRED_VALUE) && !(opt->flags & OPT_NO_VALUE))
84         val = mp_printf(h->pool, "[%s]", val);
85       first->fields[0] = mp_printf(h->pool, "-%c%s", item->letter, val);
86       first->fields[1] = NULL;
87     }
88   }
89 }
90
91 static void opt_help_scan(struct help *h, const struct opt_section *sec)
92 {
93   for (struct opt_item * item = sec->opt; item->cls != OPT_CL_END; item++) {
94     if (item->cls == OPT_CL_SECTION)
95       opt_help_scan(h, item->u.section);
96     else if (item->cls == OPT_CL_HOOK)
97       ;
98     else {
99       struct opt_precomputed opt;
100       opt_precompute(&opt, item);
101       opt_help_scan_item(h, &opt);
102     }
103   }
104 }
105
106 void opt_help(const struct opt_section * sec) {
107   // Prepare help text
108   struct help h;
109   h.pool = mp_new(4096);
110   GARY_INIT_ZERO(h.lines, 0);
111   opt_help_scan(&h, sec);
112
113   // Calculate natural width of each column
114   uint n = GARY_SIZE(h.lines);
115   uint widths[3] = { 0, 0, 0 };
116   for (uint i=0; i<n; i++) {
117     struct help_line *l = &h.lines[i];
118     for (uint f=0; f<3; f++) {
119       if (!l->fields[f])
120         l->fields[f] = "";
121       uint w = strlen(l->fields[f]);
122       widths[f] = MAX(widths[f], w);
123     }
124   }
125   if (widths[0] > 4) {
126     /*
127      *  This is tricky: if there are short options, which have an argument,
128      *  but no long variant, we are willing to let column 0 overflow to column 1.
129      */
130     widths[1] = MAX(widths[1], widths[0] - 4);
131     widths[0] = 4;
132   }
133   widths[1] += 4;
134
135   // Print columns
136   for (uint i=0; i<n; i++) {
137     struct help_line *l = &h.lines[i];
138     if (l->extra)
139       puts(l->extra);
140     else {
141       int t = 0;
142       for (uint f=0; f<3; f++) {
143         t += widths[f];
144         t -= printf("%s", l->fields[f]);
145         while (t > 0) {
146           putchar(' ');
147           t--;
148         }
149       }
150       putchar('\n');
151     }
152   }
153
154   // Clean up
155   GARY_FREE(h.lines);
156   mp_delete(h.pool);
157 }
158
159 void opt_handle_help(struct opt_item * opt UNUSED, const char * value UNUSED, void * data)
160 {
161   struct opt_context *oc = data;
162   opt_help(oc->options);
163   exit(0);
164 }