Initial release, version 0.0.0.
[gsasl.git] / argp / argp-test.c
blob8165902094d2c554edda2aaaabdf488c5be07986
1 /* Test program for argp argument parser
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE 1
23 #endif
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <stdlib.h>
30 #include <time.h>
31 #include <string.h>
32 #include <stdio.h>
34 #include "argp.h"
36 #ifndef UNUSED
37 # if __GNUC__ >= 2
38 # define UNUSED __attribute__ ((__unused__))
39 # else
40 # define UNUSED
41 # endif
42 #endif
44 #if !HAVE_ASPRINTF
45 #include <stdarg.h>
47 static int
48 asprintf (char **result, const char *format, ...)
50 size_t size;
51 char *p;
53 for (size = 200, p = NULL;; size *= 2)
55 va_list args;
56 int written;
58 p = realloc(p, size + 1);
59 if (!p)
61 fprintf(stderr, "Virtual memory exhausted.\n");
62 abort();
65 p[size] = '\0';
67 va_start(args, format);
68 written = vsnprintf(p, size, format, args);
69 va_end(args);
71 if (written >= 0)
73 *result = p;
74 return written;
78 #endif /* !HAVE_ASPRINTF */
80 const char *argp_program_version = "argp-test 1.0";
82 struct argp_option sub_options[] =
84 {"subopt1", 's', 0, 0, "Nested option 1"},
85 {"subopt2", 'S', 0, 0, "Nested option 2"},
87 { 0, 0, 0, 0, "Some more nested options:", 10},
88 {"subopt3", 'p', 0, 0, "Nested option 3"},
90 {"subopt4", 'q', 0, 0, "Nested option 4", 1},
92 {0}
95 static const char sub_args_doc[] = "STRING...\n-";
96 static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
98 static error_t
99 sub_parse_opt (int key, char *arg, struct argp_state *state UNUSED)
101 switch (key)
103 case ARGP_KEY_NO_ARGS:
104 printf ("NO SUB ARGS\n");
105 break;
106 case ARGP_KEY_ARG:
107 printf ("SUB ARG: %s\n", arg);
108 break;
110 case 's' : case 'S': case 'p': case 'q':
111 printf ("SUB KEY %c\n", key);
112 break;
114 default:
115 return ARGP_ERR_UNKNOWN;
117 return 0;
120 static char *
121 sub_help_filter (int key, const char *text, void *input UNUSED)
123 if (key == ARGP_KEY_HELP_EXTRA)
124 return strdup ("This is some extra text from the sub parser (note that it \
125 is preceded by a blank line).");
126 else
127 return (char *)text;
130 static struct argp sub_argp = {
131 sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
134 /* Structure used to communicate with the parsing functions. */
135 struct params
137 unsigned foonly; /* Value parsed for foonly. */
138 unsigned foonly_default; /* Default value for it. */
141 #define OPT_PGRP 1
142 #define OPT_SESS 2
144 struct argp_option options[] =
146 {"pid", 'p', "PID", 0, "List the process PID"},
147 {"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
148 {"no-parent", 'P', 0, 0, "Include processes without parents"},
149 {0, 'x', 0, OPTION_ALIAS},
150 {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
151 " if there's some reason ps can't"
152 " print a field for any process, it's"
153 " removed from the output entirely)" },
154 {"reverse", 'r', 0, 0, "Reverse the order of any sort"},
155 {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
156 {"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
157 "Add the processes from the session"
158 " SID (which defaults to the sid of"
159 " the current process)" },
161 {0,0,0,0, "Here are some more options:"},
162 {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
163 {"zaza", 'z', 0, 0, "Snit a zar"},
168 static const char args_doc[] = "STRING";
169 static const char doc[] = "Test program for argp."
170 "\vThis doc string comes after the options."
171 "\nHey! Some manual formatting!"
172 "\nThe current time is: %s";
174 static void
175 popt (int key, char *arg)
177 char buf[10];
178 if (isprint (key))
179 sprintf (buf, "%c", key);
180 else
181 sprintf (buf, "%d", key);
182 if (arg)
183 printf ("KEY %s: %s\n", buf, arg);
184 else
185 printf ("KEY %s\n", buf);
188 static error_t
189 parse_opt (int key, char *arg, struct argp_state *state)
191 struct params *params = state->input;
193 switch (key)
195 case ARGP_KEY_NO_ARGS:
196 printf ("NO ARGS\n");
197 break;
199 case ARGP_KEY_ARG:
200 if (state->arg_num > 0)
201 return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser. */
202 printf ("ARG: %s\n", arg);
203 break;
205 case 'f':
206 if (arg)
207 params->foonly = atoi (arg);
208 else
209 params->foonly = params->foonly_default;
210 popt (key, arg);
211 break;
213 case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
214 case 'r': case OPT_SESS: case 'z':
215 popt (key, arg);
216 break;
218 default:
219 return ARGP_ERR_UNKNOWN;
221 return 0;
224 static char *
225 help_filter (int key, const char *text, void *input)
227 char *new_text;
228 struct params *params = input;
230 if (key == ARGP_KEY_HELP_POST_DOC && text)
232 time_t now = time (0);
233 asprintf (&new_text, text, ctime (&now));
235 else if (key == 'f')
236 /* Show the default for the --foonly option. */
237 asprintf (&new_text, "%s (ZOT defaults to %x)",
238 text, params->foonly_default);
239 else
240 new_text = (char *)text;
242 return new_text;
245 static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
246 static struct argp argp = {
247 options, parse_opt, args_doc, doc, argp_children, help_filter
251 main (int argc, char **argv)
253 struct params params;
254 params.foonly = 0;
255 params.foonly_default = random ();
256 argp_parse (&argp, argc, argv, 0, 0, &params);
257 printf ("After parsing: foonly = %x\n", params.foonly);
258 return 0;