2 #include <ccan/tap/tap.h>
5 #include <ccan/opt/opt.h>
10 unsigned int test_cb_called;
11 char *test_noarg(void *arg)
17 char *test_arg(const char *optarg, const char *arg)
20 ok1(strcmp(optarg, arg) == 0);
24 void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
26 strncpy(buf, arg, OPT_SHOW_LEN);
29 char *err_output = NULL;
31 void save_err_output(const char *fmt, ...)
37 /* Check return, for fascist gcc */
38 if (vasprintf(&p, fmt, ap) == -1)
43 err_output = realloc(err_output,
44 strlen(err_output) + strlen(p) + 1);
45 strcat(err_output, p);
51 void reset_options(void)
58 static bool allocated = false;
60 bool parse_args(int *argc, char ***argv, ...)
67 a = malloc(sizeof(*a) * (*argc + 1));
69 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
71 a = realloc(a, sizeof(*a) * (*argc + 1));
80 return opt_parse(argc, *argv, save_err_output);
83 struct opt_table short_table[] = {
84 /* Short opts, different args. */
85 OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
86 OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
90 struct opt_table long_table[] = {
91 /* Long opts, different args. */
92 OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
93 OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
97 struct opt_table long_and_short_table[] = {
98 /* Short and long, different args. */
99 OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
100 OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
104 /* Sub-table test. */
105 struct opt_table subtables[] = {
106 /* Two short, and two long long, no description */
107 OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
109 OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
110 OPT_SUBTABLE(short_table, NULL),
111 OPT_SUBTABLE(long_table, "long table options"),
112 OPT_SUBTABLE(long_and_short_table, NULL),