Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-argp.c
1 /* Test suite for argp.
2    Copyright (C) 2006-2007 Free Software Foundation, Inc.
3    This file is part of the GNUlib Library.
4
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.
9
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.
14
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/>.  */
17
18 #include <config.h>
19
20 #include "argp.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #if HAVE_STRING_H
25 # include <string.h>
26 #endif
27 #if HAVE_STRINGS_H
28 # include <strings.h>
29 #endif
30 #include "progname.h"
31
32 struct test_args
33 {
34   int test;
35   int verbose;
36   char *file;
37   char *hidden;
38   int opt;
39   char *optional;
40   int optional_set;
41   int group_2_1_option;
42   int group_1_1_option;
43 };
44
45 static struct argp_option group1_option[] =
46 {
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 }
53 };
54
55 static error_t
56 group1_parser (int key, char *arg, struct argp_state *state)
57 {
58   struct test_args *args = state->input;
59
60   switch (key)
61     {
62     case 'v':
63       args->verbose++;
64       break;
65
66     case 'f':
67       args->file = arg;
68       break;
69
70     case 'H':
71       args->hidden = arg;
72       break;
73
74     default:
75       return ARGP_ERR_UNKNOWN;
76     }
77   return 0;
78 }
79
80 struct argp group1_argp = {
81   group1_option,
82   group1_parser
83 };
84
85 struct argp_child group1_child = {
86   &group1_argp,
87   0,
88   "",
89   1
90 };
91
92 \f
93 static struct argp_option group1_1_option[] =
94 {
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 }
99 };
100
101 static error_t
102 group1_1_parser (int key, char *arg, struct argp_state *state)
103 {
104   struct test_args *args = state->input;
105   switch (key)
106     {
107     case 'C':
108     case 'S':
109       args->group_1_1_option = key;
110       break;
111     default:
112       return ARGP_ERR_UNKNOWN;
113     }
114   return 0;
115 }
116
117 struct argp group1_1_argp = {
118   group1_1_option,
119   group1_1_parser
120 };
121
122 struct argp_child group1_1_child = {
123   &group1_1_argp,
124   0,
125   "",
126   2
127 };
128
129 \f
130 static struct argp_option group2_option[] =
131 {
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 }
140 };
141
142 static error_t
143 group2_parser (int key, char *arg, struct argp_state *state)
144 {
145   struct test_args *args = state->input;
146
147   switch (key)
148     {
149     case 'O':
150       args->opt = 1;
151       break;
152
153     case 'o':
154       args->optional_set = 1;
155       args->optional = arg;
156       break;
157
158     default:
159       return ARGP_ERR_UNKNOWN;
160     }
161   return 0;
162 }
163
164 struct argp group2_argp = {
165   group2_option,
166   group2_parser
167 };
168
169 struct argp_child group2_child = {
170   &group2_argp,
171   0,
172   "",
173   2
174 };
175
176 \f
177 static struct argp_option group2_1_option[] =
178 {
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 }
183 };
184
185 static error_t
186 group2_1_parser (int key, char *arg, struct argp_state *state)
187 {
188   struct test_args *args = state->input;
189   switch (key)
190     {
191     case 'p':
192     case 'e':
193       args->group_2_1_option = key;
194       break;
195     default:
196       return ARGP_ERR_UNKNOWN;
197     }
198   return 0;
199 }
200
201 struct argp group2_1_argp = {
202   group2_1_option,
203   group2_1_parser
204 };
205
206 struct argp_child group2_1_child = {
207   &group2_1_argp,
208   0,
209   "",
210   2
211 };
212
213 \f
214 static struct argp_option main_options[] =
215   {
216     { NULL, 0, NULL, 0, "Main options", 0},
217     { "test", 't', NULL, 0, NULL, 1 },
218     { NULL, 0, NULL, 0, NULL, 0 }
219   };
220
221 static error_t
222 parse_opt (int key, char *arg, struct argp_state *state)
223 {
224   struct test_args *args = state->input;
225   int i;
226
227   switch (key)
228     {
229     case ARGP_KEY_INIT:
230       for (i = 0; state->root_argp->children[i].argp; i++)
231         state->child_inputs[i] = args;
232       break;
233
234     case 't':
235       args->test = 1;
236       break;
237
238     default:
239       return ARGP_ERR_UNKNOWN;
240     }
241   return 0;
242 }
243
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";
247
248 struct argp test_argp = {
249   main_options,
250   parse_opt,
251   "ARGS...",
252   doc,
253   NULL,
254   NULL,
255   NULL
256 };
257
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));
261
262 #define INIT_TEST_COMMON(n)     \
263  int argc = NARGS (argv);       \
264  struct test_args test_args;    \
265  init_args(test_args);          \
266  test_number = n;
267
268 #define INIT_TEST1(n, arg1)            \
269  char *argv[] = { ARGV0, arg1, NULL }; \
270  INIT_TEST_COMMON(n)
271
272 #define INIT_TEST2(n, arg1, arg2)            \
273  char *argv[] = { ARGV0, arg1, arg2, NULL }; \
274  INIT_TEST_COMMON(n)
275
276 #define INIT_TEST3(n, arg1, arg2, arg3)            \
277  char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
278  INIT_TEST_COMMON(n)
279
280 int test_number;
281 unsigned failure_count = 0;
282
283 void
284 fail(const char *msg)
285 {
286   fprintf(stderr, "Test %d: %s\n", test_number, msg);
287   failure_count++;
288 }
289
290 void
291 test1(struct argp *argp)
292 {
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");
298 }
299
300 void
301 test2(struct argp *argp)
302 {
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");
308 }
309
310 void
311 test_file(struct argp *argp, int argc, char **argv, struct test_args *args)
312 {
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");
319 }
320
321 void
322 test3(struct argp *argp)
323 {
324   INIT_TEST1 (3, "--file=FILE");
325   test_file (argp, argc, argv, &test_args);
326 }
327
328 void
329 test4(struct argp *argp)
330 {
331   INIT_TEST2 (4, "--file", "FILE");
332   test_file (argp, argc, argv, &test_args);
333 }
334
335 void
336 test5(struct argp *argp)
337 {
338   INIT_TEST1 (5, "--input=FILE");
339   test_file (argp, argc, argv, &test_args);
340 }
341
342 void
343 test6(struct argp *argp)
344 {
345   INIT_TEST2 (6, "--input", "FILE");
346   test_file (argp, argc, argv, &test_args);
347 }
348
349 void
350 test_optional(struct argp *argp, int argc, char **argv,
351               struct test_args *args, char *val, char *a)
352 {
353   int index;
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");
358
359   if (!val)
360     {
361       if (args->optional)
362         fail("option processed incorrectly");
363     }
364   else if (strcmp (args->optional, val))
365     fail("option processed incorrectly");
366
367   if (a)
368     {
369       if (index == argc)
370         fail("expected command line argument not found");
371       else if (strcmp(argv[index], a))
372         fail("expected command line argument does not match");
373     }
374 }
375
376 void
377 test7(struct argp *argp)
378 {
379   INIT_TEST1 (7, "-oARG");
380   test_optional(argp, argc, argv, &test_args, "ARG", NULL);
381 }
382
383 void
384 test8(struct argp *argp)
385 {
386   INIT_TEST2 (8, "-o", "ARG");
387   test_optional(argp, argc, argv, &test_args, NULL, "ARG");
388 }
389
390 void
391 test9(struct argp *argp)
392 {
393   INIT_TEST1 (9, "--optional=ARG");
394   test_optional(argp, argc, argv, &test_args, "ARG", NULL);
395 }
396
397 void
398 test10(struct argp *argp)
399 {
400   INIT_TEST2 (10, "--optional", "ARG");
401   test_optional(argp, argc, argv, &test_args, NULL, "ARG");
402 }
403
404 void
405 test11(struct argp *argp)
406 {
407   INIT_TEST1 (11, "--optiona=ARG");
408   test_optional(argp, argc, argv, &test_args, "ARG", NULL);
409 }
410
411 void
412 test12(struct argp *argp)
413 {
414   INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
415   test_optional(argp, argc, argv, &test_args, "OPT", "FILE");
416 }
417
418 void
419 test13(struct argp *argp)
420 {
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");
426 }
427
428 void
429 test14(struct argp *argp)
430 {
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");
436 }
437
438
439
440 typedef void (*test_fp) (struct argp *argp);
441
442 test_fp test_fun[] = {
443   test1,  test2,  test3,  test4,
444   test5,  test6,  test7,  test8,
445   test9,  test10, test11, test12,
446   test13, test14,
447   NULL
448 };
449
450 int
451 main (int argc, char **argv)
452 {
453   struct argp_child argp_children[3], group1_children[2], group2_children[2];
454   test_fp *fun;
455
456   set_program_name (argv[0]);
457
458   group1_children[0] = group1_1_child;
459   group1_children[1].argp = NULL;
460   group1_argp.children = group1_children;
461
462   group2_children[0] = group2_1_child;
463   group2_children[1].argp = NULL;
464   group2_argp.children = group2_children;
465
466   argp_children[0] = group1_child;
467   argp_children[1] = group2_child;
468   argp_children[2].argp = NULL;
469   test_argp.children = argp_children;
470
471   if (argc > 0)
472     {
473       struct test_args test_args;
474       init_args(test_args);
475       return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
476     }
477
478   for (fun = test_fun; *fun; fun++)
479     (*fun) (&test_argp);
480
481   if (failure_count)
482     return 1;
483
484   return 0;
485 }