1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009-2012 Free Software Foundation, Inc.
3 This file is part of the GNUlib Library.
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.
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.
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/>. */
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 }
55 group1_parser (int key
, char *arg
, struct argp_state
*state
)
57 struct test_args
*args
= state
->input
;
77 return ARGP_ERR_UNKNOWN
;
82 struct argp group1_argp
= {
87 struct argp_child group1_child
= {
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 }
103 group1_1_parser (int key
, char *arg
, struct argp_state
*state
)
105 struct test_args
*args
= state
->input
;
110 args
->group_1_1_option
= key
;
113 return ARGP_ERR_UNKNOWN
;
118 struct argp group1_1_argp
= {
123 struct argp_child group1_1_child
= {
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 }
143 group2_parser (int key
, char *arg
, struct argp_state
*state
)
145 struct test_args
*args
= state
->input
;
154 args
->optional_set
= 1;
155 args
->optional
= arg
;
159 return ARGP_ERR_UNKNOWN
;
164 struct argp group2_argp
= {
169 struct argp_child group2_child
= {
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 }
185 group2_1_parser (int key
, char *arg
, struct argp_state
*state
)
187 struct test_args
*args
= state
->input
;
192 args
->group_2_1_option
= key
;
195 return ARGP_ERR_UNKNOWN
;
200 struct argp group2_1_argp
= {
205 struct argp_child group2_1_child
= {
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 }
220 parse_opt (int key
, char *arg
, struct argp_state
*state
)
222 struct test_args
*args
= state
->input
;
228 for (i
= 0; state
->root_argp
->children
[i
].argp
; i
++)
229 state
->child_inputs
[i
] = args
;
237 return ARGP_ERR_UNKNOWN
;
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";
246 struct argp test_argp
= {
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));
260 #define INIT_TEST_COMMON(n) \
261 int argc = NARGS (argv); \
262 struct test_args test_args; \
263 init_args (test_args); \
266 #define INIT_TEST1(n, arg1) \
267 char *argv[] = { ARGV0, arg1, NULL }; \
270 #define INIT_TEST2(n, arg1, arg2) \
271 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
274 #define INIT_TEST3(n, arg1, arg2, arg3) \
275 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
279 unsigned failure_count
= 0;
282 fail (const char *msg
)
284 fprintf (stderr
, "Test %d: %s\n", test_number
, msg
);
289 test1 (struct argp
*argp
)
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");
299 test2 (struct argp
*argp
)
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");
309 test_file (struct argp
*argp
, int argc
, char **argv
, struct test_args
*args
)
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");
320 test3 (struct argp
*argp
)
322 INIT_TEST1 (3, "--file=FILE");
323 test_file (argp
, argc
, argv
, &test_args
);
327 test4 (struct argp
*argp
)
329 INIT_TEST2 (4, "--file", "FILE");
330 test_file (argp
, argc
, argv
, &test_args
);
334 test5 (struct argp
*argp
)
336 INIT_TEST1 (5, "--input=FILE");
337 test_file (argp
, argc
, argv
, &test_args
);
341 test6 (struct argp
*argp
)
343 INIT_TEST2 (6, "--input", "FILE");
344 test_file (argp
, argc
, argv
, &test_args
);
348 test_optional (struct argp
*argp
, int argc
, char **argv
,
349 struct test_args
*args
, const char *val
, const char *a
)
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");
360 fail ("option processed incorrectly");
362 else if (strcmp (args
->optional
, val
))
363 fail ("option processed incorrectly");
368 fail ("expected command line argument not found");
369 else if (strcmp (argv
[index
], a
))
370 fail ("expected command line argument does not match");
375 test7 (struct argp
*argp
)
377 INIT_TEST1 (7, "-oARG");
378 test_optional (argp
, argc
, argv
, &test_args
, "ARG", NULL
);
382 test8 (struct argp
*argp
)
384 INIT_TEST2 (8, "-o", "ARG");
385 test_optional (argp
, argc
, argv
, &test_args
, NULL
, "ARG");
389 test9 (struct argp
*argp
)
391 INIT_TEST1 (9, "--optional=ARG");
392 test_optional (argp
, argc
, argv
, &test_args
, "ARG", NULL
);
396 test10 (struct argp
*argp
)
398 INIT_TEST2 (10, "--optional", "ARG");
399 test_optional (argp
, argc
, argv
, &test_args
, NULL
, "ARG");
403 test11 (struct argp
*argp
)
405 INIT_TEST1 (11, "--optiona=ARG");
406 test_optional (argp
, argc
, argv
, &test_args
, "ARG", NULL
);
410 test12 (struct argp
*argp
)
412 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
413 test_optional (argp
, argc
, argv
, &test_args
, "OPT", "FILE");
417 test13 (struct argp
*argp
)
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");
427 test14 (struct argp
*argp
)
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");
437 test15 (struct argp
*argp
)
439 INIT_TEST2 (1, "-r", "FILE");
440 test_file (argp
, argc
, argv
, &test_args
);
442 fail ("short alias not recognized properly");
446 typedef void (*test_fp
) (struct argp
*argp
);
448 test_fp test_fun
[] = {
449 test1
, test2
, test3
, test4
,
450 test5
, test6
, test7
, test8
,
451 test9
, test10
, test11
, test12
,
452 test13
, test14
, test15
,
457 main (int argc
, char **argv
)
459 struct argp_child argp_children
[3], group1_children
[2], group2_children
[2];
462 set_program_name (argv
[0]);
464 group1_children
[0] = group1_1_child
;
465 group1_children
[1].argp
= NULL
;
466 group1_argp
.children
= group1_children
;
468 group2_children
[0] = group2_1_child
;
469 group2_children
[1].argp
= NULL
;
470 group2_argp
.children
= group2_children
;
472 argp_children
[0] = group1_child
;
473 argp_children
[1] = group2_child
;
474 argp_children
[2].argp
= NULL
;
475 test_argp
.children
= argp_children
;
479 struct test_args test_args
;
480 init_args (test_args
);
481 return argp_parse (&test_argp
, argc
, argv
, 0, NULL
, &test_args
);
484 for (fun
= test_fun
; *fun
; fun
++)