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, const char *arg)
21 ok1(strcmp(optarg, arg) == 0);
25 void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
27 strncpy(buf, arg, OPT_SHOW_LEN);
30 char *err_output = NULL;
32 static void save_err_output(const char *fmt, ...)
38 vasprintf(&p, fmt, ap);
42 err_output = realloc(err_output,
43 strlen(err_output) + strlen(p) + 1);
44 strcat(err_output, p);
50 /* FIXME: This leaks, BTW. */
51 bool parse_args(int *argc, char ***argv, ...)
58 a = malloc(sizeof(*a) * (*argc + 1));
60 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
62 a = realloc(a, sizeof(*a) * (*argc + 1));
65 /* Re-set before parsing. */
68 return opt_parse(argc, *argv, save_err_output);
71 struct opt_table short_table[] = {
72 /* Short opts, different args. */
73 { OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a") },
74 { OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b") },
78 struct opt_table long_table[] = {
79 /* Long opts, different args. */
80 { OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd") },
81 { OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", "") },
85 struct opt_table long_and_short_table[] = {
86 /* Short and long, different args. */
87 { OPT_WITHOUT_ARG("--ggg/-g", test_noarg, "ggg",
88 "Description of ggg") },
89 { OPT_WITH_ARG("-h/--hhh", test_arg, NULL, "hhh",
90 "Description of hhh") },
95 struct opt_table subtables[] = {
96 /* Two short, and two long long, no description */
97 { OPT_WITH_ARG("--jjj/-j/--lll/-l", test_arg, show_arg, "jjj", "") },
99 { OPT_WITH_ARG("--mmm/-m", test_arg, show_arg, "mmm", opt_hidden) },
100 OPT_SUBTABLE(short_table, NULL),
101 OPT_SUBTABLE(long_table, "long table options"),
102 OPT_SUBTABLE(long_and_short_table, NULL),