2 #include <ccan/tap/tap.h>
5 #include <ccan/opt/opt.h>
10 unsigned int test_cb_called;
11 char *test_noarg(void *arg UNNEEDED)
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 bool parse_early_args_incomplete(int *argc, char ***argv, ...)
113 a = malloc(sizeof(*a) * (*argc + 1));
115 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
117 a = realloc(a, sizeof(*a) * (*argc + 1));
126 return opt_early_parse_incomplete(*argc, *argv, save_err_output);
129 struct opt_table short_table[] = {
130 /* Short opts, different args. */
131 OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
132 OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
136 struct opt_table long_table[] = {
137 /* Long opts, different args. */
138 OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
139 OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
143 struct opt_table long_and_short_table[] = {
144 /* Short and long, different args. */
145 OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
146 OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
150 /* Sub-table test. */
151 struct opt_table subtables[] = {
152 /* Two short, and two long long, no description */
153 OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
155 OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
156 OPT_SUBTABLE(short_table, NULL),
157 OPT_SUBTABLE(long_table, "long table options"),
158 OPT_SUBTABLE(long_and_short_table, NULL),