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