1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009-2010 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/>. */
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 { "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;
74 return ARGP_ERR_UNKNOWN;
79 struct argp group1_argp = {
84 struct argp_child group1_child = {
92 static struct argp_option group1_1_option[] = {
93 { NULL, 0, NULL, 0, "Option Group 1.1", 0 },
94 { "cantiga", 'C', NULL, 0, "create a cantiga" },
95 { "sonet", 'S', NULL, 0, "create a sonet" },
96 { NULL, 0, NULL, 0, NULL, 0 }
100 group1_1_parser (int key, char *arg, struct argp_state *state)
102 struct test_args *args = state->input;
107 args->group_1_1_option = key;
110 return ARGP_ERR_UNKNOWN;
115 struct argp group1_1_argp = {
120 struct argp_child group1_1_child = {
128 static struct argp_option group2_option[] = {
129 { NULL, 0, NULL, 0, "Option Group 2", 0 },
130 { "option", 'O', NULL, 0, "An option", 1 },
131 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
132 "Option with an optional argument. ARG is one of the following:", 2 },
133 { "one", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "one unit", 3 },
134 { "two", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "two units", 3 },
135 { "many", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "many units", 3 },
136 { NULL, 0, NULL, 0, NULL, 0 }
140 group2_parser (int key, char *arg, struct argp_state *state)
142 struct test_args *args = state->input;
151 args->optional_set = 1;
152 args->optional = arg;
156 return ARGP_ERR_UNKNOWN;
161 struct argp group2_argp = {
166 struct argp_child group2_child = {
174 static struct argp_option group2_1_option[] = {
175 { NULL, 0, NULL, 0, "Option Group 2.1", 0 },
176 { "poem", 'p', NULL, 0, "create a poem" },
177 { "limerick", 'l', NULL, 0, "create a limerick" },
178 { NULL, 0, NULL, 0, NULL, 0 }
182 group2_1_parser (int key, char *arg, struct argp_state *state)
184 struct test_args *args = state->input;
189 args->group_2_1_option = key;
192 return ARGP_ERR_UNKNOWN;
197 struct argp group2_1_argp = {
202 struct argp_child group2_1_child = {
210 static struct argp_option main_options[] = {
211 { NULL, 0, NULL, 0, "Main options", 0 },
212 { "test", 't', NULL, 0, NULL, 1 },
213 { NULL, 0, NULL, 0, NULL, 0 }
217 parse_opt (int key, char *arg, struct argp_state *state)
219 struct test_args *args = state->input;
225 for (i = 0; state->root_argp->children[i].argp; i++)
226 state->child_inputs[i] = args;
234 return ARGP_ERR_UNKNOWN;
239 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
240 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
241 static char doc[] = "documentation string";
243 struct argp test_argp = {
253 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
254 #define ARGV0 "test-argp"
255 #define init_args(a) memset (&(a), 0, sizeof (a));
257 #define INIT_TEST_COMMON(n) \
258 int argc = NARGS (argv); \
259 struct test_args test_args; \
260 init_args(test_args); \
263 #define INIT_TEST1(n, arg1) \
264 char *argv[] = { ARGV0, arg1, NULL }; \
267 #define INIT_TEST2(n, arg1, arg2) \
268 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
271 #define INIT_TEST3(n, arg1, arg2, arg3) \
272 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
276 unsigned failure_count = 0;
279 fail (const char *msg)
281 fprintf (stderr, "Test %d: %s\n", test_number, msg);
286 test1 (struct argp *argp)
288 INIT_TEST1 (1, "--test");
289 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
290 fail ("argp_parse failed");
291 else if (test_args.test != 1)
292 fail ("option not processed");
296 test2 (struct argp *argp)
298 INIT_TEST1 (2, "-t");
299 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
300 fail ("argp_parse failed");
301 else if (test_args.test != 1)
302 fail ("option not processed");
306 test_file (struct argp *argp, int argc, char **argv, struct test_args *args)
308 if (argp_parse (argp, argc, argv, 0, NULL, args))
309 fail ("argp_parse failed");
310 else if (!args->file)
311 fail ("option not processed");
312 else if (strcmp (args->file, "FILE"))
313 fail ("option processed incorrectly");
317 test3 (struct argp *argp)
319 INIT_TEST1 (3, "--file=FILE");
320 test_file (argp, argc, argv, &test_args);
324 test4 (struct argp *argp)
326 INIT_TEST2 (4, "--file", "FILE");
327 test_file (argp, argc, argv, &test_args);
331 test5 (struct argp *argp)
333 INIT_TEST1 (5, "--input=FILE");
334 test_file (argp, argc, argv, &test_args);
338 test6 (struct argp *argp)
340 INIT_TEST2 (6, "--input", "FILE");
341 test_file (argp, argc, argv, &test_args);
345 test_optional (struct argp *argp, int argc, char **argv,
346 struct test_args *args, char *val, char *a)
349 if (argp_parse (argp, argc, argv, 0, &index, args))
350 fail ("argp_parse failed");
351 else if (!args->optional_set)
352 fail ("option not processed");
357 fail ("option processed incorrectly");
359 else if (strcmp (args->optional, val))
360 fail ("option processed incorrectly");
365 fail ("expected command line argument not found");
366 else if (strcmp (argv[index], a))
367 fail ("expected command line argument does not match");
372 test7 (struct argp *argp)
374 INIT_TEST1 (7, "-oARG");
375 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
379 test8 (struct argp *argp)
381 INIT_TEST2 (8, "-o", "ARG");
382 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
386 test9 (struct argp *argp)
388 INIT_TEST1 (9, "--optional=ARG");
389 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
393 test10 (struct argp *argp)
395 INIT_TEST2 (10, "--optional", "ARG");
396 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
400 test11 (struct argp *argp)
402 INIT_TEST1 (11, "--optiona=ARG");
403 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
407 test12 (struct argp *argp)
409 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
410 test_optional (argp, argc, argv, &test_args, "OPT", "FILE");
414 test13 (struct argp *argp)
416 INIT_TEST1 (1, "--cantiga");
417 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
418 fail ("argp_parse failed");
419 else if (test_args.group_1_1_option != 'C')
420 fail ("option not processed");
424 test14 (struct argp *argp)
426 INIT_TEST1 (1, "--limerick");
427 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
428 fail ("argp_parse failed");
429 else if (test_args.group_2_1_option != 'l')
430 fail ("option not processed");
435 typedef void (*test_fp) (struct argp *argp);
437 test_fp test_fun[] = {
438 test1, test2, test3, test4,
439 test5, test6, test7, test8,
440 test9, test10, test11, test12,
446 main (int argc, char **argv)
448 struct argp_child argp_children[3], group1_children[2], group2_children[2];
451 set_program_name (argv[0]);
453 group1_children[0] = group1_1_child;
454 group1_children[1].argp = NULL;
455 group1_argp.children = group1_children;
457 group2_children[0] = group2_1_child;
458 group2_children[1].argp = NULL;
459 group2_argp.children = group2_children;
461 argp_children[0] = group1_child;
462 argp_children[1] = group2_child;
463 argp_children[2].argp = NULL;
464 test_argp.children = argp_children;
468 struct test_args test_args;
469 init_args (test_args);
470 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
473 for (fun = test_fun; *fun; fun++)