]> git.ozlabs.org Git - ccan/blob - ccan/opt/opt.h
9c9f337d0a61685bf301883b9a5d1d01bb93a82b
[ccan] / ccan / opt / opt.h
1 /* Licensed under GPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_OPT_H
3 #define CCAN_OPT_H
4 #include <ccan/compiler/compiler.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
6 #include <stdbool.h>
7 #include <stdlib.h>
8
9 struct opt_table;
10
11 /**
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.
17  *
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.
21  *
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.
25  *
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.
29  *
30  * See Also:
31  *      OPT_WITH_ARG()
32  */
33 #define OPT_WITHOUT_ARG(names, cb, arg, desc)   \
34         { (names), OPT_CB_NOARG((cb), 0, (arg)), { (arg) }, (desc) }
35
36 /**
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.
43  *
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.
49  *
50  * Similarly, if @show is not NULL, it should be of type "void show(char *,
51  * size_t len, const type *)".  It should write up to len bytes into the first
52  * argument; unless it uses the entire len bytes it should nul-terminate that
53  * buffer.
54  *
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.
60  *
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.
64  *
65  * See Also:
66  *      OPT_WITHOUT_ARG()
67  */
68 #define OPT_WITH_ARG(name, cb, show, arg, desc) \
69         { (name), OPT_CB_ARG((cb), 0, (show), (arg)), { (arg) }, (desc) }
70
71 /**
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.
75  */
76 #define OPT_SUBTABLE(table, desc)                                       \
77         { (const char *)(table), OPT_SUBTABLE,                          \
78           sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL,     \
79           { NULL }, (desc) }
80
81 /**
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.
87  *
88  * This is the same as OPT_WITHOUT_ARG, but for opt_early_parse() instead of
89  * opt_parse().
90  *
91  * See Also:
92  *      OPT_EARLY_WITH_ARG(), opt_early_parse()
93  */
94 #define OPT_EARLY_WITHOUT_ARG(names, cb, arg, desc)     \
95         { (names), OPT_CB_NOARG((cb), OPT_EARLY, (arg)), { (arg) }, (desc) }
96
97 /**
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.
104  *
105  * This is the same as OPT_WITH_ARG, but for opt_early_parse() instead of
106  * opt_parse().
107  *
108  * See Also:
109  *      OPT_EARLY_WITHOUT_ARG(), opt_early_parse()
110  */
111 #define OPT_EARLY_WITH_ARG(name, cb, show, arg, desc)   \
112         { (name), OPT_CB_ARG((cb), OPT_EARLY, (show), (arg)), { (arg) }, (desc) }
113
114 /**
115  * OPT_ENDTABLE - macro to create final entry in table.
116  *
117  * This must be the final element in the opt_table array.
118  */
119 #define OPT_ENDTABLE { NULL, OPT_END, NULL, NULL, NULL, { NULL }, NULL }
120
121 /**
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.
125  *
126  * The table must be terminated by OPT_ENDTABLE.
127  *
128  * Example:
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."),
138  *      OPT_ENDTABLE
139  * };
140  *
141  * ...
142  *      opt_register_table(opts, NULL);
143  */
144 void opt_register_table(const struct opt_table *table, const char *desc);
145
146 /**
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.
152  *
153  * This is used for registering a single commandline option which takes
154  * no argument.
155  *
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
158  * argument.
159  *
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.
163  */
164 #define opt_register_noarg(names, cb, arg, desc)                        \
165         _opt_register((names), OPT_CB_NOARG((cb), 0, (arg)), (arg), (desc))
166
167 /**
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.
174  *
175  * This is used for registering a single commandline option which takes
176  * an argument.
177  *
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.
182  *
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.
186  *
187  * Example:
188  * static char *explode(const char *optarg, void *unused UNNEEDED)
189  * {
190  *      errx(1, "BOOM! %s", optarg);
191  * }
192  * ...
193  *      opt_register_arg("--explode|--boom", explode, NULL, NULL, opt_hidden);
194  */
195 #define opt_register_arg(names, cb, show, arg, desc)                    \
196         _opt_register((names), OPT_CB_ARG((cb),0,(show), (arg)), (arg), (desc))
197
198 /**
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.
204  *
205  * This is the same as opt_register_noarg(), but for opt_early_parse().
206  *
207  * See Also:
208  *      opt_register_early_arg(), opt_early_parse()
209  */
210 #define opt_register_early_noarg(names, cb, arg, desc)                  \
211         _opt_register((names), OPT_CB_NOARG((cb), OPT_EARLY, (arg)),    \
212                       (arg), (desc))
213
214 /**
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.
221  *
222  * This is the same as opt_register_arg(), but for opt_early_parse().
223  *
224  * See Also:
225  *      opt_register_early_noarg(), opt_early_parse()
226  */
227 #define opt_register_early_arg(names, cb, show, arg, desc)              \
228         _opt_register((names), OPT_CB_ARG((cb), OPT_EARLY, (show),(arg)), \
229                       (arg), (desc))
230
231 /**
232  * opt_unregister - unregister an option.
233  * @names: the names it was registered with.
234  *
235  * This undoes opt_register[_early]_[no]arg.  Returns true if the option was
236  * found, otherwise false.
237  */
238 bool opt_unregister(const char *names);
239
240 /**
241  * opt_parse - parse arguments.
242  * @argc: pointer to argc
243  * @argv: argv array.
244  * @errlog: the function to print errors
245  *
246  * This iterates through the command line and calls callbacks registered with
247  * opt_register_arg()/opt_register_noarg() or OPT_WITHOUT_ARG/OPT_WITH_ARG
248  * entries in tables registered with opt_register_table().  As this occurs
249  * each option is removed from argc and argv.
250  *
251  * If there are unknown options, missing arguments or a callback
252  * returns false, then an error message is printed and false is
253  * returned: the erroneous option is not removed.
254  *
255  * On success, argc and argv will contain only the non-option
256  * elements, and true is returned.
257  *
258  * Example:
259  *      if (!opt_parse(&argc, argv, opt_log_stderr)) {
260  *              printf("You screwed up, aborting!\n");
261  *              exit(1);
262  *      }
263  *
264  * See Also:
265  *      opt_log_stderr, opt_log_stderr_exit, opt_early_parse()
266  */
267 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
268
269 /**
270  * opt_early_parse - parse early arguments.
271  * @argc: argc
272  * @argv: argv array.
273  * @errlog: the function to print errors
274  *
275  * There are times when you want to parse some arguments before any other
276  * arguments; this is especially important for debugging flags (eg. --verbose)
277  * when you have complicated callbacks in option processing.
278  *
279  * You can use opt_early_parse() to only parse options registered with
280  * opt_register_earlyarg()/opt_register_early_noarg() or
281  * OPT_EARLY_WITHOUT_ARG/OPT_EARLY_WITH_ARG entries in tables registered with
282  * opt_register_table().
283  *
284  * Note that unlike opt_parse(), argc and argv are not altered.
285  *
286  * Example:
287  *      if (!opt_early_parse(argc, argv, opt_log_stderr)) {
288  *              printf("You screwed up, aborting!\n");
289  *              exit(1);
290  *      }
291  *
292  * See Also:
293  *      opt_parse()
294  */
295 bool opt_early_parse(int argc, char *argv[],
296                      void (*errlog)(const char *fmt, ...));
297
298 /**
299  * opt_early_parse_incomplete - parse early arguments, ignoring unknown ones.
300  * @argc: argc
301  * @argv: argv array.
302  * @errlog: the function to print errors
303  *
304  * If you have plugins, you might need to do early parsing (eg. to find the
305  * plugin directory) but you don't know what options the plugins will want.
306  *
307  * Thus, this function is just like opt_early_parse, but ignores unknown options.
308  *
309  * Example:
310  *      if (!opt_early_parse_incomplete(argc, argv, opt_log_stderr)) {
311  *              printf("You screwed up, aborting!\n");
312  *              exit(1);
313  *      }
314  *
315  * See Also:
316  *      opt_early_parse()
317  */
318 bool opt_early_parse_incomplete(int argc, char *argv[],
319                                 void (*errlog)(const char *fmt, ...));
320
321
322 /**
323  * opt_free_table - reset the opt library.
324  *
325  * This frees the internal memory and returns counters to zero.  Call
326  * this as the last opt function to avoid memory leaks.  You can also
327  * use this function to reset option handling to its initial state (no
328  * options registered).
329  */
330 void opt_free_table(void);
331
332 /**
333  * opt_set_alloc - set alloc/realloc/free function for opt to use.
334  * @allocfn: allocator function
335  * @reallocfn: reallocator function, ptr may be NULL, size never 0.
336  * @freefn: free function
337  *
338  * By default opt uses malloc/realloc/free, and simply crashes if they fail.
339  * You can set your own variants here.
340  */
341 void opt_set_alloc(void *(*allocfn)(size_t size),
342                    void *(*reallocfn)(void *ptr, size_t size),
343                    void (*freefn)(void *ptr));
344
345 /**
346  * opt_log_stderr - print message to stderr.
347  * @fmt: printf-style format.
348  *
349  * This is a helper for opt_parse, to print errors to stderr.
350  *
351  * See Also:
352  *      opt_log_stderr_exit
353  */
354 void opt_log_stderr(const char *fmt, ...);
355
356 /**
357  * opt_log_stderr_exit - print message to stderr, then exit(1)
358  * @fmt: printf-style format.
359  *
360  * Just like opt_log_stderr, only then does exit(1).  This means that
361  * when handed to opt_parse, opt_parse will never return false.
362  *
363  * Example:
364  *      // This never returns false; just exits if there's an erorr.
365  *      opt_parse(&argc, argv, opt_log_stderr_exit);
366  */
367 void opt_log_stderr_exit(const char *fmt, ...);
368
369 /**
370  * opt_invalid_argument - helper to allocate an "Invalid argument '%s'" string
371  * @arg: the argument which was invalid.
372  *
373  * This is a helper for callbacks to return a simple error string.
374  */
375 char *opt_invalid_argument(const char *arg);
376
377 /**
378  * opt_usage - create usage message
379  * @argv0: the program name
380  * @extra: extra details to print after the initial command, or NULL.
381  *
382  * Creates a usage message, with the program name, arguments, some extra details
383  * and a table of all the options with their descriptions.  If an option has
384  * description opt_hidden, it is not shown here.
385  *
386  * The table of options is formatted such that descriptions are
387  * wrapped on space boundaries.  If a description has a "\n" that is
388  * left intact, and the following characters indented appropriately.
389  * If the description begins with one or more space/tab (or has a
390  * space or tab following a "\n") that line is output without wrapping.
391  *
392  * If "extra" is NULL, then the extra information is taken from any
393  * registered option which calls opt_usage_and_exit().  This avoids duplicating
394  * that string in the common case.
395  *
396  * The result should be passed to free().
397  *
398  * See Also:
399  *      opt_usage_and_exit()
400  *
401  * Example:
402  *      opt_register_arg("--explode|--boom", explode, NULL, NULL,
403  *                       "This line will be wrapped by opt_usage\n"
404  *                       "  But this won't because it's indented.");
405  */
406 char *opt_usage(const char *argv0, const char *extra);
407
408 /**
409  * opt_usage_exit_fail - complain about bad usage to stderr, exit with status 1.
410  * @msg...: printf-style message to output.
411  *
412  * This prints argv[0] (if opt_parse has been called), a colon, then
413  * the message to stderr (just like errx()).  Then it prints out the
414  * usage message, taken from any registered option which uses
415  * opt_usage_and_exit() as described in opt_usage(argv0, NULL) above.
416  * Then it exits with status 1.
417  *
418  * Example:
419  *      if (argc != 5)
420  *              opt_usage_exit_fail("Need 5 arguments, only got %u", argc);
421  */
422 void opt_usage_exit_fail(const char *msg, ...) NORETURN;
423
424 /**
425  * opt_hidden - string for undocumented options.
426  *
427  * This can be used as the desc parameter if you want an option not to be
428  * shown by opt_usage().
429  */
430 extern const char opt_hidden[];
431
432 /* Standard helpers.  You can write your own: */
433 /* Sets the @b to true. */
434 char *opt_set_bool(bool *b);
435 /* Sets @b based on arg: (yes/no/true/false). */
436 char *opt_set_bool_arg(const char *arg, bool *b);
437 void opt_show_bool(char *buf, size_t len, const bool *b);
438 /* The inverse */
439 char *opt_set_invbool(bool *b);
440 void opt_show_invbool(char *buf, size_t len, const bool *b);
441 /* Sets @b based on !arg: (yes/no/true/false). */
442 char *opt_set_invbool_arg(const char *arg, bool *b);
443
444 /* Set a char *. */
445 char *opt_set_charp(const char *arg, char **p);
446 void opt_show_charp(char *buf, size_t len, char *const *p);
447
448 /* Set an integer value, various forms.  Sets to 1 on arg == NULL. */
449 char *opt_set_intval(const char *arg, int *i);
450 void opt_show_intval(char *buf, size_t len, const int *i);
451 char *opt_set_uintval(const char *arg, unsigned int *ui);
452 void opt_show_uintval(char *buf, size_t len, const unsigned int *ui);
453 char *opt_set_longval(const char *arg, long *l);
454 void opt_show_longval(char *buf, size_t len, const long *l);
455 char *opt_set_ulongval(const char *arg, unsigned long *ul);
456 void opt_show_ulongval(char *buf, size_t len, const unsigned long *ul);
457
458 /* Set an floating point value, various forms. */
459 char *opt_set_floatval(const char *arg, float *f);
460 void opt_show_floatval(char *buf, size_t len, const float *f);
461 char *opt_set_doubleval(const char *arg, double *d);
462 void opt_show_doubleval(char *buf, size_t len, const double *d);
463
464 /* the following setting functions accept k, M, G, T, P, or E suffixes, which
465    multiplies the numeric value by the corresponding power of 1000 or 1024
466    (for the _si and _bi versions, respectively).
467  */
468 char *opt_set_intval_bi(const char *arg, int *i);
469 char *opt_set_intval_si(const char *arg, int *i);
470 char *opt_set_uintval_bi(const char *arg, unsigned int *u);
471 char *opt_set_uintval_si(const char *arg, unsigned int *u);
472 char *opt_set_longval_bi(const char *arg, long *l);
473 char *opt_set_longval_si(const char *arg, long *l);
474 char *opt_set_ulongval_bi(const char *arg, unsigned long *ul);
475 char *opt_set_ulongval_si(const char *arg, unsigned long *ul);
476 char *opt_set_longlongval_bi(const char *arg, long long *ll);
477 char *opt_set_longlongval_si(const char *arg, long long *ll);
478 char *opt_set_ulonglongval_bi(const char *arg, unsigned long long *ll);
479 char *opt_set_ulonglongval_si(const char *arg, unsigned long long *ll);
480
481
482 void opt_show_intval_bi(char *buf, size_t len, const int *x);
483 void opt_show_longval_bi(char *buf, size_t len, const long *x);
484 void opt_show_longlongval_bi(char *buf, size_t len, const long long *x);
485 void opt_show_uintval_bi(char *buf, size_t len, const unsigned int *x);
486 void opt_show_ulongval_bi(char *buf, size_t len, const unsigned long *x);
487 void opt_show_ulonglongval_bi(char *buf, size_t len, const unsigned long long *x);
488
489 void opt_show_intval_si(char *buf, size_t len, const int *x);
490 void opt_show_longval_si(char *buf, size_t len, const long *x);
491 void opt_show_longlongval_si(char *buf, size_t len, const long long *x);
492 void opt_show_uintval_si(char *buf, size_t len, const unsigned int *x);
493 void opt_show_ulongval_si(char *buf, size_t len, const unsigned long *x);
494 void opt_show_ulonglongval_si(char *buf, size_t len, const unsigned long long *x);
495
496
497
498
499 /* Increment and decrement. */
500 char *opt_inc_intval(int *i);
501 char *opt_dec_intval(int *i);
502
503 /* Display version string to stdout, exit(0). */
504 char *opt_version_and_exit(const char *version);
505
506 /* Display usage string to stdout, exit(0). */
507 char *opt_usage_and_exit(const char *extra);
508
509 /**
510  * opt_find_long: low-level access to the parser
511  * @arg: string of form 'arg' or 'arg=val'.
512  * @optarg: set to `val` of present in arg, otherwise NULL.  Can be NULL.
513  *
514  * Returns NULL if option is unknown.  Sets *@optarg to NULL if
515  * there's no '='.
516  */
517 struct opt_table *opt_find_long(const char *arg, const char **optarg);
518
519 /**
520  * opt_find_short: low-level access to the parser
521  * @arg: character representing short option
522  *
523  * Returns NULL if option is unknown.
524  */
525 struct opt_table *opt_find_short(char arg);
526
527 /* opt_type bits reserved for users to play with (ignored!).
528  * You can set bits in type e.g. (1<<OPT_USER_START) to (1<<OPT_USER_END)
529  * when calling _opt_register. */
530 #define OPT_USER_START 8
531 #define OPT_USER_END 15
532
533 /* Below here are private declarations. */
534 /* You can use this directly to build tables, but the macros will ensure
535  * consistency and type safety. */
536 enum opt_type {
537         OPT_NOARG = 1,          /* -f|--foo */
538         OPT_HASARG = 2,         /* -f arg|--foo=arg|--foo arg */
539         OPT_SUBTABLE = 4,       /* Actually, longopt points to a subtable... */
540         OPT_EARLY = 8,          /* Parse this from opt_early_parse() only. */
541         OPT_END = 16,           /* End of the table. */
542
543         /* Make sure no compiler will assume we never have large
544          * values in the enum! */
545         OPT_USER_MIN = (1 << OPT_USER_START),
546         OPT_USER_MAX = (1 << OPT_USER_END),
547 };
548
549 struct opt_table {
550         const char *names; /* pipe-separated names, --longopt or -s */
551         enum opt_type type;
552         char *(*cb)(void *arg); /* OPT_NOARG */
553         char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
554         void (*show)(char *buf, size_t len, const void *arg);
555         union {
556                 const void *carg;
557                 void *arg;
558                 size_t tlen;
559         } u;
560         const char *desc;
561 };
562
563 /* Resolves to the four parameters for non-arg callbacks. */
564 #define OPT_CB_NOARG(cb, pre, arg)                      \
565         OPT_NOARG|(pre),                                \
566         typesafe_cb_cast3(char *(*)(void *),    \
567                           char *(*)(typeof(*(arg))*),   \
568                           char *(*)(const typeof(*(arg))*),     \
569                           char *(*)(const void *), (cb)),       \
570         NULL, NULL
571
572 /* Resolves to the four parameters for arg callbacks. */
573 #define OPT_CB_ARG(cb, pre, show, arg)                                  \
574         OPT_HASARG|(pre), NULL,                                         \
575         typesafe_cb_cast3(char *(*)(const char *,void *),       \
576                           char *(*)(const char *, typeof(*(arg))*),     \
577                           char *(*)(const char *, const typeof(*(arg))*), \
578                           char *(*)(const char *, const void *),        \
579                           (cb)),                                        \
580         typesafe_cb_cast(void (*)(char *buf, size_t, const void *), \
581                          void (*)(char *buf, size_t, const typeof(*(arg))*), (show))
582
583 /* Non-typesafe register function. */
584 void _opt_register(const char *names, enum opt_type type,
585                    char *(*cb)(void *arg),
586                    char *(*cb_arg)(const char *optarg, void *arg),
587                    void (*show)(char *buf, size_t len, const void *arg),
588                    const void *arg, const char *desc);
589
590 /* We use this to get typechecking for OPT_SUBTABLE */
591 static inline int _check_is_entry(struct opt_table *e UNUSED) { return 0; }
592
593 #endif /* CCAN_OPT_H */