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