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 bool show_arg(char *buf, size_t len, const char *arg)
26 strncpy(buf, arg, len);
30 char *err_output = NULL;
32 void save_err_output(const char *fmt, ...)
38 /* Check return, for fascist gcc */
39 if (vasprintf(&p, fmt, ap) == -1)
44 err_output = realloc(err_output,
45 strlen(err_output) + strlen(p) + 1);
46 strcat(err_output, p);
52 void reset_options(void)
59 static bool allocated = false;
61 bool parse_args(int *argc, char ***argv, ...)
68 a = malloc(sizeof(*a) * (*argc + 1));
70 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
72 a = realloc(a, sizeof(*a) * (*argc + 1));
81 return opt_parse(argc, *argv, save_err_output);
84 bool parse_early_args(int *argc, char ***argv, ...)
91 a = malloc(sizeof(*a) * (*argc + 1));
93 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
95 a = realloc(a, sizeof(*a) * (*argc + 1));
104 return opt_early_parse(*argc, *argv, save_err_output);
107 bool parse_early_args_incomplete(int *argc, char ***argv, ...)
114 a = malloc(sizeof(*a) * (*argc + 1));
116 while ((a[*argc] = va_arg(ap, char *)) != NULL) {
118 a = realloc(a, sizeof(*a) * (*argc + 1));
127 return opt_early_parse_incomplete(*argc, *argv, save_err_output);
130 struct opt_table short_table[] = {
131 /* Short opts, different args. */
132 OPT_WITHOUT_ARG("-a", test_noarg, "a", "Description of a"),
133 OPT_WITH_ARG("-b", test_arg, show_arg, "b", "Description of b"),
137 struct opt_table long_table[] = {
138 /* Long opts, different args. */
139 OPT_WITHOUT_ARG("--ddd", test_noarg, "ddd", "Description of ddd"),
140 OPT_WITH_ARG("--eee <filename>", test_arg, show_arg, "eee", ""),
144 struct opt_table long_and_short_table[] = {
145 /* Short and long, different args. */
146 OPT_WITHOUT_ARG("--ggg|-g", test_noarg, "ggg", "Description of ggg"),
147 OPT_WITH_ARG("-h|--hhh", test_arg, NULL, "hhh", "Description of hhh"),
151 /* Sub-table test. */
152 struct opt_table subtables[] = {
153 /* Two short, and two long long, no description */
154 OPT_WITH_ARG("--jjj|-j|--lll|-l", test_arg, show_arg, "jjj", ""),
156 OPT_WITH_ARG("--mmm|-m", test_arg, show_arg, "mmm", opt_hidden),
157 OPT_SUBTABLE(short_table, NULL),
158 OPT_SUBTABLE(long_table, "long table options"),
159 OPT_SUBTABLE(long_and_short_table, NULL),