1 /* Test suite for argp.
2 Copyright (C) 2006-2007, 2009 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/>. */
45 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 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
52 { NULL, 0, NULL, 0, NULL, 0 }
56 group1_parser (int key, char *arg, struct argp_state *state)
58 struct test_args *args = state->input;
75 return ARGP_ERR_UNKNOWN;
80 struct argp group1_argp = {
85 struct argp_child group1_child = {
93 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[] =
132 { NULL, 0, NULL, 0, "Option Group 2", 0},
133 { "option", 'O', NULL, 0, "An option", 1 },
134 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
135 "Option with an optional argument. ARG is one of the following:", 2 },
136 { "one", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "one unit", 3 },
137 { "two", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "two units", 3 },
138 { "many", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "many units", 3 },
139 { NULL, 0, NULL, 0, NULL, 0 }
143 group2_parser (int key, char *arg, struct argp_state *state)
145 struct test_args *args = state->input;
154 args->optional_set = 1;
155 args->optional = arg;
159 return ARGP_ERR_UNKNOWN;
164 struct argp group2_argp = {
169 struct argp_child group2_child = {
177 static struct argp_option group2_1_option[] =
179 { NULL, 0, NULL, 0, "Option Group 2.1", 0},
180 { "poem", 'p', NULL, 0, "create a poem" },
181 { "limerick", 'l', NULL, 0, "create a limerick" },
182 { NULL, 0, NULL, 0, NULL, 0 }
186 group2_1_parser (int key, char *arg, struct argp_state *state)
188 struct test_args *args = state->input;
193 args->group_2_1_option = key;
196 return ARGP_ERR_UNKNOWN;
201 struct argp group2_1_argp = {
206 struct argp_child group2_1_child = {
214 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");
440 typedef void (*test_fp) (struct argp *argp);
442 test_fp test_fun[] = {
443 test1, test2, test3, test4,
444 test5, test6, test7, test8,
445 test9, test10, test11, test12,
451 main (int argc, char **argv)
453 struct argp_child argp_children[3], group1_children[2], group2_children[2];
456 set_program_name (argv[0]);
458 group1_children[0] = group1_1_child;
459 group1_children[1].argp = NULL;
460 group1_argp.children = group1_children;
462 group2_children[0] = group2_1_child;
463 group2_children[1].argp = NULL;
464 group2_argp.children = group2_children;
466 argp_children[0] = group1_child;
467 argp_children[1] = group2_child;
468 argp_children[2].argp = NULL;
469 test_argp.children = argp_children;
473 struct test_args test_args;
474 init_args(test_args);
475 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
478 for (fun = test_fun; *fun; fun++)