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 bool parse_early_args(int *argc, char ***argv, ...)
90 a = malloc(sizeof(*a) * (*argc + 1));
92 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
94 a = realloc(a, sizeof(*a) * (*argc + 1));
103 return opt_early_parse(*argc, *argv, save_err_output);
106 struct opt_table short_table[] = {
107 /* Short opts, different args. */
108 OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
109 OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
113 struct opt_table long_table[] = {
114 /* Long opts, different args. */
115 OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
116 OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
120 struct opt_table long_and_short_table[] = {
121 /* Short and long, different args. */
122 OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
123 OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
127 /* Sub-table test. */
128 struct opt_table subtables[] = {
129 /* Two short, and two long long, no description */
130 OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
132 OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
133 OPT_SUBTABLE(short_table, NULL),
134 OPT_SUBTABLE(long_table, "long table options"),
135 OPT_SUBTABLE(long_and_short_table, NULL),