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. */
47 static struct argp_option group1_option[] =
49 { NULL, 0, NULL, 0, "Option Group 1", 0},
50 { "verbose", 'v', NULL, 0, "Simple option without arguments", 1 },
51 { "file", 'f', "FILE", 0, "Option with a mandatory argument", 1 },
52 { "input", 0, NULL, OPTION_ALIAS, NULL, 1 },
53 { "hidden", 'H', "FILE", OPTION_HIDDEN, "Hidden option", 1 },
54 { NULL, 0, NULL, 0, NULL, 0 }
58 group1_parser (int key, char *arg, struct argp_state *state)
60 struct test_args *args = state->input;
77 return ARGP_ERR_UNKNOWN;
82 struct argp group1_argp = {
87 struct argp_child group1_child = {
95 static struct argp_option group2_option[] =
97 { NULL, 0, NULL, 0, "Option Group 2", 0},
98 { "option", 'O', NULL, 0, "An option", 1 },
99 { "optional", 'o', "ARG", OPTION_ARG_OPTIONAL,
100 "Option with an optional argument. ARG is one of the following:", 1 },
101 { "one", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "one unit", 2 },
102 { "two", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "two units", 2 },
103 { "many", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, "many units", 2 },
104 { NULL, 0, NULL, 0, NULL, 0 }
108 group2_parser (int key, char *arg, struct argp_state *state)
110 struct test_args *args = state->input;
119 args->optional_set = 1;
120 args->optional = arg;
124 return ARGP_ERR_UNKNOWN;
129 struct argp group2_argp = {
134 struct argp_child group2_child = {
141 static struct argp_option main_options[] =
143 { NULL, 0, NULL, 0, "Main options", 0},
144 { "test", 't', NULL, 0, NULL, 1 },
145 { NULL, 0, NULL, 0, NULL, 0 }
149 parse_opt (int key, char *arg, struct argp_state *state)
151 struct test_args *args = state->input;
157 for (i = 0; state->root_argp->children[i].argp; i++)
158 state->child_inputs[i] = args;
166 return ARGP_ERR_UNKNOWN;
171 const char *argp_program_version = "test_argp (" PACKAGE_NAME ") " VERSION;
172 const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
173 static char doc[] = "documentation string";
175 struct argp test_argp = {
185 #define NARGS(a) (sizeof(a) / sizeof((a)[0]) - 1)
186 #define ARGV0 "test-argp"
187 #define init_args(a) memset (&(a), 0, sizeof (a));
189 #define INIT_TEST_COMMON(n) \
190 int argc = NARGS (argv); \
191 struct test_args test_args; \
192 init_args(test_args); \
195 #define INIT_TEST1(n, arg1) \
196 char *argv[] = { ARGV0, arg1, NULL }; \
199 #define INIT_TEST2(n, arg1, arg2) \
200 char *argv[] = { ARGV0, arg1, arg2, NULL }; \
203 #define INIT_TEST3(n, arg1, arg2, arg3) \
204 char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
208 unsigned failure_count = 0;
211 fail(const char *msg)
213 fprintf(stderr, "Test %d: %s\n", test_number, msg);
218 test1(struct argp *argp)
220 INIT_TEST1 (1, "--test");
221 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
222 fail("argp_parse failed");
223 else if (test_args.test != 1)
224 fail("option not processed");
228 test2(struct argp *argp)
230 INIT_TEST1 (2, "-t");
231 if (argp_parse (argp, argc, argv, 0, NULL, &test_args))
232 fail("argp_parse failed");
233 else if (test_args.test != 1)
234 fail("option not processed");
238 test_file(struct argp *argp, int argc, char **argv, struct test_args *args)
240 if (argp_parse (argp, argc, argv, 0, NULL, args))
241 fail("argp_parse failed");
242 else if (!args->file)
243 fail("option not processed");
244 else if (strcmp (args->file, "FILE"))
245 fail("option processed incorrectly");
249 test3(struct argp *argp)
251 INIT_TEST1 (3, "--file=FILE");
252 test_file (argp, argc, argv, &test_args);
256 test4(struct argp *argp)
258 INIT_TEST2 (4, "--file", "FILE");
259 test_file (argp, argc, argv, &test_args);
263 test5(struct argp *argp)
265 INIT_TEST1 (5, "--input=FILE");
266 test_file (argp, argc, argv, &test_args);
270 test6(struct argp *argp)
272 INIT_TEST2 (6, "--input", "FILE");
273 test_file (argp, argc, argv, &test_args);
277 test_optional(struct argp *argp, int argc, char **argv,
278 struct test_args *args, char *val, char *a)
281 if (argp_parse (argp, argc, argv, 0, &index, args))
282 fail("argp_parse failed");
283 else if (!args->optional_set)
284 fail("option not processed");
289 fail("option processed incorrectly");
291 else if (strcmp (args->optional, val))
292 fail("option processed incorrectly");
297 fail("expected command line argument not found");
298 else if (strcmp(argv[index], a))
299 fail("expected command line argument does not match");
304 test7(struct argp *argp)
306 INIT_TEST1 (7, "-oARG");
307 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
311 test8(struct argp *argp)
313 INIT_TEST2 (8, "-o", "ARG");
314 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
318 test9(struct argp *argp)
320 INIT_TEST1 (9, "--optional=ARG");
321 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
325 test10(struct argp *argp)
327 INIT_TEST2 (10, "--optional", "ARG");
328 test_optional(argp, argc, argv, &test_args, NULL, "ARG");
332 test11(struct argp *argp)
334 INIT_TEST1 (11, "--optiona=ARG");
335 test_optional(argp, argc, argv, &test_args, "ARG", NULL);
339 test12(struct argp *argp)
341 INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
342 test_optional(argp, argc, argv, &test_args, "OPT", "FILE");
346 typedef void (*test_fp) (struct argp *argp);
348 test_fp test_fun[] = {
349 test1, test2, test3, test4,
350 test5, test6, test7, test8,
351 test9, test10, test11, test12,
356 main (int argc, char **argv)
358 struct argp_child argp_children[3];
361 set_program_name (argv[0]);
363 argp_children[0] = group1_child;
364 argp_children[1] = group2_child;
365 argp_children[2].argp = NULL;
366 test_argp.children = argp_children;
370 struct test_args test_args;
371 init_args(test_args);
372 return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
375 for (fun = test_fun; *fun; fun++)