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