1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009-2016 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/>. */
43 static struct argp_option group1_option[] = {
44 { NULL, 0, NULL, 0, "Option Group 1", 0 },
45 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
46 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
47 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
48 { "read", 'r', NULL, OPTION_ALIAS, NULL, 1 },
49 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
50 { NULL, 0, NULL, 0, NULL, 0 }
54 group1_parser (int key, char *arg, struct argp_state *state)
56 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[] = {
95 { NULL, 0, NULL, 0, "Option Group 1.1", 0 },
96 { "cantiga", 'C', NULL, 0, "create a cantiga" },
97 { "sonet", 'S', NULL, 0, "create a sonet" },
98 { NULL, 0, NULL, 0, NULL, 0 }
102 group1_1_parser (int key, char *arg, struct argp_state *state)
104 struct test_args *args = state->input;
109 args->group_1_1_option = key;
112 return ARGP_ERR_UNKNOWN;
117 struct argp group1_1_argp = {
122 struct argp_child group1_1_child = {
130 static struct argp_option group2_option[] = {
131 { NULL, 0, NULL, 0, "Option Group 2", 0 },
132 { "option", 'O', NULL, 0, "An option", 1 },
133 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
134 "Option with an optional argument. ARG is one of the following:", 2 },
135 { "one", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "one unit", 3 },
136 { "two", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "two units", 3 },
137 { "many", 0, NULL, OPTION_DOC | OPTION_NO_TRANS, "many units", 3 },
138 { NULL, 0, NULL, 0, NULL, 0 }
142 group2_parser (int key, char *arg, struct argp_state *state)
144 struct test_args *args = state->input;
153 args->optional_set = 1;
154 args->optional = arg;
158 return ARGP_ERR_UNKNOWN;
163 struct argp group2_argp = {
168 struct argp_child group2_child = {
176 static struct argp_option group2_1_option[] = {
177 { NULL, 0, NULL, 0, "Option Group 2.1", 0 },
178 { "poem", 'p', NULL, 0, "create a poem" },
179 { "limerick", 'l', NULL, 0, "create a limerick" },
180 { NULL, 0, NULL, 0, NULL, 0 }
184 group2_1_parser (int key, char *arg, struct argp_state *state)
186 struct test_args *args = state->input;
191 args->group_2_1_option = key;
194 return ARGP_ERR_UNKNOWN;
199 struct argp group2_1_argp = {
204 struct argp_child group2_1_child = {
212 static struct argp_option main_options[] = {
213 { NULL, 0, NULL, 0, "Main options", 0 },
214 { "test", 't', NULL, 0, NULL, 1 },
215 { NULL, 0, NULL, 0, NULL, 0 }
219 parse_opt (int key, char *arg, struct argp_state *state)
221 struct test_args *args = state->input;
227 for (i = 0; state->root_argp->children[i].argp; i++)
228 state->child_inputs[i] = args;
236 return ARGP_ERR_UNKNOWN;
241 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
242 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
243 static char doc[] = "documentation string";
245 struct argp test_argp = {
255 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
256 #define ARGV0 "test-argp"
257 #define init_args(a) memset (&(a), 0, sizeof (a));
259 #define INIT_TEST_COMMON(n) \
260 int argc = NARGS (argv); \
261 struct test_args test_args; \
262 init_args (test_args); \
265 #define INIT_TEST1(n, arg1) \
266 char *argv[] = { ARGV0, arg1, NULL }; \
269 #define INIT_TEST2(n, arg1, arg2) \
270 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
273 #define INIT_TEST3(n, arg1, arg2, arg3) \
274 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
278 unsigned failure_count = 0;
281 fail (const char *msg)
283 fprintf (stderr, "Test %d: %s\n", test_number, msg);
288 test1 (struct argp *argp)
290 INIT_TEST1 (1, "--test");
291 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
292 fail ("argp_parse failed");
293 else if (test_args.test != 1)
294 fail ("option not processed");
298 test2 (struct argp *argp)
300 INIT_TEST1 (2, "-t");
301 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
302 fail ("argp_parse failed");
303 else if (test_args.test != 1)
304 fail ("option not processed");
308 test_file (struct argp *argp, int argc, char **argv, struct test_args *args)
310 if (argp_parse (argp, argc, argv, 0, NULL, args))
311 fail ("argp_parse failed");
312 else if (!args->file)
313 fail ("option not processed");
314 else if (strcmp (args->file, "FILE"))
315 fail ("option processed incorrectly");
319 test3 (struct argp *argp)
321 INIT_TEST1 (3, "--file=FILE");
322 test_file (argp, argc, argv, &test_args);
326 test4 (struct argp *argp)
328 INIT_TEST2 (4, "--file", "FILE");
329 test_file (argp, argc, argv, &test_args);
333 test5 (struct argp *argp)
335 INIT_TEST1 (5, "--input=FILE");
336 test_file (argp, argc, argv, &test_args);
340 test6 (struct argp *argp)
342 INIT_TEST2 (6, "--input", "FILE");
343 test_file (argp, argc, argv, &test_args);
347 test_optional (struct argp *argp, int argc, char **argv,
348 struct test_args *args, const char *val, const char *a)
351 if (argp_parse (argp, argc, argv, 0, &index, args))
352 fail ("argp_parse failed");
353 else if (!args->optional_set)
354 fail ("option not processed");
359 fail ("option processed incorrectly");
361 else if (strcmp (args->optional, val))
362 fail ("option processed incorrectly");
367 fail ("expected command line argument not found");
368 else if (strcmp (argv[index], a))
369 fail ("expected command line argument does not match");
374 test7 (struct argp *argp)
376 INIT_TEST1 (7, "-oARG");
377 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
381 test8 (struct argp *argp)
383 INIT_TEST2 (8, "-o", "ARG");
384 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
388 test9 (struct argp *argp)
390 INIT_TEST1 (9, "--optional=ARG");
391 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
395 test10 (struct argp *argp)
397 INIT_TEST2 (10, "--optional", "ARG");
398 test_optional (argp, argc, argv, &test_args, NULL, "ARG");
402 test11 (struct argp *argp)
404 INIT_TEST1 (11, "--optiona=ARG");
405 test_optional (argp, argc, argv, &test_args, "ARG", NULL);
409 test12 (struct argp *argp)
411 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
412 test_optional (argp, argc, argv, &test_args, "OPT", "FILE");
416 test13 (struct argp *argp)
418 INIT_TEST1 (1, "--cantiga");
419 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
420 fail ("argp_parse failed");
421 else if (test_args.group_1_1_option != 'C')
422 fail ("option not processed");
426 test14 (struct argp *argp)
428 INIT_TEST1 (1, "--limerick");
429 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
430 fail ("argp_parse failed");
431 else if (test_args.group_2_1_option != 'l')
432 fail ("option not processed");
436 test15 (struct argp *argp)
438 INIT_TEST2 (1, "-r", "FILE");
439 test_file (argp, argc, argv, &test_args);
441 fail ("short alias not recognized properly");
445 typedef void (*test_fp) (struct argp *argp);
447 test_fp test_fun[] = {
448 test1, test2, test3, test4,
449 test5, test6, test7, test8,
450 test9, test10, test11, test12,
451 test13, test14, test15,
456 main (int argc, char **argv)
458 struct argp_child argp_children[3], group1_children[2], group2_children[2];
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++)