2 * UCW Library -- Parsing of command line options
4 * (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include <ucw/stkstring.h>
14 #include <ucw/strtonum.h>
18 static void opt_failure(const char * mesg, ...) FORMAT_CHECK(printf,1,2);
19 static void opt_failure(const char * mesg, ...) {
22 vfprintf(stderr, mesg, args);
23 fprintf(stderr, "\n");
24 exit(OPT_EXIT_BAD_ARGS);
28 #define OPT_ADD_DEFAULT_ITEM_FLAGS(item, flags) \
30 if (!(flags & OPT_VALUE_FLAGS) && \
31 (item->cls == OPT_CL_CALL || item->cls == OPT_CL_USER)) { \
32 fprintf(stderr, "You MUST specify some of the value flags for the %c/%s item.\n", item->letter, item->name); \
35 else if (!(flags & OPT_VALUE_FLAGS)) \
36 flags |= opt_default_value_flags[item->cls]; \
38 #define OPT_ITEM_FLAGS(item) ((item->flags & OPT_VALUE_FLAGS) ? item->flags : item->flags | opt_default_value_flags[item->cls])
40 const struct opt_section * opt_section_root;
42 #define FOREACHLINE(text) for (const char * begin = (text), * end = (text); (*end) && (end = strchrnul(begin, '\n')); begin = end+1)
44 void opt_help_noexit_internal(const struct opt_section * help) {
48 for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
49 if (item->flags & OPT_NO_HELP) continue;
50 if (item->cls == OPT_CL_SECTION) {
58 FOREACHLINE(item->help)
64 struct opt_section * sect;
65 } sections[sections_cnt];
68 const char *lines[lines_cnt][3];
69 memset(lines, 0, sizeof(lines));
72 int linelengths[3] = { -1, -1, -1 };
74 for (struct opt_item * item = help->opt; item->cls != OPT_CL_END; item++) {
75 if (item->flags & OPT_NO_HELP) continue;
77 if (item->cls == OPT_CL_HELP) {
82 #define SPLITLINES(text) do { \
85 for (const char * b = begin, * e = begin; (e < end) && (e = strchrnul(b, '\t')) && (e > end ? (e = end) : end); b = e+1) { \
86 lines[line][cell] = b; \
90 if (*e == '\t' && linelengths[cell] < (e - b)) \
91 linelengths[cell] = e-b; \
96 SPLITLINES(item->help);
100 if (item->cls == OPT_CL_SECTION) {
101 sections[s++] = (struct opt_sectlist) { .pos = line, .sect = item->u.section };
105 uns valoff = strchrnul(item->help, '\t') - item->help;
106 uns eol = strchrnul(item->help, '\n') - item->help;
109 #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)))
111 lines[line][1] = stk_printf("--%s%s", item->name, VAL(item));
112 if (linelengths[1] < (int) strlen(lines[line][1]))
113 linelengths[1] = strlen(lines[line][1]);
115 if (linelengths[0] < 0)
119 lines[line][0] = stk_printf("-%c,", item->letter);
120 if (linelengths[0] < (int) strlen(lines[line][0]))
121 linelengths[0] = strlen(lines[line][0]);
126 lines[line][2] = item->help + valoff + 1;
131 if (*(item->help + eol))
132 SPLITLINES(item->help + eol + 1);
137 #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]
138 #define LASTFIELD(k) MIN(strchrnul(lines[i][k], '\t')-lines[i][k],strchrnul(lines[i][k], '\n')-lines[i][k]), lines[i][k]
139 for (int i=0;i<line;i++) {
140 while (s < sections_cnt && sections[s].pos == i) {
141 opt_help_noexit_internal(sections[s].sect);
144 if (lines[i][0] == NULL)
146 else if (linelengths[0] == -1 || lines[i][1] == NULL)
147 printf("%.*s\n", LASTFIELD(0));
148 else if (linelengths[1] == -1 || lines[i][2] == NULL)
149 printf("%-*.*s %.*s\n", FIELD(0), LASTFIELD(1));
151 printf("%-*.*s %-*.*s %.*s\n", FIELD(0), FIELD(1), LASTFIELD(2));
153 while (s < sections_cnt && sections[s].pos == line) {
154 opt_help_noexit_internal(sections[s].sect);
159 struct opt_precomputed {
160 struct opt_precomputed_option {
161 struct opt_item * item;
165 struct opt_precomputed_option * shortopt[256];
169 static struct opt_item * opt_find_item_shortopt(int chr, struct opt_precomputed * opts) {
170 return (opts->shortopt[chr] ? opts->shortopt[chr]->item : NULL);
173 static struct opt_item * opt_find_item_longopt(char * str, struct opt_precomputed * pre) {
174 uns len = strlen(str);
175 struct opt_item * candidate = NULL;
177 for (int i=0; i<pre->opt_count; i++) {
178 if (!strncmp(pre->opts[i]->item->name, str, len)) {
179 if (strlen(pre->opts[i]->item->name) == len) {
180 if (pre->opts[i]->count++ && (pre->opts[i]->flags & OPT_SINGLE))
181 opt_failure("Option %s appeared the second time.", pre->opts[i]->item->name);
183 return pre->opts[i]->item;
186 opt_failure("Ambiguous prefix %s: Found matching %s and %s.", str, candidate->name, pre->opts[i]->item->name);
188 candidate = pre->opts[i]->item;
195 opt_failure("Invalid option %s.", str);
198 #define OPT_NAME (longopt ? stk_printf("--%s", item->name) : stk_printf("-%c", item->letter))
199 static void opt_parse_value(struct opt_item * item, char * value, int longopt) {
202 if (!strcasecmp(value, "y") || !strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcasecmp(value, "1"))
203 *((int *) item->ptr) = 1;
204 else if (!strcasecmp(value, "n") || !strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcasecmp(value, "0"))
205 *((int *) item->ptr) = 0;
207 opt_failure("Boolean argument for %s has a strange value. Supported (case insensitive): y/n, yes/no, true/false.", OPT_NAME);
212 switch (item->type) {
214 e = cf_parse_int(value, item->ptr);
216 opt_failure("Integer value parsing failed for argument %s: %s", OPT_NAME, e);
219 e = cf_parse_u64(value, item->ptr);
221 opt_failure("Unsigned 64-bit value parsing failed for argument %s: %s", OPT_NAME, e);
224 e = cf_parse_double(value, item->ptr);
226 opt_failure("Double value parsing failed for argument %s: %s", OPT_NAME, e);
229 e = cf_parse_ip(value, item->ptr);
231 opt_failure("IP parsing failed for argument %s: %s", OPT_NAME, e);
234 item->ptr = strdup(value);
242 if (*((int *)item->ptr) != -1)
243 opt_failure("Multiple switches: %s", OPT_NAME);
245 *((int *)item->ptr) = item->u.value;
248 if (item->flags & OPT_DECREMENT)
249 (*((int *)item->ptr))--;
251 (*((int *)item->ptr))++;
253 item->u.call(item, value, item->ptr);
258 e = item->u.utype->parser(value, item->ptr);
260 opt_failure("User defined type value parsing failed for argument %s: %s", OPT_NAME, e);
269 static int opt_longopt(char ** argv, int index, struct opt_precomputed * pre) {
271 char * name_in = argv[index] + 2; // skipping the -- on the beginning
272 uns pos = strchrnul(name_in, '=') - name_in;
273 struct opt_item * item = opt_find_item_longopt(strndupa(name_in, pos), pre);
275 if (item->flags & OPT_REQUIRED_VALUE) {
276 if (pos < strlen(name_in))
277 value = name_in + pos + 1;
279 value = argv[index+1];
283 else if (item->flags & OPT_MAYBE_VALUE) {
284 if (pos < strlen(name_in))
285 value = name_in + pos + 1;
288 if (pos < strlen(name_in))
289 opt_failure("Argument %s must not have any value.", item->name);
291 opt_parse_value(item, value, 1);
295 static int opt_shortopt(char ** argv, int index, struct opt_precomputed * pre) {
297 struct opt_item * item;
298 while (argv[index][++chr] && (item = opt_find_item_shortopt(argv[index][chr], pre))) {
299 if (item->flags & OPT_NO_VALUE) {
300 opt_parse_value(item, NULL, 0);
303 if (chr == 1 && (item->flags & OPT_REQUIRED_VALUE)) {
304 if (argv[index][2]) {
305 opt_parse_value(item, argv[index] + 2, 0);
309 opt_parse_value(item, argv[index+1], 0);
313 else if (chr == 1 && (item->flags & OPT_MAYBE_VALUE)) {
315 opt_parse_value(item, argv[index] + 2, 0);
317 opt_parse_value(item, NULL, 0);
319 else if (item->flags & (OPT_REQUIRED_VALUE | OPT_MAYBE_VALUE)) {
320 if (argv[index][chr+1] || (item->flags | OPT_MAYBE_VALUE))
321 opt_failure("Option -%c may or must have a value but found inside a bunch of short opts.", item->letter);
323 opt_parse_value(item, argv[index+1], 0);
329 if (argv[index][chr])
330 opt_failure("Unknown option -%c.", argv[index][chr]);
335 #define OPT_TRAVERSE_SECTIONS \
336 while (item->cls == OPT_CL_SECTION) { \
340 struct opt_stack * new_stk = alloca(sizeof(*new_stk)); \
341 new_stk->prev = stk; \
342 stk->next = new_stk; \
346 item = item->u.section->opt; \
348 if (item->cls == OPT_CL_END) { \
349 if (!stk->prev) break; \
355 void opt_parse(const struct opt_section * options, char ** argv, opt_positional * callback) {
356 opt_section_root = options;
359 struct opt_item * this;
360 struct opt_stack * prev;
361 struct opt_stack * next;
362 } * stk = alloca(sizeof(*stk));
367 struct opt_precomputed * pre = alloca(sizeof(*pre));
368 memset(pre, 0, sizeof (*pre));
372 for (struct opt_item * item = options->opt; ; item++) {
373 OPT_TRAVERSE_SECTIONS;
374 if (item->letter || item->name)
378 pre->opts = xmalloc(sizeof(*pre->opts) * count);
381 for (struct opt_item * item = options->opt; ; item++) {
382 OPT_TRAVERSE_SECTIONS;
383 if (item->letter || item->name) {
384 struct opt_precomputed_option * opt = xmalloc(sizeof(*opt));
386 opt->flags = item->flags;
388 pre->opts[pre->opt_count++] = opt;
390 pre->shortopt[(int) item->letter] = opt;
391 OPT_ADD_DEFAULT_ITEM_FLAGS(item, opt->flags);
395 int force_positional = 0;
396 for (int i=0;argv[i];i++) {
397 if (argv[i][0] != '-' || force_positional) {
401 if (argv[i][1] == '-') {
402 if (argv[i][2] == '\0')
405 i += opt_longopt(argv, i, pre);
408 i += opt_shortopt(argv, i, pre);
416 #include <ucw/fastbuf.h>
418 static void show_version(struct opt_item * opt UNUSED, const char * value UNUSED, void * data UNUSED) {
419 printf("This is a simple tea boiling console v0.1.\n");
423 struct teapot_temperature {
434 static char * temp_scale_str[] = { "C", "F", "K", "Re", "R" };
436 static enum TEAPOT_TYPE {
441 TEAPOT_UNDEFINED = -1
442 } set = TEAPOT_UNDEFINED;
444 static int english = 0;
445 static char * name = NULL;
446 static int sugar = 0;
447 static int verbose = 1;
448 static int with_gas = 0;
449 static int black_magic = 0;
451 static int water_amount = 0;
453 static const char * teapot_temperature_parser(char * in, void * ptr) {
454 struct teapot_temperature * temp = ptr;
456 const char * err = str_to_int(&temp->value, in, &next, 0);
459 if (!strcmp("C", next))
460 temp->scale = TEMP_CELSIUS;
461 else if (!strcmp("F", next))
462 temp->scale = TEMP_FAHRENHEIT;
463 else if (!strcmp("K", next))
464 temp->scale = TEMP_KELVIN;
465 else if (!strcmp("R", next))
466 temp->scale = TEMP_RANKINE;
467 else if (!strcmp("Re", next))
468 temp->scale = TEMP_REAUMUR;
470 fprintf(stderr, "Unknown scale: %s\n", next);
471 exit(OPT_EXIT_BAD_ARGS);
473 return next + strlen(next);
476 static void teapot_temperature_dumper(struct fastbuf * fb, void * ptr) {
477 struct teapot_temperature * temp = ptr;
478 bprintf(fb, "%d%s", temp->value, temp_scale_str[temp->scale]);
481 static struct cf_user_type teapot_temperature_t = {
482 .size = sizeof(struct teapot_temperature),
483 .name = "teapot_temperature_t",
484 .parser = (cf_parser1*) teapot_temperature_parser,
485 .dumper = (cf_dumper1*) teapot_temperature_dumper
488 static struct opt_section water_options = {
490 OPT_INT('w', "water", water_amount, OPT_REQUIRED | OPT_REQUIRED_VALUE, "<volume>\tAmount of water (in mls)"),
491 OPT_BOOL('G', "with-gas", with_gas, OPT_NO_VALUE, "\tUse water with gas"),
496 static struct opt_section help = {
498 OPT_HELP("A simple tea boiling console."),
499 OPT_HELP("Usage: teapot [options] name-of-the-tea"),
500 OPT_HELP("Black, green or white tea supported as well as fruit or herbal tea."),
501 OPT_HELP("You may specify more kinds of tea, all of them will be boiled for you, in the given order."),
503 OPT_HELP("Options:"),
505 OPT_CALL('V', "version", show_version, NULL, OPT_NO_VALUE, "\tShow the version"),
507 OPT_BOOL('e', "english-style", english, 0, "\tEnglish style (with milk)"),
508 OPT_INT('s', "sugar", sugar, OPT_REQUIRED_VALUE, "<spoons>\tAmount of sugar (in teaspoons)"),
509 OPT_SWITCH(0, "standard-set", set, TEAPOT_STANDARD, 0, "\tStandard teapot"),
510 OPT_SWITCH('x', "exclusive-set", set, TEAPOT_EXCLUSIVE, 0, "\tExclusive teapot"),
511 OPT_SWITCH('g', "glass-set", set, TEAPOT_GLASS, 0, "\tTransparent glass teapot"),
512 OPT_SWITCH('h', "hands", set, TEAPOT_HANDS, 0, "\tUse user's hands as a teapot (a bit dangerous)"),
513 OPT_USER('t', "temperature", temperature, teapot_temperature_t, OPT_REQUIRED_VALUE,
514 "<value>\tWanted final temperature of the tea to be served\n"
515 "\t\tSupported scales: Celsius [60C], Fahrenheit [140F],\n"
516 "\t\t Kelvin [350K], Rankine [600R] and Reaumur [50Re]\n"
517 "\t\tOnly integer values allowed."),
518 OPT_INC('v', "verbose", verbose, 0, "\tVerbose (the more -v, the more verbose)"),
519 OPT_INC('q', "quiet", verbose, OPT_DECREMENT, "\tQuiet (the more -q, the more quiet)"),
520 OPT_INT('b', "black-magic", black_magic, 0, "<strength>\tUse black magic to make the tea extraordinary delicious"),
521 OPT_BOOL('p', "pray", pray, 0, "\tPray before boiling"),
523 OPT_HELP("Water options:"),
524 OPT_SECTION(water_options),
529 #define MAX_TEA_COUNT 30
530 static char * tea_list[MAX_TEA_COUNT];
531 static int tea_num = 0;
532 static void add_tea(const char * name) {
533 if (tea_num >= MAX_TEA_COUNT) {
534 fprintf(stderr, "Cannot boil more than %d teas.\n", MAX_TEA_COUNT);
535 exit(OPT_EXIT_BAD_ARGS);
537 tea_list[tea_num++] = strdup(name);
540 static void boil_tea(const char * name) {
541 printf("Boiling a tea: %s\n", name);
544 int main(int argc, char ** argv)
546 opt_parse(&help, argv, add_tea);
548 for (int i=0; i<tea_num; i++)
549 boil_tea(tea_list[i]);
551 printf("Everything OK. Bye.\n");