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/>. */
46 static struct argp_option group1_option[] = {
47 { NULL, 0, NULL, 0, "Option Group 1", 0 },
48 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
49 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
50 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
51 { "read", 'r', 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;
79 return ARGP_ERR_UNKNOWN;
84 struct argp group1_argp = {
89 struct argp_child group1_child = {
97 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[] = {
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[] = {
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[] = {
216 { NULL, 0, NULL, 0, "Main options", 0 },
217 { "test", 't', NULL, 0, NULL, 1 },
218 { NULL, 0, NULL, 0, NULL, 0 }
222 parse_opt (int key, char *arg, struct argp_state *state)
224 struct test_args *args = state->input;
230 for (i = 0; state->root_argp->children[i].argp; i++)
231 state->child_inputs[i] = args;
239 return ARGP_ERR_UNKNOWN;
244 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
245 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
246 static char doc[] = "documentation string";
248 struct argp test_argp = {
258 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
259 #define ARGV0 "test-argp"
260 #define init_args(a) memset (&(a), 0, sizeof (a));
262 #define INIT_TEST_COMMON(n) \
263 int argc = NARGS (argv); \
264 struct test_args test_args; \
265 init_args (test_args); \
268 #define INIT_TEST1(n, arg1) \
269 char *argv[] = { ARGV0, arg1, NULL }; \
272 #define INIT_TEST2(n, arg1, arg2) \
273 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
276 #define INIT_TEST3(n, arg1, arg2, arg3) \
277 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
281 unsigned failure_count = 0;
284 fail (const char *msg)
286 fprintf (stderr, "Test %d: %s\n", test_number, msg);
291 test1 (struct argp *argp)
293 INIT_TEST1 (1, "--test");
294 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
295 fail ("argp_parse failed");
296 else if (test_args.test != 1)
297 fail ("option not processed");
301 test2 (struct argp *argp)
303 INIT_TEST1 (2, "-t");
304 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
305 fail ("argp_parse failed");
306 else if (test_args.test != 1)
307 fail ("option not processed");
311 test_file (struct argp *argp, int argc, char **argv, struct test_args *args)
313 if (argp_parse (argp, argc, argv, 0, NULL, args))
314 fail ("argp_parse failed");
315 else if (!args->file)
316 fail ("option not processed");
317 else if (strcmp (args->file, "FILE"))
318 fail ("option processed incorrectly");
322 test3 (struct argp *argp)
324 INIT_TEST1 (3, "--file=FILE");
325 test_file (argp, argc, argv, &test_args);
329 test4 (struct argp *argp)
331 INIT_TEST2 (4, "--file", "FILE");
332 test_file (argp, argc, argv, &test_args);
336 test5 (struct argp *argp)
338 INIT_TEST1 (5, "--input=FILE");
339 test_file (argp, argc, argv, &test_args);
343 test6 (struct argp *argp)
345 INIT_TEST2 (6, "--input", "FILE");
346 test_file (argp, argc, argv, &test_args);
350 test_optional (struct argp *argp, int argc, char **argv,
351 struct test_args *args, char *val, char *a)
354 if (argp_parse (argp, argc, argv, 0, &index, args))
355 fail ("argp_parse failed");
356 else if (!args->optional_set)
357 fail ("option not processed");
362 fail ("option processed incorrectly");
364 else if (strcmp (args->optional, val))
365 fail ("option processed incorrectly");
370 fail ("expected command line argument not found");
371 else if (strcmp (argv[index], a))
372 fail ("expected command line argument does not match");
377 test7 (struct argp *argp)
379 INIT_TEST1 (7, "-oARG");
380 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
384 test8 (struct argp *argp)
386 INIT_TEST2 (8, "-o", "ARG");
387 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
391 test9 (struct argp *argp)
393 INIT_TEST1 (9, "--optional=ARG");
394 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
398 test10 (struct argp *argp)
400 INIT_TEST2 (10, "--optional", "ARG");
401 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
405 test11 (struct argp *argp)
407 INIT_TEST1 (11, "--optiona=ARG");
408 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
412 test12 (struct argp *argp)
414 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
415 test_optional (argp, argc, argv, &test_args, "OPT", "FILE");
419 test13 (struct argp *argp)
421 INIT_TEST1 (1, "--cantiga");
422 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
423 fail ("argp_parse failed");
424 else if (test_args.group_1_1_option != 'C')
425 fail ("option not processed");
429 test14 (struct argp *argp)
431 INIT_TEST1 (1, "--limerick");
432 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
433 fail ("argp_parse failed");
434 else if (test_args.group_2_1_option != 'l')
435 fail ("option not processed");
439 test15 (struct argp *argp)
441 INIT_TEST2 (1, "-r", "FILE");
442 test_file (argp, argc, argv, &test_args);
444 fail ("short alias not recognized properly");
448 typedef void (*test_fp) (struct argp *argp);
450 test_fp test_fun[] = {
451 test1, test2, test3, test4,
452 test5, test6, test7, test8,
453 test9, test10, test11, test12,
454 test13, test14, test15,
459 main (int argc, char **argv)
461 struct argp_child argp_children[3], group1_children[2], group2_children[2];
464 set_program_name (argv[0]);
466 group1_children[0] = group1_1_child;
467 group1_children[1].argp = NULL;
468 group1_argp.children = group1_children;
470 group2_children[0] = group2_1_child;
471 group2_children[1].argp = NULL;
472 group2_argp.children = group2_children;
474 argp_children[0] = group1_child;
475 argp_children[1] = group2_child;
476 argp_children[2].argp = NULL;
477 test_argp.children = argp_children;
481 struct test_args test_args;
482 init_args (test_args);
483 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
486 for (fun = test_fun; *fun; fun++)