1 /* Licensed under GPLv2+ - see LICENSE file for details */
4 #include <ccan/compiler/compiler.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
12 * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
13 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
14 * @cb: the callback when the option is found.
15 * @arg: the argument to hand to @cb.
16 * @desc: the description for opt_usage(), or opt_hidden.
18 * This is a typesafe wrapper for initializing a struct opt_table. The callback
19 * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)",
20 * where "type" is the type of the @arg argument.
22 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
23 * returned string to form an error message for errlog(), free() the
24 * string (or see opt_set_alloc) and return false.
26 * Any number of equivalent short or long options can be listed in @names,
27 * separated by '|'. Short options are a single hyphen followed by a single
28 * character, long options are two hyphens followed by one or more characters.
33 #define OPT_WITHOUT_ARG(names, cb, arg, desc) \
34 { (names), OPT_CB_NOARG((cb), 0, (arg)), { (arg) }, (desc) }
37 * OPT_WITH_ARG() - macro for initializing an opt_table entry (with arg)
38 * @names: the option names eg. "--foo=<arg>", "-f" or "-f|--foo <arg>".
39 * @cb: the callback when the option is found (along with <arg>).
40 * @show: the callback to print the value in get_usage (or NULL)
41 * @arg: the argument to hand to @cb and @show
42 * @desc: the description for opt_usage(), or opt_hidden.
44 * This is a typesafe wrapper for initializing a struct opt_table. The callback
45 * is of type "char *cb(const char *, type *)",
46 * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
47 * where "type" is the type of the @arg argument. The first argument to the
48 * @cb is the argument found on the commandline.
50 * Similarly, if @show is not NULL, it should be of type "void *show(char *,
51 * const type *)". It should write up to OPT_SHOW_LEN bytes into the first
52 * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
53 * nul-terminate that buffer.
55 * Any number of equivalent short or long options can be listed in @names,
56 * separated by '|'. Short options are a single hyphen followed by a single
57 * character, long options are two hyphens followed by one or more characters.
58 * A space or equals in @names is ignored for parsing, and only used
59 * for printing the usage.
61 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
62 * returned string to form an error message for errlog(), free() the
63 * string (or see opt_set_alloc) and return false.
68 #define OPT_WITH_ARG(name, cb, show, arg, desc) \
69 { (name), OPT_CB_ARG((cb), 0, (show), (arg)), { (arg) }, (desc) }
72 * OPT_SUBTABLE() - macro for including another table inside a table.
73 * @table: the table to include in this table.
74 * @desc: description of this subtable (for opt_usage()) or NULL.
76 #define OPT_SUBTABLE(table, desc) \
77 { (const char *)(table), OPT_SUBTABLE, \
78 sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, \
82 * OPT_EARLY_WITHOUT_ARG() - macro for a early opt_table entry (without arg)
83 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
84 * @cb: the callback when the option is found.
85 * @arg: the argument to hand to @cb.
86 * @desc: the description for opt_usage(), or opt_hidden.
88 * This is the same as OPT_WITHOUT_ARG, but for opt_early_parse() instead of
92 * OPT_EARLY_WITH_ARG(), opt_early_parse()
94 #define OPT_EARLY_WITHOUT_ARG(names, cb, arg, desc) \
95 { (names), OPT_CB_NOARG((cb), OPT_EARLY, (arg)), { (arg) }, (desc) }
98 * OPT_EARLY_WITH_ARG() - macro for an early opt_table entry (with arg)
99 * @names: the option names eg. "--foo=<arg>", "-f" or "-f|--foo <arg>".
100 * @cb: the callback when the option is found (along with <arg>).
101 * @show: the callback to print the value in get_usage (or NULL)
102 * @arg: the argument to hand to @cb and @show
103 * @desc: the description for opt_usage(), or opt_hidden.
105 * This is the same as OPT_WITH_ARG, but for opt_early_parse() instead of
109 * OPT_EARLY_WITHOUT_ARG(), opt_early_parse()
111 #define OPT_EARLY_WITH_ARG(name, cb, show, arg, desc) \
112 { (name), OPT_CB_ARG((cb), OPT_EARLY, (show), (arg)), { (arg) }, (desc) }
115 * OPT_ENDTABLE - macro to create final entry in table.
117 * This must be the final element in the opt_table array.
119 #define OPT_ENDTABLE { NULL, OPT_END, NULL, NULL, NULL, { NULL }, NULL }
122 * opt_register_table - register a table of options
123 * @table: the table of options
124 * @desc: description of this subtable (for opt_usage()) or NULL.
126 * The table must be terminated by OPT_ENDTABLE.
129 * static int verbose = 0;
130 * static struct opt_table opts[] = {
131 * OPT_WITHOUT_ARG("--verbose", opt_inc_intval, &verbose,
132 * "Verbose mode (can be specified more than once)"),
133 * OPT_WITHOUT_ARG("-v", opt_inc_intval, &verbose,
134 * "Verbose mode (can be specified more than once)"),
135 * OPT_WITHOUT_ARG("--usage", opt_usage_and_exit,
136 * "args...\nA silly test program.",
137 * "Print this message."),
142 * opt_register_table(opts, NULL);
144 void opt_register_table(const struct opt_table *table, const char *desc);
147 * opt_register_noarg - register an option with no arguments
148 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
149 * @cb: the callback when the option is found.
150 * @arg: the argument to hand to @cb.
151 * @desc: the verbose description of the option (for opt_usage()), or NULL.
153 * This is used for registering a single commandline option which takes
156 * The callback is of type "char *cb(type *)", "char *cb(const type *)"
157 * or "char *cb(void *)", where "type" is the type of the @arg
160 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
161 * returned string to form an error message for errlog(), free() the
162 * string (or see opt_set_alloc) and return false.
164 #define opt_register_noarg(names, cb, arg, desc) \
165 _opt_register((names), OPT_CB_NOARG((cb), 0, (arg)), (arg), (desc))
168 * opt_register_arg - register an option with an arguments
169 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
170 * @cb: the callback when the option is found.
171 * @show: the callback to print the value in get_usage (or NULL)
172 * @arg: the argument to hand to @cb.
173 * @desc: the verbose description of the option (for opt_usage()), or NULL.
175 * This is used for registering a single commandline option which takes
178 * The callback is of type "char *cb(const char *, type *)",
179 * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
180 * where "type" is the type of the @arg argument. The first argument to the
181 * @cb is the argument found on the commandline.
183 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
184 * returned string to form an error message for errlog(), free() the
185 * string (or see opt_set_alloc) and return false.
188 * static char *explode(const char *optarg, void *unused UNNEEDED)
190 * errx(1, "BOOM! %s", optarg);
193 * opt_register_arg("--explode|--boom", explode, NULL, NULL, opt_hidden);
195 #define opt_register_arg(names, cb, show, arg, desc) \
196 _opt_register((names), OPT_CB_ARG((cb),0,(show), (arg)), (arg), (desc))
199 * opt_register_early_noarg - register an early option with no arguments
200 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
201 * @cb: the callback when the option is found.
202 * @arg: the argument to hand to @cb.
203 * @desc: the verbose description of the option (for opt_usage()), or NULL.
205 * This is the same as opt_register_noarg(), but for opt_early_parse().
208 * opt_register_early_arg(), opt_early_parse()
210 #define opt_register_early_noarg(names, cb, arg, desc) \
211 _opt_register((names), OPT_CB_NOARG((cb), OPT_EARLY, (arg)), \
215 * opt_register_early_arg - register an early option with an arguments
216 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
217 * @cb: the callback when the option is found.
218 * @show: the callback to print the value in get_usage (or NULL)
219 * @arg: the argument to hand to @cb.
220 * @desc: the verbose description of the option (for opt_usage()), or NULL.
222 * This is the same as opt_register_arg(), but for opt_early_parse().
225 * opt_register_early_noarg(), opt_early_parse()
227 #define opt_register_early_arg(names, cb, show, arg, desc) \
228 _opt_register((names), OPT_CB_ARG((cb), OPT_EARLY, (show),(arg)), \
232 * opt_parse - parse arguments.
233 * @argc: pointer to argc
235 * @errlog: the function to print errors
237 * This iterates through the command line and calls callbacks registered with
238 * opt_register_arg()/opt_register_noarg() or OPT_WITHOUT_ARG/OPT_WITH_ARG
239 * entries in tables registered with opt_register_table(). As this occurs
240 * each option is removed from argc and argv.
242 * If there are unknown options, missing arguments or a callback
243 * returns false, then an error message is printed and false is
244 * returned: the erroneous option is not removed.
246 * On success, argc and argv will contain only the non-option
247 * elements, and true is returned.
250 * if (!opt_parse(&argc, argv, opt_log_stderr)) {
251 * printf("You screwed up, aborting!\n");
256 * opt_log_stderr, opt_log_stderr_exit, opt_early_parse()
258 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
261 * opt_early_parse - parse early arguments.
264 * @errlog: the function to print errors
266 * There are times when you want to parse some arguments before any other
267 * arguments; this is especially important for debugging flags (eg. --verbose)
268 * when you have complicated callbacks in option processing.
270 * You can use opt_early_parse() to only parse options registered with
271 * opt_register_earlyarg()/opt_register_early_noarg() or
272 * OPT_EARLY_WITHOUT_ARG/OPT_EARLY_WITH_ARG entries in tables registered with
273 * opt_register_table().
275 * Note that unlike opt_parse(), argc and argv are not altered.
278 * if (!opt_early_parse(argc, argv, opt_log_stderr)) {
279 * printf("You screwed up, aborting!\n");
286 bool opt_early_parse(int argc, char *argv[],
287 void (*errlog)(const char *fmt, ...));
290 * opt_early_parse_incomplete - parse early arguments, ignoring unknown ones.
293 * @errlog: the function to print errors
295 * If you have plugins, you might need to do early parsing (eg. to find the
296 * plugin directory) but you don't know what options the plugins will want.
298 * Thus, this function is just like opt_early_parse, but ignores unknown options.
301 * if (!opt_early_parse_incomplete(argc, argv, opt_log_stderr)) {
302 * printf("You screwed up, aborting!\n");
309 bool opt_early_parse_incomplete(int argc, char *argv[],
310 void (*errlog)(const char *fmt, ...));
314 * opt_free_table - reset the opt library.
316 * This frees the internal memory and returns counters to zero. Call
317 * this as the last opt function to avoid memory leaks. You can also
318 * use this function to reset option handling to its initial state (no
319 * options registered).
321 void opt_free_table(void);
324 * opt_set_alloc - set alloc/realloc/free function for opt to use.
325 * @allocfn: allocator function
326 * @reallocfn: reallocator function, ptr may be NULL, size never 0.
327 * @freefn: free function
329 * By default opt uses malloc/realloc/free, and simply crashes if they fail.
330 * You can set your own variants here.
332 void opt_set_alloc(void *(*allocfn)(size_t size),
333 void *(*reallocfn)(void *ptr, size_t size),
334 void (*freefn)(void *ptr));
337 * opt_log_stderr - print message to stderr.
338 * @fmt: printf-style format.
340 * This is a helper for opt_parse, to print errors to stderr.
343 * opt_log_stderr_exit
345 void opt_log_stderr(const char *fmt, ...);
348 * opt_log_stderr_exit - print message to stderr, then exit(1)
349 * @fmt: printf-style format.
351 * Just like opt_log_stderr, only then does exit(1). This means that
352 * when handed to opt_parse, opt_parse will never return false.
355 * // This never returns false; just exits if there's an erorr.
356 * opt_parse(&argc, argv, opt_log_stderr_exit);
358 void opt_log_stderr_exit(const char *fmt, ...);
361 * opt_invalid_argument - helper to allocate an "Invalid argument '%s'" string
362 * @arg: the argument which was invalid.
364 * This is a helper for callbacks to return a simple error string.
366 char *opt_invalid_argument(const char *arg);
369 * opt_usage - create usage message
370 * @argv0: the program name
371 * @extra: extra details to print after the initial command, or NULL.
373 * Creates a usage message, with the program name, arguments, some extra details
374 * and a table of all the options with their descriptions. If an option has
375 * description opt_hidden, it is not shown here.
377 * The table of options is formatted such that descriptions are
378 * wrapped on space boundaries. If a description has a "\n" that is
379 * left intact, and the following characters indented appropriately.
380 * If the description begins with one or more space/tab (or has a
381 * space or tab following a "\n") that line is output without wrapping.
383 * If "extra" is NULL, then the extra information is taken from any
384 * registered option which calls opt_usage_and_exit(). This avoids duplicating
385 * that string in the common case.
387 * The result should be passed to free().
390 * opt_usage_and_exit()
393 * opt_register_arg("--explode|--boom", explode, NULL, NULL,
394 * "This line will be wrapped by opt_usage\n"
395 * " But this won't because it's indented.");
397 char *opt_usage(const char *argv0, const char *extra);
400 * opt_usage_exit_fail - complain about bad usage to stderr, exit with status 1.
401 * @msg...: printf-style message to output.
403 * This prints argv[0] (if opt_parse has been called), a colon, then
404 * the message to stderr (just like errx()). Then it prints out the
405 * usage message, taken from any registered option which uses
406 * opt_usage_and_exit() as described in opt_usage(argv0, NULL) above.
407 * Then it exits with status 1.
411 * opt_usage_exit_fail("Need 5 arguments, only got %u", argc);
413 void opt_usage_exit_fail(const char *msg, ...) NORETURN;
416 * opt_hidden - string for undocumented options.
418 * This can be used as the desc parameter if you want an option not to be
419 * shown by opt_usage().
421 extern const char opt_hidden[];
423 /* Maximum length of arg to show in opt_usage */
424 #define OPT_SHOW_LEN 80
426 /* Standard helpers. You can write your own: */
427 /* Sets the @b to true. */
428 char *opt_set_bool(bool *b);
429 /* Sets @b based on arg: (yes/no/true/false). */
430 char *opt_set_bool_arg(const char *arg, bool *b);
431 void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b);
433 char *opt_set_invbool(bool *b);
434 void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b);
435 /* Sets @b based on !arg: (yes/no/true/false). */
436 char *opt_set_invbool_arg(const char *arg, bool *b);
439 char *opt_set_charp(const char *arg, char **p);
440 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p);
442 /* Set an integer value, various forms. Sets to 1 on arg == NULL. */
443 char *opt_set_intval(const char *arg, int *i);
444 void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i);
445 char *opt_set_uintval(const char *arg, unsigned int *ui);
446 void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui);
447 char *opt_set_longval(const char *arg, long *l);
448 void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l);
449 char *opt_set_ulongval(const char *arg, unsigned long *ul);
450 void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul);
452 /* Set an floating point value, various forms. */
453 char *opt_set_floatval(const char *arg, float *f);
454 void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f);
455 char *opt_set_doubleval(const char *arg, double *d);
456 void opt_show_doubleval(char buf[OPT_SHOW_LEN], const double *d);
458 /* the following setting functions accept k, M, G, T, P, or E suffixes, which
459 multiplies the numeric value by the corresponding power of 1000 or 1024
460 (for the _si and _bi versions, respectively).
462 char *opt_set_intval_bi(const char *arg, int *i);
463 char *opt_set_intval_si(const char *arg, int *i);
464 char *opt_set_uintval_bi(const char *arg, unsigned int *u);
465 char *opt_set_uintval_si(const char *arg, unsigned int *u);
466 char *opt_set_longval_bi(const char *arg, long *l);
467 char *opt_set_longval_si(const char *arg, long *l);
468 char *opt_set_ulongval_bi(const char *arg, unsigned long *ul);
469 char *opt_set_ulongval_si(const char *arg, unsigned long *ul);
470 char *opt_set_longlongval_bi(const char *arg, long long *ll);
471 char *opt_set_longlongval_si(const char *arg, long long *ll);
472 char *opt_set_ulonglongval_bi(const char *arg, unsigned long long *ll);
473 char *opt_set_ulonglongval_si(const char *arg, unsigned long long *ll);
476 void opt_show_intval_bi(char buf[OPT_SHOW_LEN], const int *x);
477 void opt_show_longval_bi(char buf[OPT_SHOW_LEN], const long *x);
478 void opt_show_longlongval_bi(char buf[OPT_SHOW_LEN], const long long *x);
479 void opt_show_uintval_bi(char buf[OPT_SHOW_LEN], const unsigned int *x);
480 void opt_show_ulongval_bi(char buf[OPT_SHOW_LEN], const unsigned long *x);
481 void opt_show_ulonglongval_bi(char buf[OPT_SHOW_LEN], const unsigned long long *x);
483 void opt_show_intval_si(char buf[OPT_SHOW_LEN], const int *x);
484 void opt_show_longval_si(char buf[OPT_SHOW_LEN], const long *x);
485 void opt_show_longlongval_si(char buf[OPT_SHOW_LEN], const long long *x);
486 void opt_show_uintval_si(char buf[OPT_SHOW_LEN], const unsigned int *x);
487 void opt_show_ulongval_si(char buf[OPT_SHOW_LEN], const unsigned long *x);
488 void opt_show_ulonglongval_si(char buf[OPT_SHOW_LEN], const unsigned long long *x);
493 /* Increment and decrement. */
494 char *opt_inc_intval(int *i);
495 char *opt_dec_intval(int *i);
497 /* Display version string to stdout, exit(0). */
498 char *opt_version_and_exit(const char *version);
500 /* Display usage string to stdout, exit(0). */
501 char *opt_usage_and_exit(const char *extra);
503 /* Below here are private declarations. */
504 /* You can use this directly to build tables, but the macros will ensure
505 * consistency and type safety. */
507 OPT_NOARG = 1, /* -f|--foo */
508 OPT_HASARG = 2, /* -f arg|--foo=arg|--foo arg */
509 OPT_SUBTABLE = 4, /* Actually, longopt points to a subtable... */
510 OPT_EARLY = 8, /* Parse this from opt_early_parse() only. */
511 OPT_END = 16, /* End of the table. */
515 const char *names; /* pipe-separated names, --longopt or -s */
517 char *(*cb)(void *arg); /* OPT_NOARG */
518 char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
519 void (*show)(char buf[OPT_SHOW_LEN], const void *arg);
528 /* Resolves to the four parameters for non-arg callbacks. */
529 #define OPT_CB_NOARG(cb, pre, arg) \
531 typesafe_cb_cast3(char *(*)(void *), \
532 char *(*)(typeof(*(arg))*), \
533 char *(*)(const typeof(*(arg))*), \
534 char *(*)(const void *), (cb)), \
537 /* Resolves to the four parameters for arg callbacks. */
538 #define OPT_CB_ARG(cb, pre, show, arg) \
539 OPT_HASARG|(pre), NULL, \
540 typesafe_cb_cast3(char *(*)(const char *,void *), \
541 char *(*)(const char *, typeof(*(arg))*), \
542 char *(*)(const char *, const typeof(*(arg))*), \
543 char *(*)(const char *, const void *), \
545 typesafe_cb_cast(void (*)(char buf[], const void *), \
546 void (*)(char buf[], const typeof(*(arg))*), (show))
548 /* Non-typesafe register function. */
549 void _opt_register(const char *names, enum opt_type type,
550 char *(*cb)(void *arg),
551 char *(*cb_arg)(const char *optarg, void *arg),
552 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
553 const void *arg, const char *desc);
555 /* We use this to get typechecking for OPT_SUBTABLE */
556 static inline int _check_is_entry(struct opt_table *e UNUSED) { return 0; }
558 #endif /* CCAN_OPT_H */