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. */
48 static struct argp_option group1_option[] =
50 { NULL, 0, NULL, 0, "Option Group 1", 0},
51 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
52 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
53 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
54 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
55 { NULL, 0, NULL, 0, NULL, 0 }
59 group1_parser (int key, char *arg, struct argp_state *state)
61 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[] =
98 { NULL, 0, NULL, 0, "Option Group 1.1", 0},
99 { "cantiga", 'C', NULL, 0, "create a cantiga" },
100 { "sonet", 'S', NULL, 0, "create a sonet" },
101 { NULL, 0, NULL, 0, NULL, 0 }
105 group1_1_parser (int key, char *arg, struct argp_state *state)
107 struct test_args *args = state->input;
112 args->group_1_1_option = key;
115 return ARGP_ERR_UNKNOWN;
120 struct argp group1_1_argp = {
125 struct argp_child group1_1_child = {
133 static struct argp_option group2_option[] =
135 { NULL, 0, NULL, 0, "Option Group 2", 0},
136 { "option", 'O', NULL, 0, "An option", 1 },
137 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
138 "Option with an optional argument. ARG is one of the following:", 2 },
139 { "one", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "one unit", 3 },
140 { "two", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "two units", 3 },
141 { "many", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "many units", 3 },
142 { NULL, 0, NULL, 0, NULL, 0 }
146 group2_parser (int key, char *arg, struct argp_state *state)
148 struct test_args *args = state->input;
157 args->optional_set = 1;
158 args->optional = arg;
162 return ARGP_ERR_UNKNOWN;
167 struct argp group2_argp = {
172 struct argp_child group2_child = {
180 static struct argp_option group2_1_option[] =
182 { NULL, 0, NULL, 0, "Option Group 2.1", 0},
183 { "poem", 'p', NULL, 0, "create a poem" },
184 { "limerick", 'l', NULL, 0, "create a limerick" },
185 { NULL, 0, NULL, 0, NULL, 0 }
189 group2_1_parser (int key, char *arg, struct argp_state *state)
191 struct test_args *args = state->input;
196 args->group_2_1_option = key;
199 return ARGP_ERR_UNKNOWN;
204 struct argp group2_1_argp = {
209 struct argp_child group2_1_child = {
217 static struct argp_option main_options[] =
219 { NULL, 0, NULL, 0, "Main options", 0},
220 { "test", 't', NULL, 0, NULL, 1 },
221 { NULL, 0, NULL, 0, NULL, 0 }
225 parse_opt (int key, char *arg, struct argp_state *state)
227 struct test_args *args = state->input;
233 for (i = 0; state->root_argp->children[i].argp; i++)
234 state->child_inputs[i] = args;
242 return ARGP_ERR_UNKNOWN;
247 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
248 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
249 static char doc[] = "documentation string";
251 struct argp test_argp = {
261 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
262 #define ARGV0 "test-argp"
263 #define init_args(a) memset (&(a), 0, sizeof (a));
265 #define INIT_TEST_COMMON(n) \
266 int argc = NARGS (argv); \
267 struct test_args test_args; \
268 init_args(test_args); \
271 #define INIT_TEST1(n, arg1) \
272 char *argv[] = { ARGV0, arg1, NULL }; \
275 #define INIT_TEST2(n, arg1, arg2) \
276 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
279 #define INIT_TEST3(n, arg1, arg2, arg3) \
280 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
284 unsigned failure_count = 0;
287 fail(const char *msg)
289 fprintf(stderr, "Test %d: %s\n", test_number, msg);
294 test1(struct argp *argp)
296 INIT_TEST1 (1, "--test");
297 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
298 fail("argp_parse failed");
299 else if (test_args.test != 1)
300 fail("option not processed");
304 test2(struct argp *argp)
306 INIT_TEST1 (2, "-t");
307 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
308 fail("argp_parse failed");
309 else if (test_args.test != 1)
310 fail("option not processed");
314 test_file(struct argp *argp, int argc, char **argv, struct test_args *args)
316 if (argp_parse (argp, argc, argv, 0, NULL, args))
317 fail("argp_parse failed");
318 else if (!args->file)
319 fail("option not processed");
320 else if (strcmp (args->file, "FILE"))
321 fail("option processed incorrectly");
325 test3(struct argp *argp)
327 INIT_TEST1 (3, "--file=FILE");
328 test_file (argp, argc, argv, &test_args);
332 test4(struct argp *argp)
334 INIT_TEST2 (4, "--file", "FILE");
335 test_file (argp, argc, argv, &test_args);
339 test5(struct argp *argp)
341 INIT_TEST1 (5, "--input=FILE");
342 test_file (argp, argc, argv, &test_args);
346 test6(struct argp *argp)
348 INIT_TEST2 (6, "--input", "FILE");
349 test_file (argp, argc, argv, &test_args);
353 test_optional(struct argp *argp, int argc, char **argv,
354 struct test_args *args, char *val, char *a)
357 if (argp_parse (argp, argc, argv, 0, &index, args))
358 fail("argp_parse failed");
359 else if (!args->optional_set)
360 fail("option not processed");
365 fail("option processed incorrectly");
367 else if (strcmp (args->optional, val))
368 fail("option processed incorrectly");
373 fail("expected command line argument not found");
374 else if (strcmp(argv[index], a))
375 fail("expected command line argument does not match");
380 test7(struct argp *argp)
382 INIT_TEST1 (7, "-oARG");
383 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
387 test8(struct argp *argp)
389 INIT_TEST2 (8, "-o", "ARG");
390 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
394 test9(struct argp *argp)
396 INIT_TEST1 (9, "--optional=ARG");
397 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
401 test10(struct argp *argp)
403 INIT_TEST2 (10, "--optional", "ARG");
404 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
408 test11(struct argp *argp)
410 INIT_TEST1 (11, "--optiona=ARG");
411 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
415 test12(struct argp *argp)
417 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
418 test_optional(argp, argc, argv, &test_args, "OPT", "FILE");
422 test13(struct argp *argp)
424 INIT_TEST1 (1, "--cantiga");
425 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
426 fail("argp_parse failed");
427 else if (test_args.group_1_1_option != 'C')
428 fail("option not processed");
432 test14(struct argp *argp)
434 INIT_TEST1 (1, "--limerick");
435 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
436 fail("argp_parse failed");
437 else if (test_args.group_2_1_option != 'l')
438 fail("option not processed");
443 typedef void (*test_fp) (struct argp *argp);
445 test_fp test_fun[] = {
446 test1, test2, test3, test4,
447 test5, test6, test7, test8,
448 test9, test10, test11, test12,
454 main (int argc, char **argv)
456 struct argp_child argp_children[3], group1_children[2], group2_children[2];
459 set_program_name (argv[0]);
461 group1_children[0] = group1_1_child;
462 group1_children[1].argp = NULL;
463 group1_argp.children = group1_children;
465 group2_children[0] = group2_1_child;
466 group2_children[1].argp = NULL;
467 group2_argp.children = group2_children;
469 argp_children[0] = group1_child;
470 argp_children[1] = group2_child;
471 argp_children[2].argp = NULL;
472 test_argp.children = argp_children;
476 struct test_args test_args;
477 init_args(test_args);
478 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
481 for (fun = test_fun; *fun; fun++)