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. */
30
const char *argp_program_version = "argp-test 1.0";
32
struct argp_option sub_options[] =
34
{"subopt1", 's', 0, 0, "Nested option 1"},
35
{"subopt2", 'S', 0, 0, "Nested option 2"},
37
{ 0, 0, 0, 0, "Some more nested options:", 10},
38
{"subopt3", 'p', 0, 0, "Nested option 3"},
40
{"subopt4", 'q', 0, 0, "Nested option 4", 1},
45
static const char sub_args_doc[] = "STRING...\n-";
46
static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
49
sub_parse_opt (int key, char *arg, struct argp_state *state)
53
case ARGP_KEY_NO_ARGS:
54
printf ("NO SUB ARGS\n");
57
printf ("SUB ARG: %s\n", arg);
60
case 's' : case 'S': case 'p': case 'q':
61
printf ("SUB KEY %c\n", key);
65
return ARGP_ERR_UNKNOWN;
71
sub_help_filter (int key, const char *text, void *input)
73
if (key == ARGP_KEY_HELP_EXTRA)
74
return strdup ("This is some extra text from the sub parser (note that it \
75
is preceded by a blank line).");
80
static struct argp sub_argp = {
81
sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
84
/* Structure used to communicate with the parsing functions. */
87
unsigned foonly; /* Value parsed for foonly. */
88
unsigned foonly_default; /* Default value for it. */
94
struct argp_option options[] =
96
{"pid", 'p', "PID", 0, "List the process PID"},
97
{"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
98
{"no-parent", 'P', 0, 0, "Include processes without parents"},
99
{0, 'x', 0, OPTION_ALIAS},
100
{"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
101
" if there's some reason ps can't"
102
" print a field for any process, it's"
103
" removed from the output entirely)" },
104
{"reverse", 'r', 0, 0, "Reverse the order of any sort"},
105
{"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
106
{"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
107
"Add the processes from the session"
108
" SID (which defaults to the sid of"
109
" the current process)" },
111
{0,0,0,0, "Here are some more options:"},
112
{"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
113
{"zaza", 'z', 0, 0, "Snit a zar"},
118
static const char args_doc[] = "STRING";
119
static const char doc[] = "Test program for argp."
120
"\vThis doc string comes after the options."
121
"\nHey! Some manual formatting!"
122
"\nThe current time is: %s";
125
popt (int key, char *arg)
129
sprintf (buf, "%c", key);
131
sprintf (buf, "%d", key);
133
printf ("KEY %s: %s\n", buf, arg);
135
printf ("KEY %s\n", buf);
139
parse_opt (int key, char *arg, struct argp_state *state)
141
struct params *params = state->input;
145
case ARGP_KEY_NO_ARGS:
146
printf ("NO ARGS\n");
150
if (state->arg_num > 0)
151
return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser. */
152
printf ("ARG: %s\n", arg);
157
params->foonly = atoi (arg);
159
params->foonly = params->foonly_default;
163
case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
164
case 'r': case OPT_SESS: case 'z':
169
return ARGP_ERR_UNKNOWN;
175
help_filter (int key, const char *text, void *input)
178
struct params *params = input;
180
if (key == ARGP_KEY_HELP_POST_DOC && text)
182
time_t now = time (0);
183
asprintf (&new_text, text, ctime (&now));
186
/* Show the default for the --foonly option. */
187
asprintf (&new_text, "%s (ZOT defaults to %x)",
188
text, params->foonly_default);
190
new_text = (char *)text;
195
static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
196
static struct argp argp = {
197
options, parse_opt, args_doc, doc, argp_children, help_filter
201
main (int argc, char **argv)
203
struct params params;
205
params.foonly_default = random ();
206
argp_parse (&argp, argc, argv, 0, 0, ¶ms);
207
printf ("After parsing: foonly = %x\n", params.foonly);