1 /* Test suite for argp.
2 Copyright (C) 2006-2007 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 2, or (at your option)
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 along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
47 static struct argp_option group1_option[] =
49 { NULL, 0, NULL, 0, "Option Group 1", 0},
50 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
51 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
52 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
53 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
54 { NULL, 0, NULL, 0, NULL, 0 }
58 group1_parser (int key, char *arg, struct argp_state *state)
60 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[] =
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[] =
134 { NULL, 0, NULL, 0, "Option Group 2", 0},
135 { "option", 'O', NULL, 0, "An option", 1 },
136 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
137 "Option with an optional argument. ARG is one of the following:", 2 },
138 { "one", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "one unit", 3 },
139 { "two", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "two units", 3 },
140 { "many", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "many units", 3 },
141 { NULL, 0, NULL, 0, NULL, 0 }
145 group2_parser (int key, char *arg, struct argp_state *state)
147 struct test_args *args = state->input;
156 args->optional_set = 1;
157 args->optional = arg;
161 return ARGP_ERR_UNKNOWN;
166 struct argp group2_argp = {
171 struct argp_child group2_child = {
179 static struct argp_option group2_1_option[] =
181 { NULL, 0, NULL, 0, "Option Group 2.1", 0},
182 { "poem", 'p', NULL, 0, "create a poem" },
183 { "limerick", 'l', NULL, 0, "create a limerick" },
184 { NULL, 0, NULL, 0, NULL, 0 }
188 group2_1_parser (int key, char *arg, struct argp_state *state)
190 struct test_args *args = state->input;
195 args->group_2_1_option = key;
198 return ARGP_ERR_UNKNOWN;
203 struct argp group2_1_argp = {
208 struct argp_child group2_1_child = {
216 static struct argp_option main_options[] =
218 { NULL, 0, NULL, 0, "Main options", 0},
219 { "test", 't', NULL, 0, NULL, 1 },
220 { NULL, 0, NULL, 0, NULL, 0 }
224 parse_opt (int key, char *arg, struct argp_state *state)
226 struct test_args *args = state->input;
232 for (i = 0; state->root_argp->children[i].argp; i++)
233 state->child_inputs[i] = args;
241 return ARGP_ERR_UNKNOWN;
246 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
247 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
248 static char doc[] = "documentation string";
250 struct argp test_argp = {
260 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
261 #define ARGV0 "test-argp"
262 #define init_args(a) memset (&(a), 0, sizeof (a));
264 #define INIT_TEST_COMMON(n) \
265 int argc = NARGS (argv); \
266 struct test_args test_args; \
267 init_args(test_args); \
270 #define INIT_TEST1(n, arg1) \
271 char *argv[] = { ARGV0, arg1, NULL }; \
274 #define INIT_TEST2(n, arg1, arg2) \
275 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
278 #define INIT_TEST3(n, arg1, arg2, arg3) \
279 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
283 unsigned failure_count = 0;
286 fail(const char *msg)
288 fprintf(stderr, "Test %d: %s\n", test_number, msg);
293 test1(struct argp *argp)
295 INIT_TEST1 (1, "--test");
296 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
297 fail("argp_parse failed");
298 else if (test_args.test != 1)
299 fail("option not processed");
303 test2(struct argp *argp)
305 INIT_TEST1 (2, "-t");
306 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
307 fail("argp_parse failed");
308 else if (test_args.test != 1)
309 fail("option not processed");
313 test_file(struct argp *argp, int argc, char **argv, struct test_args *args)
315 if (argp_parse (argp, argc, argv, 0, NULL, args))
316 fail("argp_parse failed");
317 else if (!args->file)
318 fail("option not processed");
319 else if (strcmp (args->file, "FILE"))
320 fail("option processed incorrectly");
324 test3(struct argp *argp)
326 INIT_TEST1 (3, "--file=FILE");
327 test_file (argp, argc, argv, &test_args);
331 test4(struct argp *argp)
333 INIT_TEST2 (4, "--file", "FILE");
334 test_file (argp, argc, argv, &test_args);
338 test5(struct argp *argp)
340 INIT_TEST1 (5, "--input=FILE");
341 test_file (argp, argc, argv, &test_args);
345 test6(struct argp *argp)
347 INIT_TEST2 (6, "--input", "FILE");
348 test_file (argp, argc, argv, &test_args);
352 test_optional(struct argp *argp, int argc, char **argv,
353 struct test_args *args, char *val, char *a)
356 if (argp_parse (argp, argc, argv, 0, &index, args))
357 fail("argp_parse failed");
358 else if (!args->optional_set)
359 fail("option not processed");
364 fail("option processed incorrectly");
366 else if (strcmp (args->optional, val))
367 fail("option processed incorrectly");
372 fail("expected command line argument not found");
373 else if (strcmp(argv[index], a))
374 fail("expected command line argument does not match");
379 test7(struct argp *argp)
381 INIT_TEST1 (7, "-oARG");
382 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
386 test8(struct argp *argp)
388 INIT_TEST2 (8, "-o", "ARG");
389 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
393 test9(struct argp *argp)
395 INIT_TEST1 (9, "--optional=ARG");
396 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
400 test10(struct argp *argp)
402 INIT_TEST2 (10, "--optional", "ARG");
403 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
407 test11(struct argp *argp)
409 INIT_TEST1 (11, "--optiona=ARG");
410 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
414 test12(struct argp *argp)
416 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
417 test_optional(argp, argc, argv, &test_args, "OPT", "FILE");
421 test13(struct argp *argp)
423 INIT_TEST1 (1, "--cantiga");
424 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
425 fail("argp_parse failed");
426 else if (test_args.group_1_1_option != 'C')
427 fail("option not processed");
431 test14(struct argp *argp)
433 INIT_TEST1 (1, "--limerick");
434 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
435 fail("argp_parse failed");
436 else if (test_args.group_2_1_option != 'l')
437 fail("option not processed");
442 typedef void (*test_fp) (struct argp *argp);
444 test_fp test_fun[] = {
445 test1, test2, test3, test4,
446 test5, test6, test7, test8,
447 test9, test10, test11, test12,
453 main (int argc, char **argv)
455 struct argp_child argp_children[3], group1_children[2], group2_children[2];
458 set_program_name (argv[0]);
460 group1_children[0] = group1_1_child;
461 group1_children[1].argp = NULL;
462 group1_argp.children = group1_children;
464 group2_children[0] = group2_1_child;
465 group2_children[1].argp = NULL;
466 group2_argp.children = group2_children;
468 argp_children[0] = group1_child;
469 argp_children[1] = group2_child;
470 argp_children[2].argp = NULL;
471 test_argp.children = argp_children;
475 struct test_args test_args;
476 init_args(test_args);
477 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
480 for (fun = test_fun; *fun; fun++)