25c927c900beb0345ed88ae523e75576a24b06e9
[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 #include "progname.h"
35
36 struct test_args
37 {
38   int test;
39   int verbose;
40   char *file;
41   char *hidden;
42   int opt;
43   char *optional;
44   int optional_set;
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 group2_option[] = 
96 {
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 }
105 };
106
107 static error_t
108 group2_parser (int key, char *arg, struct argp_state *state)
109 {
110   struct test_args *args = state->input;
111
112   switch (key)
113     {
114     case 'O':
115       args->opt = 1;
116       break;
117       
118     case 'o':
119       args->optional_set = 1;
120       args->optional = arg;
121       break;
122       
123     default:
124       return ARGP_ERR_UNKNOWN;
125     }
126   return 0;
127 }
128
129 struct argp group2_argp = {
130   group2_option,
131   group2_parser
132 };
133
134 struct argp_child group2_child = {
135   &group2_argp,
136   0,
137   "",
138   2
139 };
140
141 static struct argp_option main_options[] =
142   {
143     { NULL, 0, NULL, 0, "Main options", 0},
144     { "test", 't', NULL, 0, NULL, 1 }, 
145     { NULL, 0, NULL, 0, NULL, 0 }
146   };
147
148 static error_t
149 parse_opt (int key, char *arg, struct argp_state *state)
150 {
151   struct test_args *args = state->input;
152   int i;
153   
154   switch (key)
155     {
156     case ARGP_KEY_INIT:
157       for (i = 0; state->root_argp->children[i].argp; i++)
158         state->child_inputs[i] = args;
159       break;
160
161     case 't':
162       args->test = 1;
163       break;
164       
165     default:
166       return ARGP_ERR_UNKNOWN;
167     }
168   return 0;
169 }
170
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";
174
175 struct argp test_argp = {
176   main_options,
177   parse_opt,
178   "ARGS...",
179   doc,
180   NULL,
181   NULL,
182   NULL
183 };
184
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));
188
189 #define INIT_TEST_COMMON(n)     \
190  int argc = NARGS (argv);       \
191  struct test_args test_args;    \
192  init_args(test_args);          \
193  test_number = n;
194
195 #define INIT_TEST1(n, arg1)            \
196  char *argv[] = { ARGV0, arg1, NULL }; \
197  INIT_TEST_COMMON(n)
198
199 #define INIT_TEST2(n, arg1, arg2)            \
200  char *argv[] = { ARGV0, arg1, arg2, NULL }; \
201  INIT_TEST_COMMON(n)
202
203 #define INIT_TEST3(n, arg1, arg2, arg3)            \
204  char *argv[] = { ARGV0, arg1, arg2, arg3, NULL }; \
205  INIT_TEST_COMMON(n)
206
207 int test_number;
208 unsigned failure_count = 0;
209
210 void
211 fail(const char *msg)
212 {
213   fprintf(stderr, "Test %d: %s\n", test_number, msg);
214   failure_count++;
215 }
216
217 void
218 test1(struct argp *argp)
219 {
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");
225 }
226
227 void
228 test2(struct argp *argp)
229 {
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");
235 }
236
237 void
238 test_file(struct argp *argp, int argc, char **argv, struct test_args *args)
239 {
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");
246 }
247
248 void
249 test3(struct argp *argp)
250 {
251   INIT_TEST1 (3, "--file=FILE");
252   test_file (argp, argc, argv, &test_args);
253 }
254
255 void
256 test4(struct argp *argp)
257 {
258   INIT_TEST2 (4, "--file", "FILE");
259   test_file (argp, argc, argv, &test_args);
260 }
261
262 void
263 test5(struct argp *argp)
264 {
265   INIT_TEST1 (5, "--input=FILE");
266   test_file (argp, argc, argv, &test_args);
267 }
268
269 void
270 test6(struct argp *argp)
271 {
272   INIT_TEST2 (6, "--input", "FILE");
273   test_file (argp, argc, argv, &test_args);
274 }
275
276 void
277 test_optional(struct argp *argp, int argc, char **argv,
278               struct test_args *args, char *val, char *a)
279 {
280   int index;
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");
285
286   if (!val)
287     {
288       if (args->optional)
289         fail("option processed incorrectly");
290     }
291   else if (strcmp (args->optional, val))
292     fail("option processed incorrectly");
293
294   if (a)
295     {
296       if (index == argc)
297         fail("expected command line argument not found");
298       else if (strcmp(argv[index], a))
299         fail("expected command line argument does not match");
300     }
301 }
302
303 void
304 test7(struct argp *argp)
305 {
306   INIT_TEST1 (7, "-oARG");
307   test_optional(argp, argc, argv, &test_args, "ARG", NULL);
308 }
309
310 void
311 test8(struct argp *argp)
312 {
313   INIT_TEST2 (8, "-o", "ARG");
314   test_optional(argp, argc, argv, &test_args, NULL, "ARG");
315 }
316
317 void
318 test9(struct argp *argp)
319 {
320   INIT_TEST1 (9, "--optional=ARG");
321   test_optional(argp, argc, argv, &test_args, "ARG", NULL);
322 }
323
324 void
325 test10(struct argp *argp)
326 {
327   INIT_TEST2 (10, "--optional", "ARG");
328   test_optional(argp, argc, argv, &test_args, NULL, "ARG");
329 }
330   
331 void
332 test11(struct argp *argp)
333 {
334   INIT_TEST1 (11, "--optiona=ARG");
335   test_optional(argp, argc, argv, &test_args, "ARG", NULL);
336 }
337
338 void
339 test12(struct argp *argp)
340 {
341   INIT_TEST3 (12, "--option", "--optional=OPT", "FILE");
342   test_optional(argp, argc, argv, &test_args, "OPT", "FILE");
343 }  
344
345
346 typedef void (*test_fp) (struct argp *argp);
347
348 test_fp test_fun[] = {
349   test1,  test2,  test3,  test4,
350   test5,  test6,  test7,  test8,
351   test9,  test10, test11, test12,
352   NULL
353 };
354
355 int
356 main (int argc, char **argv)
357 {
358   struct argp_child argp_children[3];
359   test_fp *fun;
360
361   set_program_name (argv[0]);
362
363   argp_children[0] = group1_child;
364   argp_children[1] = group2_child;
365   argp_children[2].argp = NULL;
366   test_argp.children = argp_children;
367
368   if (argc > 0)
369     {
370       struct test_args test_args;    
371       init_args(test_args);          
372       return argp_parse (&test_argp, argc, argv, 0, NULL, &test_args);
373     }
374   
375   for (fun = test_fun; *fun; fun++)
376     (*fun) (&test_argp);
377
378   if (failure_count)
379     return 1;
380
381   return 0;
382 }