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. */
46 static struct argp_option group1_option[] =
48 { NULL, 0, NULL, 0, "Option Group 1", 0},
49 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
50 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
51 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
52 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
53 { NULL, 0, NULL, 0, NULL, 0 }
57 group1_parser (int key, char *arg, struct argp_state *state)
59 struct test_args *args = state->input;
76 return ARGP_ERR_UNKNOWN;
81 struct argp group1_argp = {
86 struct argp_child group1_child = {
94 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[] =
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[] =
180 { NULL, 0, NULL, 0, "Option Group 2.1", 0},
181 { "poem", 'p', NULL, 0, "create a poem" },
182 { "limerick", 'l', NULL, 0, "create a limerick" },
183 { NULL, 0, NULL, 0, NULL, 0 }
187 group2_1_parser (int key, char *arg, struct argp_state *state)
189 struct test_args *args = state->input;
194 args->group_2_1_option = key;
197 return ARGP_ERR_UNKNOWN;
202 struct argp group2_1_argp = {
207 struct argp_child group2_1_child = {
215 static struct argp_option main_options[] =
217 { NULL, 0, NULL, 0, "Main options", 0},
218 { "test", 't', NULL, 0, NULL, 1 },
219 { NULL, 0, NULL, 0, NULL, 0 }
223 parse_opt (int key, char *arg, struct argp_state *state)
225 struct test_args *args = state->input;
231 for (i = 0; state->root_argp->children[i].argp; i++)
232 state->child_inputs[i] = args;
240 return ARGP_ERR_UNKNOWN;
245 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
246 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
247 static char doc[] = "documentation string";
249 struct argp test_argp = {
259 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
260 #define ARGV0 "test-argp"
261 #define init_args(a) memset (&(a), 0, sizeof (a));
263 #define INIT_TEST_COMMON(n) \
264 int argc = NARGS (argv); \
265 struct test_args test_args; \
266 init_args(test_args); \
269 #define INIT_TEST1(n, arg1) \
270 char *argv[] = { ARGV0, arg1, NULL }; \
273 #define INIT_TEST2(n, arg1, arg2) \
274 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
277 #define INIT_TEST3(n, arg1, arg2, arg3) \
278 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
282 unsigned failure_count = 0;
285 fail(const char *msg)
287 fprintf(stderr, "Test %d: %s\n", test_number, msg);
292 test1(struct argp *argp)
294 INIT_TEST1 (1, "--test");
295 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
296 fail("argp_parse failed");
297 else if (test_args.test != 1)
298 fail("option not processed");
302 test2(struct argp *argp)
304 INIT_TEST1 (2, "-t");
305 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
306 fail("argp_parse failed");
307 else if (test_args.test != 1)
308 fail("option not processed");
312 test_file(struct argp *argp, int argc, char **argv, struct test_args *args)
314 if (argp_parse (argp, argc, argv, 0, NULL, args))
315 fail("argp_parse failed");
316 else if (!args->file)
317 fail("option not processed");
318 else if (strcmp (args->file, "FILE"))
319 fail("option processed incorrectly");
323 test3(struct argp *argp)
325 INIT_TEST1 (3, "--file=FILE");
326 test_file (argp, argc, argv, &test_args);
330 test4(struct argp *argp)
332 INIT_TEST2 (4, "--file", "FILE");
333 test_file (argp, argc, argv, &test_args);
337 test5(struct argp *argp)
339 INIT_TEST1 (5, "--input=FILE");
340 test_file (argp, argc, argv, &test_args);
344 test6(struct argp *argp)
346 INIT_TEST2 (6, "--input", "FILE");
347 test_file (argp, argc, argv, &test_args);
351 test_optional(struct argp *argp, int argc, char **argv,
352 struct test_args *args, char *val, char *a)
355 if (argp_parse (argp, argc, argv, 0, &index, args))
356 fail("argp_parse failed");
357 else if (!args->optional_set)
358 fail("option not processed");
363 fail("option processed incorrectly");
365 else if (strcmp (args->optional, val))
366 fail("option processed incorrectly");
371 fail("expected command line argument not found");
372 else if (strcmp(argv[index], a))
373 fail("expected command line argument does not match");
378 test7(struct argp *argp)
380 INIT_TEST1 (7, "-oARG");
381 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
385 test8(struct argp *argp)
387 INIT_TEST2 (8, "-o", "ARG");
388 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
392 test9(struct argp *argp)
394 INIT_TEST1 (9, "--optional=ARG");
395 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
399 test10(struct argp *argp)
401 INIT_TEST2 (10, "--optional", "ARG");
402 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
406 test11(struct argp *argp)
408 INIT_TEST1 (11, "--optiona=ARG");
409 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
413 test12(struct argp *argp)
415 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
416 test_optional(argp, argc, argv, &test_args, "OPT", "FILE");
420 test13(struct argp *argp)
422 INIT_TEST1 (1, "--cantiga");
423 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
424 fail("argp_parse failed");
425 else if (test_args.group_1_1_option != 'C')
426 fail("option not processed");
430 test14(struct argp *argp)
432 INIT_TEST1 (1, "--limerick");
433 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
434 fail("argp_parse failed");
435 else if (test_args.group_2_1_option != 'l')
436 fail("option not processed");
441 typedef void (*test_fp) (struct argp *argp);
443 test_fp test_fun[] = {
444 test1, test2, test3, test4,
445 test5, test6, test7, test8,
446 test9, test10, test11, test12,
452 main (int argc, char **argv)
454 struct argp_child argp_children[3], group1_children[2], group2_children[2];
457 set_program_name (argv[0]);
459 group1_children[0] = group1_1_child;
460 group1_children[1].argp = NULL;
461 group1_argp.children = group1_children;
463 group2_children[0] = group2_1_child;
464 group2_children[1].argp = NULL;
465 group2_argp.children = group2_children;
467 argp_children[0] = group1_child;
468 argp_children[1] = group2_child;
469 argp_children[2].argp = NULL;
470 test_argp.children = argp_children;
474 struct test_args test_args;
475 init_args(test_args);
476 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
479 for (fun = test_fun; *fun; fun++)