source: trunk/src/libctests/glibc/argp/argp-test.c@ 2036

Last change on this file since 2036 was 2036, checked in by bird, 20 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
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>.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#include <stdlib.h>
26#include <time.h>
27#include <string.h>
28#include <argp.h>
29
30const char *argp_program_version = "argp-test 1.0";
31
32
33struct argp_option sub_options[] =
34{
35 {"subopt1", 's', 0, 0, "Nested option 1"},
36 {"subopt2", 'S', 0, 0, "Nested option 2"},
37
38 { 0, 0, 0, 0, "Some more nested options:", 10},
39 {"subopt3", 'p', 0, 0, "Nested option 3"},
40
41 {"subopt4", 'q', 0, 0, "Nested option 4", 1},
42
43 {0}
44};
45
46static const char sub_args_doc[] = "STRING...\n-";
47static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
48
49static error_t
50sub_parse_opt (int key, char *arg, struct argp_state *state)
51{
52 switch (key)
53 {
54 case ARGP_KEY_NO_ARGS:
55 printf ("NO SUB ARGS\n");
56 break;
57 case ARGP_KEY_ARG:
58 printf ("SUB ARG: %s\n", arg);
59 break;
60
61 case 's' : case 'S': case 'p': case 'q':
62 printf ("SUB KEY %c\n", key);
63 break;
64
65 default:
66 return ARGP_ERR_UNKNOWN;
67 }
68 return 0;
69}
70
71static char *
72sub_help_filter (int key, const char *text, void *input)
73{
74 if (key == ARGP_KEY_HELP_EXTRA)
75 return strdup ("This is some extra text from the sub parser (note that it \
76is preceded by a blank line).");
77 else
78 return (char *)text;
79}
80
81static struct argp sub_argp = {
82 sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter
83};
84
85
86/* Structure used to communicate with the parsing functions. */
87struct params
88{
89 unsigned foonly; /* Value parsed for foonly. */
90 unsigned foonly_default; /* Default value for it. */
91};
92
93#define OPT_PGRP 1
94#define OPT_SESS 2
95
96struct argp_option options[] =
97{
98 {"pid", 'p', "PID", 0, "List the process PID"},
99 {"pgrp", OPT_PGRP,"PGRP",0, "List processes in the process group PGRP"},
100 {"no-parent", 'P', 0, 0, "Include processes without parents"},
101 {0, 'x', 0, OPTION_ALIAS},
102 {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally"
103 " if there's some reason ps can't"
104 " print a field for any process, it's"
105 " removed from the output entirely)" },
106 {"reverse", 'r', 0, 0, "Reverse the order of any sort"},
107 {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS},
108 {"session", OPT_SESS,"SID", OPTION_ARG_OPTIONAL,
109 "Add the processes from the session"
110 " SID (which defaults to the sid of"
111 " the current process)" },
112
113 {0,0,0,0, "Here are some more options:"},
114 {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly"},
115 {"zaza", 'z', 0, 0, "Snit a zar"},
116
117 {0}
118};
119
120static const char args_doc[] = "STRING";
121static const char doc[] = "Test program for argp."
122 "\vThis doc string comes after the options."
123 "\nHey! Some manual formatting!"
124 "\nThe current time is: %s";
125
126static void
127popt (int key, char *arg)
128{
129 char buf[10];
130 if (isprint (key))
131 sprintf (buf, "%c", key);
132 else
133 sprintf (buf, "%d", key);
134 if (arg)
135 printf ("KEY %s: %s\n", buf, arg);
136 else
137 printf ("KEY %s\n", buf);
138}
139
140static error_t
141parse_opt (int key, char *arg, struct argp_state *state)
142{
143 struct params *params = state->input;
144
145 switch (key)
146 {
147 case ARGP_KEY_NO_ARGS:
148 printf ("NO ARGS\n");
149 break;
150
151 case ARGP_KEY_ARG:
152 if (state->arg_num > 0)
153 return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser. */
154 printf ("ARG: %s\n", arg);
155 break;
156
157 case 'f':
158 if (arg)
159 params->foonly = atoi (arg);
160 else
161 params->foonly = params->foonly_default;
162 popt (key, arg);
163 break;
164
165 case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
166 case 'r': case OPT_SESS: case 'z':
167 popt (key, arg);
168 break;
169
170 default:
171 return ARGP_ERR_UNKNOWN;
172 }
173 return 0;
174}
175
176static char *
177help_filter (int key, const char *text, void *input)
178{
179 char *new_text;
180 struct params *params = input;
181
182 if (key == ARGP_KEY_HELP_POST_DOC && text)
183 {
184 time_t now = time (0);
185 asprintf (&new_text, text, ctime (&now));
186 }
187 else if (key == 'f')
188 /* Show the default for the --foonly option. */
189 asprintf (&new_text, "%s (ZOT defaults to %x)",
190 text, params->foonly_default);
191 else
192 new_text = (char *)text;
193
194 return new_text;
195}
196
197static struct argp_child argp_children[] = { { &sub_argp }, { 0 } };
198static struct argp argp = {
199 options, parse_opt, args_doc, doc, argp_children, help_filter
200};
201
202
203int
204main (int argc, char **argv)
205{
206 struct params params;
207 params.foonly = 0;
208 params.foonly_default = random ();
209 argp_parse (&argp, argc, argv, 0, 0, &params);
210 printf ("After parsing: foonly = %x\n", params.foonly);
211 return 0;
212}
Note: See TracBrowser for help on using the repository browser.