]> git.ozlabs.org Git - ccan/blob - ccan/opt/test/utils.c
opt: allow show callbacks to return false.
[ccan] / ccan / opt / test / utils.c
1 #include "config.h"
2 #include <ccan/tap/tap.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <ccan/opt/opt.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include "utils.h"
9
10 unsigned int test_cb_called;
11 char *test_noarg(void *arg UNNEEDED)
12 {
13         test_cb_called++;
14         return NULL;
15 }
16
17 char *test_arg(const char *optarg, const char *arg)
18 {
19         test_cb_called++;
20         ok1(strcmp(optarg, arg) == 0);
21         return NULL;
22 }
23
24 bool show_arg(char *buf, size_t len, const char *arg)
25 {
26         strncpy(buf, arg, len);
27         return true;
28 }
29
30 char *err_output = NULL;
31
32 void save_err_output(const char *fmt, ...)
33 {
34         va_list ap;
35         char *p;
36
37         va_start(ap, fmt);
38         /* Check return, for fascist gcc */
39         if (vasprintf(&p, fmt, ap) == -1)
40                 p = NULL;
41         va_end(ap);
42
43         if (err_output) {
44                 err_output = realloc(err_output,
45                                      strlen(err_output) + strlen(p) + 1);
46                 strcat(err_output, p);
47                 free(p);
48         } else
49                 err_output = p;
50 }       
51
52 void reset_options(void)
53 {
54         opt_free_table();
55         free(err_output);
56         err_output = NULL;
57 }
58
59 static bool allocated = false;
60
61 bool parse_args(int *argc, char ***argv, ...)
62 {
63         char **a;
64         va_list ap;
65
66         va_start(ap, argv);
67         *argc = 1;
68         a = malloc(sizeof(*a) * (*argc + 1));
69         a[0] = (*argv)[0];
70         while ((a[*argc] = va_arg(ap, char *)) != NULL) {
71                 (*argc)++;
72                 a = realloc(a, sizeof(*a) * (*argc + 1));
73         }
74
75         if (allocated)
76                 free(*argv);
77
78         *argv = a;
79         allocated = true;
80
81         return opt_parse(argc, *argv, save_err_output);
82 }
83
84 bool parse_early_args(int *argc, char ***argv, ...)
85 {
86         char **a;
87         va_list ap;
88
89         va_start(ap, argv);
90         *argc = 1;
91         a = malloc(sizeof(*a) * (*argc + 1));
92         a[0] = (*argv)[0];
93         while ((a[*argc] = va_arg(ap, char *)) != NULL) {
94                 (*argc)++;
95                 a = realloc(a, sizeof(*a) * (*argc + 1));
96         }
97
98         if (allocated)
99                 free(*argv);
100
101         *argv = a;
102         allocated = true;
103
104         return opt_early_parse(*argc, *argv, save_err_output);
105 }
106
107 bool parse_early_args_incomplete(int *argc, char ***argv, ...)
108 {
109         char **a;
110         va_list ap;
111
112         va_start(ap, argv);
113         *argc = 1;
114         a = malloc(sizeof(*a) * (*argc + 1));
115         a[0] = (*argv)[0];
116         while ((a[*argc] = va_arg(ap, char *)) != NULL) {
117                 (*argc)++;
118                 a = realloc(a, sizeof(*a) * (*argc + 1));
119         }
120
121         if (allocated)
122                 free(*argv);
123
124         *argv = a;
125         allocated = true;
126
127         return opt_early_parse_incomplete(*argc, *argv, save_err_output);
128 }
129
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"),
134         OPT_ENDTABLE
135 };
136
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", ""),
141         OPT_ENDTABLE
142 };
143
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"),
148         OPT_ENDTABLE
149 };
150
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", ""),
155         /* Hidden option */
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),
160         OPT_ENDTABLE
161 };