]> mj.ucw.cz Git - libucw.git/blob - ucw/opt.c
b9957648051a379c22a85d8cd03e5a7a30d0cecd
[libucw.git] / ucw / opt.c
1 /*
2  *      UCW Library -- Parsing of command line options
3  *
4  *      (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/opt.h>
12 #include <ucw/conf.h>
13 #include <ucw/stkstring.h>
14 #include <ucw/strtonum.h>
15
16 #include <alloca.h>
17 #include <math.h>
18
19 static void opt_failure(const char * mesg, ...) FORMAT_CHECK(printf,1,2) NONRET;
20 static void opt_failure(const char * mesg, ...) {
21   va_list args;
22   va_start(args, mesg);
23   vfprintf(stderr, mesg, args);
24   fprintf(stderr, "\n");
25   opt_usage();
26   exit(OPT_EXIT_BAD_ARGS);
27   va_end(args);
28 }
29
30 #define OPT_ADD_DEFAULT_ITEM_FLAGS(item, flags) \
31   do { \
32     if (item->letter >= 256) { \
33       if (flags & OPT_VALUE_FLAGS) \
34         flags &= ~OPT_VALUE_FLAGS; \
35       flags |= OPT_REQUIRED_VALUE; \
36     } \
37     if (!(flags & OPT_VALUE_FLAGS) && \
38         (item->cls == OPT_CL_CALL || item->cls == OPT_CL_USER)) { \
39       fprintf(stderr, "You MUST specify some of the value flags for the %c/%s item.\n", item->letter, item->name); \
40       ASSERT(0); \
41     } \
42     else if (!(flags & OPT_VALUE_FLAGS)) \
43       flags |= opt_default_value_flags[item->cls]; \
44   } while (0)
45 #define OPT_ITEM_FLAGS(item) ((item->flags & OPT_VALUE_FLAGS) ? item->flags : item->flags | opt_default_value_flags[item->cls])
46
47 const struct opt_section * opt_section_root;
48
49 #define FOREACHLINE(text) for (const char * begin = (text), * end = (text); (*end) && (end = strchrnul(begin, '\n')); begin = end+1)
50
51 void opt_help_internal(const struct opt_section * help) {
52   int sections_cnt = 0;
53   int lines_cnt = 0;
54
55   for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
56     if (item->flags & OPT_NO_HELP) continue;
57     if (item->cls == OPT_CL_SECTION) {
58       sections_cnt++;
59       continue;
60     }
61     if (!*(item->help)) {
62       lines_cnt++;
63       continue;
64     }
65     FOREACHLINE(item->help)
66       lines_cnt++;
67   }
68
69   struct opt_sectlist {
70     int pos;
71     struct opt_section * sect;
72   } sections[sections_cnt];
73   int s = 0;
74
75   const char *lines[lines_cnt][3];
76   memset(lines, 0, sizeof(lines));
77   int line = 0;
78
79   int linelengths[3] = { -1, -1, -1 };
80
81   for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
82     if (item->flags & OPT_NO_HELP) continue;
83
84     if (item->cls == OPT_CL_HELP) {
85       if (!*(item->help)) {
86         line++;
87         continue;
88       }
89 #define SPLITLINES(text) do { \
90       FOREACHLINE(text) { \
91         int cell = 0; \
92         for (const char * b = begin, * e = begin; (e < end) && (e = strchrnul(b, '\t')) && (e > end ? (e = end) : end); b = e+1) { \
93           lines[line][cell] = b; \
94           if (cell >= 2) \
95             break; \
96           else \
97             if (*e == '\t' && linelengths[cell] < (e - b)) \
98               linelengths[cell] = e-b; \
99           cell++; \
100         } \
101         line++; \
102       } } while (0)
103       SPLITLINES(item->help);
104       continue;
105     }
106
107     if (item->cls == OPT_CL_SECTION) {
108       sections[s++] = (struct opt_sectlist) { .pos = line, .sect = item->u.section };
109       continue;
110     }
111
112     uns valoff = strchrnul(item->help, '\t') - item->help;
113     uns eol = strchrnul(item->help, '\n') - item->help;
114     if (valoff > eol)
115       valoff = eol;
116 #define VAL(it) ((OPT_ITEM_FLAGS(it) & OPT_REQUIRED_VALUE) ? stk_printf("=%.*s", valoff, item->help)  : ((OPT_ITEM_FLAGS(it) & OPT_NO_VALUE) ? "" : stk_printf("(=%.*s)", valoff, item->help)))
117     if (item->name) {
118       lines[line][1] = stk_printf("--%s%s", item->name, VAL(item));
119       if (linelengths[1] < (int) strlen(lines[line][1]))
120         linelengths[1] = strlen(lines[line][1]);
121       lines[line][0] = "";
122       if (linelengths[0] < 0)
123         linelengths[0] = 0;
124     }
125     if (item->letter) {
126       lines[line][0] = stk_printf("-%c,", item->letter);
127       if (linelengths[0] < (int) strlen(lines[line][0]))
128         linelengths[0] = strlen(lines[line][0]);
129     }
130 #undef VAL
131
132     if (eol > valoff) {
133       lines[line][2] = item->help + valoff + 1;
134     }
135
136     line++;
137
138     if (*(item->help + eol))
139       SPLITLINES(item->help + eol + 1);
140   }
141 #undef SPLITLINES
142
143   s = 0;
144 #define FIELD(k) linelengths[k], MIN(strchrnul(lines[i][k], '\t')-lines[i][k],strchrnul(lines[i][k], '\n')-lines[i][k]), lines[i][k]
145 #define LASTFIELD(k) MIN(strchrnul(lines[i][k], '\t')-lines[i][k],strchrnul(lines[i][k], '\n')-lines[i][k]), lines[i][k]
146   for (int i=0;i<line;i++) {
147     while (s < sections_cnt && sections[s].pos == i) {
148       opt_help_internal(sections[s].sect);
149       s++;
150     }
151     if (lines[i][0] == NULL)
152       printf("\n");
153     else if (linelengths[0] == -1 || lines[i][1] == NULL)
154       printf("%.*s\n", LASTFIELD(0));
155     else if (linelengths[1] == -1 || lines[i][2] == NULL)
156       printf("%-*.*s  %.*s\n", FIELD(0), LASTFIELD(1));
157     else
158       printf("%-*.*s  %-*.*s  %.*s\n", FIELD(0), FIELD(1), LASTFIELD(2));
159   }
160   while (s < sections_cnt && sections[s].pos == line) {
161     opt_help_internal(sections[s].sect);
162     s++;
163   }
164 }
165
166 static int opt_positional_max = 0;
167 static int opt_positional_count = 0;
168
169 struct opt_precomputed {
170   struct opt_precomputed_option {
171     struct opt_item * item;
172     const char * name;
173     short flags;
174     short count;
175   } ** opts;
176   struct opt_precomputed_option ** shortopt;
177   short opt_count;
178 };
179
180 static struct opt_precomputed_option * opt_find_item_shortopt(int chr, struct opt_precomputed * pre) {
181   struct opt_precomputed_option * candidate = pre->shortopt[chr];
182   if (candidate->count++ && (candidate->flags & OPT_SINGLE))
183     opt_failure("Option %s appeared the second time.", candidate->name);
184   return candidate;
185 }
186
187 static struct opt_precomputed_option * opt_find_item_longopt(char * str, struct opt_precomputed * pre) {
188   uns len = strlen(str);
189   struct opt_precomputed_option * candidate = NULL;
190
191   for (int i=0; i<pre->opt_count; i++) {
192     if (!strncmp(pre->opts[i]->name, str, len)) {
193       if (strlen(pre->opts[i]->name) == len) {
194         if (pre->opts[i]->count++ && (pre->opts[i]->flags & OPT_SINGLE))
195           opt_failure("Option %s appeared the second time.", pre->opts[i]->name);
196
197         return pre->opts[i];
198       }
199       if (candidate)
200         opt_failure("Ambiguous prefix %s: Found matching %s and %s.", str, candidate->name, pre->opts[i]->name);
201       else
202         candidate = pre->opts[i];
203     }
204     if (!strncmp("no-", str, 3) && !strncmp(pre->opts[i]->name, str+3, len-3)) {
205       if (strlen(pre->opts[i]->name) == len-3) {
206         if (pre->opts[i]->count++ && (pre->opts[i]->flags & OPT_SINGLE))
207           opt_failure("Option %s appeared the second time.", pre->opts[i]->name);
208
209         return pre->opts[i];
210       }
211       if (candidate)
212         opt_failure("Ambiguous prefix %s: Found matching %s and %s.", str, candidate->name, pre->opts[i]->name);
213       else
214         candidate = pre->opts[i];
215     }
216   }
217
218   if (candidate)
219     return candidate;
220
221   opt_failure("Invalid option %s.", str);
222 }
223
224 #define OPT_NAME (longopt == 2 ? stk_printf("positional arg #%d", opt_positional_count) : (longopt == 1 ? stk_printf("--%s", opt->name) : stk_printf("-%c", item->letter)))
225 static void opt_parse_value(struct opt_precomputed_option * opt, char * value, int longopt) {
226   struct opt_item * item = opt->item;
227   switch (item->cls) {
228     case OPT_CL_BOOL:
229       if (!value || !strcasecmp(value, "y") || !strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcasecmp(value, "1"))
230         *((int *) item->ptr) = 1 ^ (!!(opt->flags & OPT_NEGATIVE));
231       else if (!strcasecmp(value, "n") || !strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcasecmp(value, "0"))
232         *((int *) item->ptr) = 0 ^ (!!(opt->flags & OPT_NEGATIVE));
233       else
234         opt_failure("Boolean argument for %s has a strange value. Supported (case insensitive): y/n, yes/no, true/false.", OPT_NAME);
235       break;
236     case OPT_CL_STATIC:
237       {
238         char * e = NULL;
239         switch (item->type) {
240           case CT_INT:
241             if (!value)
242               *((int*)item->ptr) = 0;
243             else
244               e = cf_parse_int(value, item->ptr);
245             if (e)
246               opt_failure("Integer value parsing failed for %s: %s", OPT_NAME, e);
247             break;
248           case CT_U64:
249             if (!value)
250               *((u64*)item->ptr) = 0;
251             else
252               e = cf_parse_u64(value, item->ptr);
253             if (e)
254               opt_failure("Unsigned 64-bit value parsing failed for %s: %s", OPT_NAME, e);
255             break;
256           case CT_DOUBLE:
257             if (!value)
258               *((double*)item->ptr) = NAN;
259             else
260               e = cf_parse_double(value, item->ptr);
261             if (e)
262               opt_failure("Double value parsing failed for %s: %s", OPT_NAME, e);
263             break;
264           case CT_IP:
265             if (!value)
266               e = cf_parse_ip("0.0.0.0", item->ptr);
267             else
268               e = cf_parse_ip(value, item->ptr);
269             if (e)
270               opt_failure("IP parsing failed for %s: %s", OPT_NAME, e);
271             break;
272           case CT_STRING:
273             if (!value)
274               item->ptr = NULL;
275             else
276               *((const char **) (item->ptr)) = xstrdup(value);
277             break;
278           default:
279             ASSERT(0);
280         }
281         break;
282       }
283     case OPT_CL_SWITCH:
284       if (*((int *)item->ptr) != -1)
285         opt_failure("Multiple switches: %s", OPT_NAME);
286       else
287         *((int *)item->ptr) = item->u.value;
288       break;
289     case OPT_CL_INC:
290       if (opt->flags & OPT_NEGATIVE)
291         (*((int *)item->ptr))--;
292       else
293         (*((int *)item->ptr))++;
294     case OPT_CL_CALL:
295       item->u.call(item, value, item->ptr);
296       break;
297     case OPT_CL_USER:
298       {
299         char * e = NULL;
300         e = item->u.utype->parser(value, item->ptr);
301         if (e)
302           opt_failure("User defined type value parsing failed for %s: %s", OPT_NAME, e);
303         break;
304       }
305     default:
306       ASSERT(0);
307   }
308 }
309 #undef OPT_NAME
310
311 static int opt_longopt(char ** argv, int index, struct opt_precomputed * pre) {
312   int eaten = 0;
313   char * name_in = argv[index] + 2; // skipping the -- on the beginning
314   uns pos = strchrnul(name_in, '=') - name_in;
315   struct opt_precomputed_option * opt = opt_find_item_longopt(strndupa(name_in, pos), pre);
316   char * value = NULL;
317
318   if (opt->item->cls == OPT_CL_BOOL && !strncmp(name_in, "no-", 3) && !strncmp(name_in+3, opt->item->name, pos-3))
319     value = "n";
320   else if (opt->flags & OPT_REQUIRED_VALUE) {
321     if (pos < strlen(name_in))
322       value = name_in + pos + 1;
323     else {
324       value = argv[index+1];
325       if (!value)
326         opt_failure("Argument --%s must have a value but nothing supplied.", opt->name);
327       eaten++;
328     }
329   }
330   else if (opt->flags & OPT_MAYBE_VALUE) {
331     if (pos < strlen(name_in))
332       value = name_in + pos + 1;
333   }
334   else {
335     if (pos < strlen(name_in))
336       opt_failure("Argument --%s must not have any value.", opt->name);
337   }
338   opt_parse_value(opt, value, 1);
339   return eaten;
340 }
341
342 static int opt_shortopt(char ** argv, int index, struct opt_precomputed * pre) {
343   int chr = 0;
344   struct opt_precomputed_option * opt;
345   while (argv[index][++chr] && (opt = opt_find_item_shortopt(argv[index][chr], pre))) {
346     if (opt->flags & OPT_NO_VALUE) {
347       opt_parse_value(opt, NULL, 0);
348       continue;
349     }
350     else if (opt->flags & OPT_REQUIRED_VALUE) {
351       if (chr == 1 && argv[index][2])
352         opt_parse_value(opt, argv[index] + 2, 0);
353       else if (argv[index][chr+1])
354         opt_failure("Option -%c must have a value but found inside a bunch of short opts.", opt->item->letter);
355       else if (!argv[index+1])
356         opt_failure("Option -%c must have a value but nothing supplied.", opt->item->letter);
357       else {
358         opt_parse_value(opt, argv[index+1], 0);
359         return 1;
360       }
361     }
362     else if (opt->flags & OPT_MAYBE_VALUE) {
363       if (chr == 1 && argv[index][2])
364         opt_parse_value(opt, argv[index] + 2, 0);
365       else
366         opt_parse_value(opt, NULL, 0);
367     }
368     else {
369       ASSERT(0);
370     }
371   }
372
373   if (argv[index][chr])
374     opt_failure("Unknown option -%c.", argv[index][chr]);
375   
376   return 0;
377 }
378
379 static void opt_positional(char * value, struct opt_precomputed * pre) {
380   opt_positional_count++;
381   struct opt_precomputed_option * opt = opt_find_item_shortopt((opt_positional_count > opt_positional_max ? 256 : opt_positional_count + 256), pre);
382   if (!opt) {
383     ASSERT(opt_positional_count > opt_positional_max);
384     opt_failure("Too many positional args.");
385   }
386
387   opt_parse_value(opt, value, 2);
388 }
389
390 #define OPT_TRAVERSE_SECTIONS \
391     while (item->cls == OPT_CL_SECTION) { \
392       if (stk->next) \
393         stk = stk->next; \
394       else { \
395         struct opt_stack * new_stk = alloca(sizeof(*new_stk)); \
396         new_stk->prev = stk; \
397         stk->next = new_stk; \
398         stk = new_stk; \
399       } \
400       stk->this = item; \
401       item = item->u.section->opt; \
402     } \
403     if (item->cls == OPT_CL_END) { \
404       if (!stk->prev) break; \
405       item = stk->this; \
406       stk = stk->prev; \
407       continue; \
408     }
409
410 void opt_parse(const struct opt_section * options, char ** argv) {
411   opt_section_root = options;
412
413   struct opt_stack {
414     struct opt_item * this;
415     struct opt_stack * prev;
416     struct opt_stack * next;
417   } * stk = alloca(sizeof(*stk));
418   stk->this = NULL;
419   stk->prev = NULL;
420   stk->next = NULL;
421
422   struct opt_precomputed * pre = alloca(sizeof(*pre));
423   memset(pre, 0, sizeof (*pre));
424
425   int count = 0;
426
427   for (struct opt_item * item = options->opt; ; item++) {
428     OPT_TRAVERSE_SECTIONS;
429     if (item->letter || item->name)
430       count++;
431     if (item->cls == OPT_CL_BOOL)
432       count++;
433     if (item->letter > 256)
434       opt_positional_max++;
435   }
436   
437   pre->opts = alloca(sizeof(*pre->opts) * count);
438   pre->shortopt = alloca(sizeof(*pre->shortopt) * (opt_positional_max + 257));
439   memset(pre->shortopt, 0, sizeof(*pre->shortopt) * (opt_positional_max + 257));
440   
441   pre->opt_count = 0;
442
443   for (struct opt_item * item = options->opt; ; item++) {
444     OPT_TRAVERSE_SECTIONS;
445     if (item->letter || item->name) {
446       struct opt_precomputed_option * opt = xmalloc(sizeof(*opt));
447       opt->item = item;
448       opt->flags = item->flags;
449       opt->count = 0;
450       opt->name = item->name;
451       pre->opts[pre->opt_count++] = opt;
452       if (item->letter)
453         pre->shortopt[(int) item->letter] = opt;
454       OPT_ADD_DEFAULT_ITEM_FLAGS(item, opt->flags);
455     }
456   }
457
458   int force_positional = 0;
459   for (int i=0;argv[i];i++) {
460     if (argv[i][0] != '-' || force_positional) {
461       opt_positional(argv[i], pre);
462     }
463     else {
464       if (argv[i][1] == '-') {
465         if (argv[i][2] == '\0')
466           force_positional++;
467         else
468           i += opt_longopt(argv, i, pre);
469       }
470       else if (argv[i][1])
471         i += opt_shortopt(argv, i, pre);
472       else
473         opt_positional(argv[i], pre);
474     }
475   }
476
477   for (int i=0;i<opt_positional_max+257;i++) {
478     if (!pre->shortopt[i])
479       continue;
480     if (!pre->shortopt[i]->count && (pre->shortopt[i]->flags | OPT_REQUIRED))
481       if (i < 256)
482         opt_failure("Required option -%c not found.\n", pre->shortopt[i]->item->letter);
483       else
484         opt_failure("Required positional argument #%d not found.\n", (i > 256) ? pre->shortopt[i]->item->letter-256 : opt_positional_max+1);
485   }
486
487   for (int i=0;i<pre->opt_count;i++) {
488     if (!pre->opts[i])
489       continue;
490     if (!pre->opts[i]->count && (pre->opts[i]->flags | OPT_REQUIRED))
491       opt_failure("Required option --%s not found.\n", pre->opts[i]->item->name);
492   }
493 }
494
495 #ifdef TEST
496 #include <ucw/fastbuf.h>
497
498 static void show_version(struct opt_item * opt UNUSED, const char * value UNUSED, void * data UNUSED) {
499   printf("This is a simple tea boiling console v0.1.\n");
500   exit(EXIT_SUCCESS);
501 }
502
503 struct teapot_temperature {
504   enum {
505     TEMP_CELSIUS = 0,
506     TEMP_FAHRENHEIT,
507     TEMP_KELVIN,
508     TEMP_REAUMUR,
509     TEMP_RANKINE
510   } scale;
511   int value;
512 } temperature;
513
514 static char * temp_scale_str[] = { "C", "F", "K", "Re", "R" };
515
516 static enum TEAPOT_TYPE {
517   TEAPOT_STANDARD = 0,
518   TEAPOT_EXCLUSIVE,
519   TEAPOT_GLASS,
520   TEAPOT_HANDS,
521   TEAPOT_UNDEFINED = -1
522 } set = TEAPOT_UNDEFINED;
523
524 static char * teapot_type_str[] = { "standard", "exclusive", "glass", "hands" };
525
526 static int english = 0;
527 static int sugar = 0;
528 static int verbose = 1;
529 static int with_gas = 0;
530 static int black_magic = 0;
531 static int pray = 0;
532 static int water_amount = 0;
533 static char * first_tea = NULL;
534
535 #define MAX_TEA_COUNT 30
536 static char * tea_list[MAX_TEA_COUNT];
537 static int tea_num = 0;
538 static void add_tea(struct opt_item * opt UNUSED, const char * name, void * data) {
539   char ** tea_list = data;
540   if (tea_num >= MAX_TEA_COUNT) {
541     fprintf(stderr, "Cannot boil more than %d teas.\n", MAX_TEA_COUNT);
542     exit(OPT_EXIT_BAD_ARGS);
543   }
544   tea_list[tea_num++] = xstrdup(name);
545 }
546
547 static const char * teapot_temperature_parser(char * in, void * ptr) {
548   struct teapot_temperature * temp = ptr;
549   const char * next;
550   const char * err = str_to_int(&temp->value, in, &next, 10);
551   if (err)
552     return err;
553   if (!strcmp("C", next))
554     temp->scale = TEMP_CELSIUS;
555   else if (!strcmp("F", next))
556     temp->scale = TEMP_FAHRENHEIT;
557   else if (!strcmp("K", next))
558     temp->scale = TEMP_KELVIN;
559   else if (!strcmp("R", next))
560     temp->scale = TEMP_RANKINE;
561   else if (!strcmp("Re", next))
562     temp->scale = TEMP_REAUMUR;
563   else {
564     fprintf(stderr, "Unknown scale: %s\n", next);
565     exit(OPT_EXIT_BAD_ARGS);
566   }
567   return NULL;
568 }
569
570 static void teapot_temperature_dumper(struct fastbuf * fb, void * ptr) {
571   struct teapot_temperature * temp = ptr;
572   bprintf(fb, "%d%s", temp->value, temp_scale_str[temp->scale]);
573 }
574
575 static struct cf_user_type teapot_temperature_t = {
576   .size = sizeof(struct teapot_temperature),
577   .name = "teapot_temperature_t",
578   .parser = (cf_parser1*) teapot_temperature_parser,
579   .dumper = (cf_dumper1*) teapot_temperature_dumper
580 };
581
582 static struct opt_section water_options = {
583   OPT_ITEMS {
584     OPT_INT('w', "water", water_amount, OPT_REQUIRED | OPT_REQUIRED_VALUE, "<volume>\tAmount of water (in mls)"),
585     OPT_BOOL('G', "with-gas", with_gas, OPT_NO_VALUE, "\tUse water with gas"),
586     OPT_END
587   }
588 };
589
590 static struct opt_section help = {
591   OPT_ITEMS {
592     OPT_HELP("A simple tea boiling console."),
593     OPT_HELP("Usage: teapot [options] name-of-the-tea"),
594     OPT_HELP("Black, green or white tea supported as well as fruit or herbal tea."),
595     OPT_HELP("You may specify more kinds of tea, all of them will be boiled for you, in the given order."),
596     OPT_HELP(""),
597     OPT_HELP("Options:"),
598     OPT_HELP_OPTION,
599     OPT_CALL('V', "version", show_version, NULL, OPT_NO_VALUE, "\tShow the version"),
600     OPT_HELP(""),
601     OPT_BOOL('e', "english-style", english, 0, "\tEnglish style (with milk)"),
602     OPT_INT('s', "sugar", sugar, OPT_REQUIRED_VALUE, "<spoons>\tAmount of sugar (in teaspoons)"),
603     OPT_SWITCH(0, "standard-set", set, TEAPOT_STANDARD, 0, "\tStandard teapot"),
604     OPT_SWITCH('x', "exclusive-set", set, TEAPOT_EXCLUSIVE, 0, "\tExclusive teapot"),
605     OPT_SWITCH('g', "glass-set", set, TEAPOT_GLASS, 0, "\tTransparent glass teapot"),
606     OPT_SWITCH('h', "hands", set, TEAPOT_HANDS, 0, "\tUse user's hands as a teapot (a bit dangerous)"),
607     OPT_USER('t', "temperature", temperature, teapot_temperature_t, OPT_REQUIRED_VALUE | OPT_REQUIRED,
608                   "<value>\tWanted final temperature of the tea to be served\n"
609               "\t\tSupported scales:  Celsius [60C], Fahrenheit [140F],\n"
610               "\t\t                   Kelvin [350K], Rankine [600R] and Reaumur [50Re]\n"
611               "\t\tOnly integer values allowed."),
612     OPT_INC('v', "verbose", verbose, 0, "\tVerbose (the more -v, the more verbose)"),
613     OPT_INC('q', "quiet", verbose, OPT_NEGATIVE, "\tQuiet (the more -q, the more quiet)"),
614     OPT_INT('b', "black-magic", black_magic, 0, "<strength>\tUse black magic to make the tea extraordinary delicious"),
615     OPT_BOOL('p', "pray", pray, OPT_SINGLE, "\tPray before boiling"),
616     OPT_STRING(OPT_POSITIONAL(1), NULL, first_tea, OPT_REQUIRED | OPT_NO_HELP, ""),
617     OPT_CALL(OPT_POSITIONAL_TAIL, NULL, add_tea, &tea_list, OPT_NO_HELP, ""),
618     OPT_HELP(""),
619     OPT_HELP("Water options:"),
620     OPT_SECTION(water_options),
621     OPT_END
622   }
623 };
624
625 static void boil_tea(const char * name) {
626   printf("Boiling a tea: %s\n", name);
627 }
628
629 int main(int argc UNUSED, char ** argv)
630 {
631   opt_parse(&help, argv+1);
632
633   printf("Parsed values:\n");
634   printf("English style: %s\n", english ? "yes" : "no");
635   if (sugar)
636     printf("Sugar: %d teaspoons\n", sugar);
637   if (set != -1)
638     printf("Chosen teapot: %s\n", teapot_type_str[set]);
639   printf("Temperature: %d%s\n", temperature.value, temp_scale_str[temperature.scale]);
640   printf("Verbosity: %d\n", verbose);
641   if (black_magic)
642     printf("Black magic: %d\n", black_magic);
643   printf("Prayer: %s\n", pray ? "yes" : "no");
644   printf("Water amount: %d\n", water_amount);
645   printf("Gas: %s\n", with_gas ? "yes" : "no");
646   printf("First tea: %s\n", first_tea);
647   for (int i=0; i<tea_num; i++)
648     boil_tea(tea_list[i]);
649
650   printf("Everything OK. Bye.\n");
651 }
652
653 #endif