1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009-2014 Free Software Foundation, Inc.
3 This file is part of the GNUlib Library.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "argp.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #if HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28 #include "progname.h"
29
30 struct test_args
31 {
32 int test;
33 int verbose;
34 char *file;
35 int read;
36 char *hidden;
37 int opt;
38 char *optional;
39 int optional_set;
40 int group_2_1_option;
41 int group_1_1_option;
42 };
43
44 static struct argp_option group1_option[] = {
45 { NULL, 0, NULL, 0, "Option Group 1", 0 },
46 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
47 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
48 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
49 { "read", 'r', NULL, OPTION_ALIAS, NULL, 1 },
50 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
51 { NULL, 0, NULL, 0, NULL, 0 }
52 };
53
54 static error_t
group1_parser(int key,char * arg,struct argp_state * state)55 group1_parser (int key, char *arg, struct argp_state *state)
56 {
57 struct test_args *args = state->input;
58
59 switch (key)
60 {
61 case 'v':
62 args->verbose++;
63 break;
64
65 case 'r':
66 args->read = 1;
67 /* fall through */
68 case 'f':
69 args->file = arg;
70 break;
71
72 case 'H':
73 args->hidden = arg;
74 break;
75
76 default:
77 return ARGP_ERR_UNKNOWN;
78 }
79 return 0;
80 }
81
82 struct argp group1_argp = {
83 group1_option,
84 group1_parser
85 };
86
87 struct argp_child group1_child = {
88 &group1_argp,
89 0,
90 "",
91 1
92 };
93
94
95 static struct argp_option group1_1_option[] = {
96 { NULL, 0, NULL, 0, "Option Group 1.1", 0 },
97 { "cantiga", 'C', NULL, 0, "create a cantiga" },
98 { "sonet", 'S', NULL, 0, "create a sonet" },
99 { NULL, 0, NULL, 0, NULL, 0 }
100 };
101
102 static error_t
group1_1_parser(int key,char * arg,struct argp_state * state)103 group1_1_parser (int key, char *arg, struct argp_state *state)
104 {
105 struct test_args *args = state->input;
106 switch (key)
107 {
108 case 'C':
109 case 'S':
110 args->group_1_1_option = key;
111 break;
112 default:
113 return ARGP_ERR_UNKNOWN;
114 }
115 return 0;
116 }
117
118 struct argp group1_1_argp = {
119 group1_1_option,
120 group1_1_parser
121 };
122
123 struct argp_child group1_1_child = {
124 &group1_1_argp,
125 0,
126 "",
127 2
128 };
129
130
131 static struct argp_option group2_option[] = {
132 { NULL, 0, NULL, 0, "Option Group 2", 0 },
133 { "option", 'O', NULL, 0, "An option", 1 },
134 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
135 "Option with an optional argument. ARG is one of the following:", 2 },
136 { "one", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "one unit", 3 },
137 { "two", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "two units", 3 },
138 { "many", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "many units", 3 },
139 { NULL, 0, NULL, 0, NULL, 0 }
140 };
141
142 static error_t
group2_parser(int key,char * arg,struct argp_state * state)143 group2_parser (int key, char *arg, struct argp_state *state)
144 {
145 struct test_args *args = state->input;
146
147 switch (key)
148 {
149 case 'O':
150 args->opt = 1;
151 break;
152
153 case 'o':
154 args->optional_set = 1;
155 args->optional = arg;
156 break;
157
158 default:
159 return ARGP_ERR_UNKNOWN;
160 }
161 return 0;
162 }
163
164 struct argp group2_argp = {
165 group2_option,
166 group2_parser
167 };
168
169 struct argp_child group2_child = {
170 &group2_argp,
171 0,
172 "",
173 2
174 };
175
176
177 static struct argp_option group2_1_option[] = {
178 { NULL, 0, NULL, 0, "Option Group 2.1", 0 },
179 { "poem", 'p', NULL, 0, "create a poem" },
180 { "limerick", 'l', NULL, 0, "create a limerick" },
181 { NULL, 0, NULL, 0, NULL, 0 }
182 };
183
184 static error_t
group2_1_parser(int key,char * arg,struct argp_state * state)185 group2_1_parser (int key, char *arg, struct argp_state *state)
186 {
187 struct test_args *args = state->input;
188 switch (key)
189 {
190 case 'p':
191 case 'e':
192 args->group_2_1_option = key;
193 break;
194 default:
195 return ARGP_ERR_UNKNOWN;
196 }
197 return 0;
198 }
199
200 struct argp group2_1_argp = {
201 group2_1_option,
202 group2_1_parser
203 };
204
205 struct argp_child group2_1_child = {
206 &group2_1_argp,
207 0,
208 "",
209 2
210 };
211
212
213 static struct argp_option main_options[] = {
214 { NULL, 0, NULL, 0, "Main options", 0 },
215 { "test", 't', NULL, 0, NULL, 1 },
216 { NULL, 0, NULL, 0, NULL, 0 }
217 };
218
219 static error_t
parse_opt(int key,char * arg,struct argp_state * state)220 parse_opt (int key, char *arg, struct argp_state *state)
221 {
222 struct test_args *args = state->input;
223 int i;
224
225 switch (key)
226 {
227 case ARGP_KEY_INIT:
228 for (i = 0; state->root_argp->children[i].argp; i++)
229 state->child_inputs[i] = args;
230 break;
231
232 case 't':
233 args->test = 1;
234 break;
235
236 default:
237 return ARGP_ERR_UNKNOWN;
238 }
239 return 0;
240 }
241
242 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
243 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
244 static char doc[] = "documentation string";
245
246 struct argp test_argp = {
247 main_options,
248 parse_opt,
249 "ARGS...",
250 doc,
251 NULL,
252 NULL,
253 NULL
254 };
255
256 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
257 #define ARGV0 "test-argp"
258 #define init_args(a) memset (&(a), 0, sizeof (a));
259
260 #define INIT_TEST_COMMON(n) \
261 int argc = NARGS (argv); \
262 struct test_args test_args; \
263 init_args (test_args); \
264 test_number = n;
265
266 #define INIT_TEST1(n, arg1) \
267 char *argv[] = { ARGV0, arg1, NULL }; \
268 INIT_TEST_COMMON (n)
269
270 #define INIT_TEST2(n, arg1, arg2) \
271 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
272 INIT_TEST_COMMON (n)
273
274 #define INIT_TEST3(n, arg1, arg2, arg3) \
275 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
276 INIT_TEST_COMMON (n)
277
278 int test_number;
279 unsigned failure_count = 0;
280
281 void
fail(const char * msg)282 fail (const char *msg)
283 {
284 fprintf (stderr, "Test %d: %s\n", test_number, msg);
285 failure_count++;
286 }
287
288 void
test1(struct argp * argp)289 test1 (struct argp *argp)
290 {
291 INIT_TEST1 (1, "--test");
292 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
293 fail ("argp_parse failed");
294 else if (test_args.test != 1)
295 fail ("option not processed");
296 }
297
298 void
test2(struct argp * argp)299 test2 (struct argp *argp)
300 {
301 INIT_TEST1 (2, "-t");
302 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
303 fail ("argp_parse failed");
304 else if (test_args.test != 1)
305 fail ("option not processed");
306 }
307
308 void
test_file(struct argp * argp,int argc,char ** argv,struct test_args * args)309 test_file (struct argp *argp, int argc, char **argv, struct test_args *args)
310 {
311 if (argp_parse (argp, argc, argv, 0, NULL, args))
312 fail ("argp_parse failed");
313 else if (!args->file)
314 fail ("option not processed");
315 else if (strcmp (args->file, "FILE"))
316 fail ("option processed incorrectly");
317 }
318
319 void
test3(struct argp * argp)320 test3 (struct argp *argp)
321 {
322 INIT_TEST1 (3, "--file=FILE");
323 test_file (argp, argc, argv, &test_args);
324 }
325
326 void
test4(struct argp * argp)327 test4 (struct argp *argp)
328 {
329 INIT_TEST2 (4, "--file", "FILE");
330 test_file (argp, argc, argv, &test_args);
331 }
332
333 void
test5(struct argp * argp)334 test5 (struct argp *argp)
335 {
336 INIT_TEST1 (5, "--input=FILE");
337 test_file (argp, argc, argv, &test_args);
338 }
339
340 void
test6(struct argp * argp)341 test6 (struct argp *argp)
342 {
343 INIT_TEST2 (6, "--input", "FILE");
344 test_file (argp, argc, argv, &test_args);
345 }
346
347 void
test_optional(struct argp * argp,int argc,char ** argv,struct test_args * args,const char * val,const char * a)348 test_optional (struct argp *argp, int argc, char **argv,
349 struct test_args *args, const char *val, const char *a)
350 {
351 int index;
352 if (argp_parse (argp, argc, argv, 0, &index, args))
353 fail ("argp_parse failed");
354 else if (!args->optional_set)
355 fail ("option not processed");
356
357 if (!val)
358 {
359 if (args->optional)
360 fail ("option processed incorrectly");
361 }
362 else if (strcmp (args->optional, val))
363 fail ("option processed incorrectly");
364
365 if (a)
366 {
367 if (index == argc)
368 fail ("expected command line argument not found");
369 else if (strcmp (argv[index], a))
370 fail ("expected command line argument does not match");
371 }
372 }
373
374 void
test7(struct argp * argp)375 test7 (struct argp *argp)
376 {
377 INIT_TEST1 (7, "-oARG");
378 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
379 }
380
381 void
test8(struct argp * argp)382 test8 (struct argp *argp)
383 {
384 INIT_TEST2 (8, "-o", "ARG");
385 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
386 }
387
388 void
test9(struct argp * argp)389 test9 (struct argp *argp)
390 {
391 INIT_TEST1 (9, "--optional=ARG");
392 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
393 }
394
395 void
test10(struct argp * argp)396 test10 (struct argp *argp)
397 {
398 INIT_TEST2 (10, "--optional", "ARG");
399 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
400 }
401
402 void
test11(struct argp * argp)403 test11 (struct argp *argp)
404 {
405 INIT_TEST1 (11, "--optiona=ARG");
406 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
407 }
408
409 void
test12(struct argp * argp)410 test12 (struct argp *argp)
411 {
412 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
413 test_optional (argp, argc, argv, &test_args, "OPT", "FILE");
414 }
415
416 void
test13(struct argp * argp)417 test13 (struct argp *argp)
418 {
419 INIT_TEST1 (1, "--cantiga");
420 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
421 fail ("argp_parse failed");
422 else if (test_args.group_1_1_option != 'C')
423 fail ("option not processed");
424 }
425
426 void
test14(struct argp * argp)427 test14 (struct argp *argp)
428 {
429 INIT_TEST1 (1, "--limerick");
430 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
431 fail ("argp_parse failed");
432 else if (test_args.group_2_1_option != 'l')
433 fail ("option not processed");
434 }
435
436 void
test15(struct argp * argp)437 test15 (struct argp *argp)
438 {
439 INIT_TEST2 (1, "-r", "FILE");
440 test_file (argp, argc, argv, &test_args);
441 if (!test_args.read)
442 fail ("short alias not recognized properly");
443 }
444
445
446 typedef void (*test_fp) (struct argp *argp);
447
448 test_fp test_fun[] = {
449 test1, test2, test3, test4,
450 test5, test6, test7, test8,
451 test9, test10, test11, test12,
452 test13, test14, test15,
453 NULL
454 };
455
456 int
main(int argc,char ** argv)457 main (int argc, char **argv)
458 {
459 struct argp_child argp_children[3], group1_children[2], group2_children[2];
460 test_fp *fun;
461
462 set_program_name (argv[0]);
463
464 group1_children[0] = group1_1_child;
465 group1_children[1].argp = NULL;
466 group1_argp.children = group1_children;
467
468 group2_children[0] = group2_1_child;
469 group2_children[1].argp = NULL;
470 group2_argp.children = group2_children;
471
472 argp_children[0] = group1_child;
473 argp_children[1] = group2_child;
474 argp_children[2].argp = NULL;
475 test_argp.children = argp_children;
476
477 if (argc > 0)
478 {
479 struct test_args test_args;
480 init_args (test_args);
481 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
482 }
483
484 for (fun = test_fun; *fun; fun++)
485 (*fun) (&test_argp);
486
487 if (failure_count)
488 return 1;
489
490 return 0;
491 }
492