1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009-2020 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 <https://www.gnu.org/licenses/>. */
45 static struct argp_option group1_option
[] = {
46 { NULL
, 0, NULL
, 0, "Option Group 1", 0 },
47 { "verbose", 'v', NULL
, 0, "Simple option without arguments", 1 },
48 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
49 { "input", 0, NULL
, OPTION_ALIAS
, NULL
, 1 },
50 { "read", 'r', NULL
, OPTION_ALIAS
, NULL
, 1 },
51 { "hidden", 'H', "FILE", OPTION_HIDDEN
, "Hidden option", 1 },
52 { NULL
, 0, NULL
, 0, NULL
, 0 }
56 group1_parser (int key
, char *arg
, struct argp_state
*state
)
58 struct test_args
*args
= state
->input
;
78 return ARGP_ERR_UNKNOWN
;
83 struct argp group1_argp
= {
88 struct argp_child group1_child
= {
96 static struct argp_option group1_1_option
[] = {
97 { NULL
, 0, NULL
, 0, "Option Group 1.1", 0 },
98 { "cantiga", 'C', NULL
, 0, "create a cantiga" },
99 { "sonet", 'S', NULL
, 0, "create a sonet" },
100 { NULL
, 0, NULL
, 0, NULL
, 0 }
104 group1_1_parser (int key
, char *arg
, struct argp_state
*state
)
106 struct test_args
*args
= state
->input
;
111 args
->group_1_1_option
= key
;
114 return ARGP_ERR_UNKNOWN
;
119 struct argp group1_1_argp
= {
124 struct argp_child group1_1_child
= {
132 static struct argp_option group2_option
[] = {
133 { NULL
, 0, NULL
, 0, "Option Group 2", 0 },
134 { "option", 'O', NULL
, 0, "An option", 1 },
135 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL
,
136 "Option with an optional argument. ARG is one of the following:", 2 },
137 { "one", 0, NULL
, OPTION_DOC
| OPTION_NO_TRANS
, "one unit", 3 },
138 { "two", 0, NULL
, OPTION_DOC
| OPTION_NO_TRANS
, "two units", 3 },
139 { "many", 0, NULL
, OPTION_DOC
| OPTION_NO_TRANS
, "many units", 3 },
140 { NULL
, 0, NULL
, 0, NULL
, 0 }
144 group2_parser (int key
, char *arg
, struct argp_state
*state
)
146 struct test_args
*args
= state
->input
;
155 args
->optional_set
= 1;
156 args
->optional
= arg
;
160 return ARGP_ERR_UNKNOWN
;
165 struct argp group2_argp
= {
170 struct argp_child group2_child
= {
178 static struct argp_option group2_1_option
[] = {
179 { NULL
, 0, NULL
, 0, "Option Group 2.1", 0 },
180 { "poem", 'p', NULL
, 0, "create a poem" },
181 { "limerick", 'l', NULL
, 0, "create a limerick" },
182 { NULL
, 0, NULL
, 0, NULL
, 0 }
186 group2_1_parser (int key
, char *arg
, struct argp_state
*state
)
188 struct test_args
*args
= state
->input
;
193 args
->group_2_1_option
= key
;
196 return ARGP_ERR_UNKNOWN
;
201 struct argp group2_1_argp
= {
206 struct argp_child group2_1_child
= {
214 static struct argp_option main_options
[] = {
215 { NULL
, 0, NULL
, 0, "Main options", 0 },
216 { "test", 't', NULL
, 0, NULL
, 1 },
217 { NULL
, 0, NULL
, 0, NULL
, 0 }
221 parse_opt (int key
, char *arg
, struct argp_state
*state
)
223 struct test_args
*args
= state
->input
;
229 for (i
= 0; state
->root_argp
->children
[i
].argp
; i
++)
230 state
->child_inputs
[i
] = args
;
238 return ARGP_ERR_UNKNOWN
;
243 const char *argp_program_version
= "test_argp (" PACKAGE_NAME
") " VERSION
;
244 const char *argp_program_bug_address
= "<" PACKAGE_BUGREPORT
">";
245 static char doc
[] = "documentation string";
247 struct argp test_argp
= {
257 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
258 #define ARGV0 "test-argp"
259 #define init_args(a) memset (&(a), 0, sizeof (a));
261 #define INIT_TEST_COMMON(n) \
262 int argc = NARGS (argv); \
263 struct test_args test_args; \
264 init_args (test_args); \
267 #define INIT_TEST1(n, arg1) \
268 char *argv[] = { ARGV0, arg1, NULL }; \
271 #define INIT_TEST2(n, arg1, arg2) \
272 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
275 #define INIT_TEST3(n, arg1, arg2, arg3) \
276 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
280 unsigned failure_count
= 0;
283 fail (const char *msg
)
285 fprintf (stderr
, "Test %d: %s\n", test_number
, msg
);
290 test1 (struct argp
*argp
)
292 INIT_TEST1 (1, "--test");
293 if (argp_parse (argp
, argc
, argv
, 0, NULL
, &test_args
))
294 fail ("argp_parse failed");
295 else if (test_args
.test
!= 1)
296 fail ("option not processed");
300 test2 (struct argp
*argp
)
302 INIT_TEST1 (2, "-t");
303 if (argp_parse (argp
, argc
, argv
, 0, NULL
, &test_args
))
304 fail ("argp_parse failed");
305 else if (test_args
.test
!= 1)
306 fail ("option not processed");
310 test_file (struct argp
*argp
, int argc
, char **argv
, struct test_args
*args
)
312 if (argp_parse (argp
, argc
, argv
, 0, NULL
, args
))
313 fail ("argp_parse failed");
314 else if (!args
->file
)
315 fail ("option not processed");
316 else if (strcmp (args
->file
, "FILE"))
317 fail ("option processed incorrectly");
321 test3 (struct argp
*argp
)
323 INIT_TEST1 (3, "--file=FILE");
324 test_file (argp
, argc
, argv
, &test_args
);
328 test4 (struct argp
*argp
)
330 INIT_TEST2 (4, "--file", "FILE");
331 test_file (argp
, argc
, argv
, &test_args
);
335 test5 (struct argp
*argp
)
337 INIT_TEST1 (5, "--input=FILE");
338 test_file (argp
, argc
, argv
, &test_args
);
342 test6 (struct argp
*argp
)
344 INIT_TEST2 (6, "--input", "FILE");
345 test_file (argp
, argc
, argv
, &test_args
);
349 test_optional (struct argp
*argp
, int argc
, char **argv
,
350 struct test_args
*args
, const char *val
, const char *a
)
353 if (argp_parse (argp
, argc
, argv
, 0, &index
, args
))
354 fail ("argp_parse failed");
355 else if (!args
->optional_set
)
356 fail ("option not processed");
361 fail ("option processed incorrectly");
363 else if (strcmp (args
->optional
, val
))
364 fail ("option processed incorrectly");
369 fail ("expected command line argument not found");
370 else if (strcmp (argv
[index
], a
))
371 fail ("expected command line argument does not match");
376 test7 (struct argp
*argp
)
378 INIT_TEST1 (7, "-oARG");
379 test_optional (argp
, argc
, argv
, &test_args
, "ARG", NULL
);
383 test8 (struct argp
*argp
)
385 INIT_TEST2 (8, "-o", "ARG");
386 test_optional (argp
, argc
, argv
, &test_args
, NULL
, "ARG");
390 test9 (struct argp
*argp
)
392 INIT_TEST1 (9, "--optional=ARG");
393 test_optional (argp
, argc
, argv
, &test_args
, "ARG", NULL
);
397 test10 (struct argp
*argp
)
399 INIT_TEST2 (10, "--optional", "ARG");
400 test_optional (argp
, argc
, argv
, &test_args
, NULL
, "ARG");
404 test11 (struct argp
*argp
)
406 INIT_TEST1 (11, "--optiona=ARG");
407 test_optional (argp
, argc
, argv
, &test_args
, "ARG", NULL
);
411 test12 (struct argp
*argp
)
413 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
414 test_optional (argp
, argc
, argv
, &test_args
, "OPT", "FILE");
418 test13 (struct argp
*argp
)
420 INIT_TEST1 (1, "--cantiga");
421 if (argp_parse (argp
, argc
, argv
, 0, NULL
, &test_args
))
422 fail ("argp_parse failed");
423 else if (test_args
.group_1_1_option
!= 'C')
424 fail ("option not processed");
428 test14 (struct argp
*argp
)
430 INIT_TEST1 (1, "--limerick");
431 if (argp_parse (argp
, argc
, argv
, 0, NULL
, &test_args
))
432 fail ("argp_parse failed");
433 else if (test_args
.group_2_1_option
!= 'l')
434 fail ("option not processed");
438 test15 (struct argp
*argp
)
440 INIT_TEST2 (1, "-r", "FILE");
441 test_file (argp
, argc
, argv
, &test_args
);
443 fail ("short alias not recognized properly");
447 typedef void (*test_fp
) (struct argp
*argp
);
449 static test_fp test_fun
[] = {
450 test1
, test2
, test3
, test4
,
451 test5
, test6
, test7
, test8
,
452 test9
, test10
, test11
, test12
,
453 test13
, test14
, test15
,
458 main (int argc
, char **argv
)
460 struct argp_child argp_children
[3], group1_children
[2], group2_children
[2];
463 group1_children
[0] = group1_1_child
;
464 group1_children
[1].argp
= NULL
;
465 group1_argp
.children
= group1_children
;
467 group2_children
[0] = group2_1_child
;
468 group2_children
[1].argp
= NULL
;
469 group2_argp
.children
= group2_children
;
471 argp_children
[0] = group1_child
;
472 argp_children
[1] = group2_child
;
473 argp_children
[2].argp
= NULL
;
474 test_argp
.children
= argp_children
;
478 struct test_args test_args
;
479 init_args (test_args
);
480 return argp_parse (&test_argp
, argc
, argv
, 0, NULL
, &test_args
);
483 for (fun
= test_fun
; *fun
; fun
++)