2 #include <ccan/tap/tap.h>
5 #include <ccan/opt/opt.h>
11 unsigned int test_cb_called;
12 char *test_noarg(void *arg)
18 char *test_arg(const char *optarg, void *arg)
21 ok1(strcmp(optarg, arg) == 0);
25 char *err_output = NULL;
27 static void save_err_output(const char *fmt, ...)
33 vasprintf(&p, fmt, ap);
37 err_output = realloc(err_output,
38 strlen(err_output) + strlen(p) + 1);
39 strcat(err_output, p);
45 /* FIXME: This leaks, BTW. */
46 bool parse_args(int *argc, char ***argv, ...)
53 a = malloc(sizeof(*a) * (*argc + 1));
55 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
57 a = realloc(a, sizeof(*a) * (*argc + 1));
60 /* Re-set before parsing. */
63 return opt_parse(argc, *argv, save_err_output);
66 struct opt_table short_table[] = {
67 /* Short opts, different args. */
68 { OPT_WITHOUT_ARG(NULL, 'a', test_noarg, "a"), "Description of a" },
69 { OPT_WITH_ARG(NULL, 'b', test_arg, "b"), "Description of b" },
73 struct opt_table long_table[] = {
74 /* Long opts, different args. */
75 { OPT_WITHOUT_ARG("ddd", 0, test_noarg, "ddd"), "Description of ddd" },
76 { OPT_WITH_ARG("eee", 0, test_arg, "eee"), "Description of eee" },
80 struct opt_table long_and_short_table[] = {
81 /* Short and long, different args. */
82 { OPT_WITHOUT_ARG("ggg", 'g', test_noarg, "ggg"),
83 "Description of ggg" },
84 { OPT_WITH_ARG("hhh", 'h', test_arg, "hhh"), "Description of hhh"},
89 struct opt_table subtables[] = {
90 /* Short and long, no description */
91 { OPT_WITH_ARG("jjj", 'j', test_arg, "jjj") },
92 OPT_SUBTABLE(short_table, NULL),
93 OPT_SUBTABLE(long_table, "long table options"),
94 OPT_SUBTABLE(long_and_short_table, opt_table_hidden),