]> mj.ucw.cz Git - libucw.git/blob - ucw/opt.c
Opt: Tests and some minor fixes
[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)
183     opt_failure("Invalid option -%c", chr);
184   if (candidate->count++ && (candidate->flags & OPT_SINGLE))
185     opt_failure("Option -%c appeared the second time.", candidate->item->letter);
186   return candidate;
187 }
188
189 static struct opt_precomputed_option * opt_find_item_longopt(char * str, struct opt_precomputed * pre) {
190   uns len = strlen(str);
191   struct opt_precomputed_option * candidate = NULL;
192
193   for (int i=0; i<pre->opt_count; i++) {
194     if (!strncmp(pre->opts[i]->name, str, len)) {
195       if (strlen(pre->opts[i]->name) == len) {
196         if (pre->opts[i]->count++ && (pre->opts[i]->flags & OPT_SINGLE))
197           opt_failure("Option %s appeared the second time.", pre->opts[i]->name);
198
199         return pre->opts[i];
200       }
201       if (candidate)
202         opt_failure("Ambiguous prefix %s: Found matching %s and %s.", str, candidate->name, pre->opts[i]->name);
203       else
204         candidate = pre->opts[i];
205     }
206     if (!strncmp("no-", str, 3) && !strncmp(pre->opts[i]->name, str+3, len-3)) {
207       if (strlen(pre->opts[i]->name) == len-3) {
208         if (pre->opts[i]->count++ && (pre->opts[i]->flags & OPT_SINGLE))
209           opt_failure("Option %s appeared the second time.", pre->opts[i]->name);
210
211         return pre->opts[i];
212       }
213       if (candidate)
214         opt_failure("Ambiguous prefix %s: Found matching %s and %s.", str, candidate->name, pre->opts[i]->name);
215       else
216         candidate = pre->opts[i];
217     }
218   }
219
220   if (candidate)
221     return candidate;
222
223   opt_failure("Invalid option %s.", str);
224 }
225
226 #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)))
227 static void opt_parse_value(struct opt_precomputed_option * opt, char * value, int longopt) {
228   struct opt_item * item = opt->item;
229   switch (item->cls) {
230     case OPT_CL_BOOL:
231       if (!value || !strcasecmp(value, "y") || !strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcasecmp(value, "1"))
232         *((int *) item->ptr) = 1 ^ (!!(opt->flags & OPT_NEGATIVE));
233       else if (!strcasecmp(value, "n") || !strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcasecmp(value, "0"))
234         *((int *) item->ptr) = 0 ^ (!!(opt->flags & OPT_NEGATIVE));
235       else
236         opt_failure("Boolean argument for %s has a strange value. Supported (case insensitive): y/n, yes/no, true/false.", OPT_NAME);
237       break;
238     case OPT_CL_STATIC:
239       {
240         char * e = NULL;
241         switch (item->type) {
242           case CT_INT:
243             if (!value)
244               *((int*)item->ptr) = 0;
245             else
246               e = cf_parse_int(value, item->ptr);
247             if (e)
248               opt_failure("Integer value parsing failed for %s: %s", OPT_NAME, e);
249             break;
250           case CT_U64:
251             if (!value)
252               *((u64*)item->ptr) = 0;
253             else
254               e = cf_parse_u64(value, item->ptr);
255             if (e)
256               opt_failure("Unsigned 64-bit value parsing failed for %s: %s", OPT_NAME, e);
257             break;
258           case CT_DOUBLE:
259             if (!value)
260               *((double*)item->ptr) = NAN;
261             else
262               e = cf_parse_double(value, item->ptr);
263             if (e)
264               opt_failure("Double value parsing failed for %s: %s", OPT_NAME, e);
265             break;
266           case CT_IP:
267             if (!value)
268               e = cf_parse_ip("0.0.0.0", item->ptr);
269             else
270               e = cf_parse_ip(value, item->ptr);
271             if (e)
272               opt_failure("IP parsing failed for %s: %s", OPT_NAME, e);
273             break;
274           case CT_STRING:
275             if (!value)
276               item->ptr = NULL;
277             else
278               *((const char **) (item->ptr)) = xstrdup(value);
279             break;
280           default:
281             ASSERT(0);
282         }
283         break;
284       }
285     case OPT_CL_SWITCH:
286       if (*((int *)item->ptr) != -1)
287         opt_failure("Multiple switches: %s", OPT_NAME);
288       else
289         *((int *)item->ptr) = item->u.value;
290       break;
291     case OPT_CL_INC:
292       if (opt->flags & OPT_NEGATIVE)
293         (*((int *)item->ptr))--;
294       else
295         (*((int *)item->ptr))++;
296       break;
297     case OPT_CL_CALL:
298       item->u.call(item, value, item->ptr);
299       break;
300     case OPT_CL_USER:
301       {
302         char * e = NULL;
303         e = item->u.utype->parser(value, item->ptr);
304         if (e)
305           opt_failure("User defined type value parsing failed for %s: %s", OPT_NAME, e);
306         break;
307       }
308     default:
309       ASSERT(0);
310   }
311 }
312 #undef OPT_NAME
313
314 static int opt_longopt(char ** argv, int index, struct opt_precomputed * pre) {
315   int eaten = 0;
316   char * name_in = argv[index] + 2; // skipping the -- on the beginning
317   uns pos = strchrnul(name_in, '=') - name_in;
318   struct opt_precomputed_option * opt = opt_find_item_longopt(strndupa(name_in, pos), pre);
319   char * value = NULL;
320
321   if (opt->item->cls == OPT_CL_BOOL && !strncmp(name_in, "no-", 3) && !strncmp(name_in+3, opt->item->name, pos-3))
322     value = "n";
323   else if (opt->flags & OPT_REQUIRED_VALUE) {
324     if (pos < strlen(name_in))
325       value = name_in + pos + 1;
326     else {
327       value = argv[index+1];
328       if (!value)
329         opt_failure("Argument --%s must have a value but nothing supplied.", opt->name);
330       eaten++;
331     }
332   }
333   else if (opt->flags & OPT_MAYBE_VALUE) {
334     if (pos < strlen(name_in))
335       value = name_in + pos + 1;
336   }
337   else {
338     if (pos < strlen(name_in))
339       opt_failure("Argument --%s must not have any value.", opt->name);
340   }
341   opt_parse_value(opt, value, 1);
342   return eaten;
343 }
344
345 static int opt_shortopt(char ** argv, int index, struct opt_precomputed * pre) {
346   int chr = 0;
347   struct opt_precomputed_option * opt;
348   while (argv[index][++chr] && (opt = opt_find_item_shortopt(argv[index][chr], pre))) {
349     if (opt->flags & OPT_NO_VALUE) {
350       opt_parse_value(opt, NULL, 0);
351     }
352     else if (opt->flags & OPT_REQUIRED_VALUE) {
353       if (chr == 1 && argv[index][2]) {
354         opt_parse_value(opt, argv[index] + 2, 0);
355         return 0;
356       }
357       else if (argv[index][chr+1])
358         opt_failure("Option -%c must have a value but found inside a bunch of short opts.", opt->item->letter);
359       else if (!argv[index+1])
360         opt_failure("Option -%c must have a value but nothing supplied.", opt->item->letter);
361       else {
362         opt_parse_value(opt, argv[index+1], 0);
363         return 1;
364       }
365     }
366     else if (opt->flags & OPT_MAYBE_VALUE) {
367       if (chr == 1 && argv[index][2]) {
368         opt_parse_value(opt, argv[index] + 2, 0);
369         return 0;
370       }
371       else
372         opt_parse_value(opt, NULL, 0);
373     }
374     else {
375       ASSERT(0);
376     }
377   }
378
379   if (argv[index][chr])
380     opt_failure("Unknown option -%c.", argv[index][chr]);
381   
382   return 0;
383 }
384
385 static void opt_positional(char * value, struct opt_precomputed * pre) {
386   opt_positional_count++;
387   struct opt_precomputed_option * opt = opt_find_item_shortopt((opt_positional_count > opt_positional_max ? 256 : opt_positional_count + 256), pre);
388   if (!opt) {
389     ASSERT(opt_positional_count > opt_positional_max);
390     opt_failure("Too many positional args.");
391   }
392
393   opt_parse_value(opt, value, 2);
394 }
395
396 #define OPT_TRAVERSE_SECTIONS \
397     while (item->cls == OPT_CL_SECTION) { \
398       if (stk->next) \
399         stk = stk->next; \
400       else { \
401         struct opt_stack * new_stk = alloca(sizeof(*new_stk)); \
402         new_stk->prev = stk; \
403         stk->next = new_stk; \
404         stk = new_stk; \
405       } \
406       stk->this = item; \
407       item = item->u.section->opt; \
408     } \
409     if (item->cls == OPT_CL_END) { \
410       if (!stk->prev) break; \
411       item = stk->this; \
412       stk = stk->prev; \
413       continue; \
414     }
415
416 void opt_parse(const struct opt_section * options, char ** argv) {
417   opt_section_root = options;
418
419   struct opt_stack {
420     struct opt_item * this;
421     struct opt_stack * prev;
422     struct opt_stack * next;
423   } * stk = alloca(sizeof(*stk));
424   stk->this = NULL;
425   stk->prev = NULL;
426   stk->next = NULL;
427
428   struct opt_precomputed * pre = alloca(sizeof(*pre));
429   memset(pre, 0, sizeof (*pre));
430
431   int count = 0;
432
433   for (struct opt_item * item = options->opt; ; item++) {
434     OPT_TRAVERSE_SECTIONS;
435     if (item->letter || item->name)
436       count++;
437     if (item->cls == OPT_CL_BOOL)
438       count++;
439     if (item->letter > 256)
440       opt_positional_max++;
441   }
442   
443   pre->opts = alloca(sizeof(*pre->opts) * count);
444   pre->shortopt = alloca(sizeof(*pre->shortopt) * (opt_positional_max + 257));
445   memset(pre->shortopt, 0, sizeof(*pre->shortopt) * (opt_positional_max + 257));
446   
447   pre->opt_count = 0;
448
449   for (struct opt_item * item = options->opt; ; item++) {
450     OPT_TRAVERSE_SECTIONS;
451     if (item->letter || item->name) {
452       struct opt_precomputed_option * opt = xmalloc(sizeof(*opt));
453       opt->item = item;
454       opt->flags = item->flags;
455       opt->count = 0;
456       opt->name = item->name;
457       pre->opts[pre->opt_count++] = opt;
458       if (item->letter)
459         pre->shortopt[(int) item->letter] = opt;
460       OPT_ADD_DEFAULT_ITEM_FLAGS(item, opt->flags);
461     }
462   }
463
464   int force_positional = 0;
465   for (int i=0;argv[i];i++) {
466     if (argv[i][0] != '-' || force_positional) {
467       opt_positional(argv[i], pre);
468     }
469     else {
470       if (argv[i][1] == '-') {
471         if (argv[i][2] == '\0')
472           force_positional++;
473         else
474           i += opt_longopt(argv, i, pre);
475       }
476       else if (argv[i][1])
477         i += opt_shortopt(argv, i, pre);
478       else
479         opt_positional(argv[i], pre);
480     }
481   }
482
483   for (int i=0;i<opt_positional_max+257;i++) {
484     if (!pre->shortopt[i])
485       continue;
486     if (!pre->shortopt[i]->count && (pre->shortopt[i]->flags & OPT_REQUIRED))
487       if (i < 256)
488         opt_failure("Required option -%c not found.\n", pre->shortopt[i]->item->letter);
489       else
490         opt_failure("Required positional argument #%d not found.\n", (i > 256) ? pre->shortopt[i]->item->letter-256 : opt_positional_max+1);
491   }
492
493   for (int i=0;i<pre->opt_count;i++) {
494     if (!pre->opts[i])
495       continue;
496     if (!pre->opts[i]->count && (pre->opts[i]->flags & OPT_REQUIRED))
497       opt_failure("Required option --%s not found.\n", pre->opts[i]->item->name);
498   }
499 }
500
501 #ifdef TEST
502 #include <ucw/fastbuf.h>
503
504 static void show_version(struct opt_item * opt UNUSED, const char * value UNUSED, void * data UNUSED) {
505   printf("This is a simple tea boiling console v0.1.\n");
506   exit(EXIT_SUCCESS);
507 }
508
509 struct teapot_temperature {
510   enum {
511     TEMP_CELSIUS = 0,
512     TEMP_FAHRENHEIT,
513     TEMP_KELVIN,
514     TEMP_REAUMUR,
515     TEMP_RANKINE
516   } scale;
517   int value;
518 } temperature;
519
520 static char * temp_scale_str[] = { "C", "F", "K", "Re", "R" };
521
522 static enum TEAPOT_TYPE {
523   TEAPOT_STANDARD = 0,
524   TEAPOT_EXCLUSIVE,
525   TEAPOT_GLASS,
526   TEAPOT_HANDS,
527   TEAPOT_UNDEFINED = -1
528 } set = TEAPOT_UNDEFINED;
529
530 static char * teapot_type_str[] = { "standard", "exclusive", "glass", "hands" };
531
532 static int english = 0;
533 static int sugar = 0;
534 static int verbose = 1;
535 static int with_gas = 0;
536 static int black_magic = 0;
537 static int pray = 0;
538 static int water_amount = 0;
539 static char * first_tea = NULL;
540
541 #define MAX_TEA_COUNT 30
542 static char * tea_list[MAX_TEA_COUNT];
543 static int tea_num = 0;
544 static void add_tea(struct opt_item * opt UNUSED, const char * name, void * data) {
545   char ** tea_list = data;
546   if (tea_num >= MAX_TEA_COUNT) {
547     fprintf(stderr, "Cannot boil more than %d teas.\n", MAX_TEA_COUNT);
548     exit(OPT_EXIT_BAD_ARGS);
549   }
550   tea_list[tea_num++] = xstrdup(name);
551 }
552
553 static const char * teapot_temperature_parser(char * in, void * ptr) {
554   struct teapot_temperature * temp = ptr;
555   const char * next;
556   const char * err = str_to_int(&temp->value, in, &next, 10);
557   if (err)
558     return err;
559   if (!strcmp("C", next))
560     temp->scale = TEMP_CELSIUS;
561   else if (!strcmp("F", next))
562     temp->scale = TEMP_FAHRENHEIT;
563   else if (!strcmp("K", next))
564     temp->scale = TEMP_KELVIN;
565   else if (!strcmp("R", next))
566     temp->scale = TEMP_RANKINE;
567   else if (!strcmp("Re", next))
568     temp->scale = TEMP_REAUMUR;
569   else {
570     fprintf(stderr, "Unknown scale: %s\n", next);
571     exit(OPT_EXIT_BAD_ARGS);
572   }
573   return NULL;
574 }
575
576 static void teapot_temperature_dumper(struct fastbuf * fb, void * ptr) {
577   struct teapot_temperature * temp = ptr;
578   bprintf(fb, "%d%s", temp->value, temp_scale_str[temp->scale]);
579 }
580
581 static struct cf_user_type teapot_temperature_t = {
582   .size = sizeof(struct teapot_temperature),
583   .name = "teapot_temperature_t",
584   .parser = (cf_parser1*) teapot_temperature_parser,
585   .dumper = (cf_dumper1*) teapot_temperature_dumper
586 };
587
588 static struct opt_section water_options = {
589   OPT_ITEMS {
590     OPT_INT('w', "water", water_amount, OPT_REQUIRED | OPT_REQUIRED_VALUE, "<volume>\tAmount of water (in mls; required)"),
591     OPT_BOOL('G', "with-gas", with_gas, OPT_NO_VALUE, "\tUse water with gas"),
592     OPT_END
593   }
594 };
595
596 static struct opt_section help = {
597   OPT_ITEMS {
598     OPT_HELP("A simple tea boiling console."),
599     OPT_HELP("Usage: teapot [options] name-of-the-tea"),
600     OPT_HELP("Black, green or white tea supported as well as fruit or herbal tea."),
601     OPT_HELP("You may specify more kinds of tea, all of them will be boiled for you, in the given order."),
602     OPT_HELP("At least one kind of tea must be specified."),
603     OPT_HELP(""),
604     OPT_HELP("Options:"),
605     OPT_HELP_OPTION,
606     OPT_CALL('V', "version", show_version, NULL, OPT_NO_VALUE, "\tShow the version"),
607     OPT_HELP(""),
608     OPT_BOOL('e', "english-style", english, 0, "\tEnglish style (with milk)"),
609     OPT_INT('s', "sugar", sugar, OPT_REQUIRED_VALUE, "<spoons>\tAmount of sugar (in teaspoons)"),
610     OPT_SWITCH(0, "standard-set", set, TEAPOT_STANDARD, 0, "\tStandard teapot"),
611     OPT_SWITCH('x', "exclusive-set", set, TEAPOT_EXCLUSIVE, 0, "\tExclusive teapot"),
612     OPT_SWITCH('g', "glass-set", set, TEAPOT_GLASS, 0, "\tTransparent glass teapot"),
613     OPT_SWITCH('h', "hands", set, TEAPOT_HANDS, 0, "\tUse user's hands as a teapot (a bit dangerous)"),
614     OPT_USER('t', "temperature", temperature, teapot_temperature_t, OPT_REQUIRED_VALUE | OPT_REQUIRED,
615                   "<value>\tWanted final temperature of the tea to be served (required)\n"
616               "\t\tSupported scales:  Celsius [60C], Fahrenheit [140F],\n"
617               "\t\t                   Kelvin [350K], Rankine [600R] and Reaumur [50Re]\n"
618               "\t\tOnly integer values allowed."),
619     OPT_INC('v', "verbose", verbose, 0, "\tVerbose (the more -v, the more verbose)"),
620     OPT_INC('q', "quiet", verbose, OPT_NEGATIVE, "\tQuiet (the more -q, the more quiet)"),
621     OPT_INT('b', "black-magic", black_magic, 0, "<strength>\tUse black magic to make the tea extraordinary delicious"),
622     OPT_BOOL('p', "pray", pray, OPT_SINGLE, "\tPray before boiling"),
623     OPT_STRING(OPT_POSITIONAL(1), NULL, first_tea, OPT_REQUIRED | OPT_NO_HELP, ""),
624     OPT_CALL(OPT_POSITIONAL_TAIL, NULL, add_tea, &tea_list, OPT_NO_HELP, ""),
625     OPT_HELP(""),
626     OPT_HELP("Water options:"),
627     OPT_SECTION(water_options),
628     OPT_END
629   }
630 };
631
632 int main(int argc UNUSED, char ** argv)
633 {
634   opt_parse(&help, argv+1);
635
636   printf("English style: %s|", english ? "yes" : "no");
637   if (sugar)
638     printf("Sugar: %d teaspoons|", sugar);
639   if (set != -1)
640     printf("Chosen teapot: %s|", teapot_type_str[set]);
641   printf("Temperature: %d%s|", temperature.value, temp_scale_str[temperature.scale]);
642   printf("Verbosity: %d|", verbose);
643   if (black_magic)
644     printf("Black magic: %d|", black_magic);
645   printf("Prayer: %s|", pray ? "yes" : "no");
646   printf("Water amount: %d|", water_amount);
647   printf("Gas: %s|", with_gas ? "yes" : "no");
648   printf("First tea: %s|", first_tea);
649   for (int i=0; i<tea_num; i++)
650     printf("Boiling a tea: %s|", tea_list[i]);
651
652   printf("Everything OK. Bye.\n");
653 }
654
655 #endif