Debian Codegraph @

pod/pTk/ParseArgv.pod

raw
1 # Copyright (c) 1990-1992 The Regents of the University of California.
2 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
3 # See the file "license.terms" for information on usage and redistribution
4 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
5 #
6 #
7
8 =head1 NAME
9
10 Tk_ParseArgv - process command-line options
11
12 =for category C Programming
13
14 =head1 SYNOPSIS
15
16 B<#include E<lt>tk.hE<gt>>
17
18 int
19 B<Tk_ParseArgv>(I<interp, tkwin, argcPtr, argv, argTable, flags>)
20
21 =head1 ARGUMENTS
22
23 =over 4
24
25 =item Tcl_Interp *interp (in)
26
27 Interpreter to use for returning error messages.
28
29 =item Tk_Window tkwin (in)
30
31 Window to use when arguments specify Tk options. If NULL, then
32 no Tk options will be processed.
33
34 =item int argcPtr (in/out)
35
36 Pointer to number of arguments in argv; gets modified to hold
37 number of unprocessed arguments that remain after the call.
38
39 =item char **argv (in/out)
40
41 Command line arguments passed to main program. Modified to
42 hold unprocessed arguments that remain after the call.
43
44 =item Tk_ArgvInfo *argTable (in)
45
46 Array of argument descriptors, terminated by element with
47 type TK_ARGV_END.
48
49 =item int flags (in)
50
51 If non-zero, then it specifies one or more flags that control the
52 parsing of arguments. Different flags may be OR'ed together.
53 The flags currently defined are TK_ARGV_DONT_SKIP_FIRST_ARG,
54 TK_ARGV_NO_ABBREV, TK_ARGV_NO_LEFTOVERS, and TK_ARGV_NO_DEFAULTS.
55
56 =back
57
58 =head1 DESCRIPTION
59
60 B<Tk_ParseArgv> processes an array of command-line arguments according
61 to a table describing the kinds of arguments that are expected.
62 Each of the arguments in I<argv> is processed in turn: if it matches
63 one of the entries in I<argTable>, the argument is processed
64 according to that entry and discarded. The arguments that do not
65 match anything in I<argTable> are copied down to the beginning
66 of I<argv> (retaining their original order) and returned to
67 the caller. At the end of the call
68 B<Tk_ParseArgv> sets I<*argcPtr> to hold the number of
69 arguments that are left in I<argv>, and I<argv[*argcPtr]>
70 will hold the value NULL. Normally, B<Tk_ParseArgv>
71 assumes that I<argv[0]> is a command name, so it is treated like
72 an argument that doesn't match I<argTable> and returned to the
73 caller; however, if the TK_ARGV_DONT_SKIP_FIRST_ARG bit is set in
74 I<flags> then I<argv[0]> will be processed just like the other
75 elements of I<argv>.
76
77 B<Tk_ParseArgv> normally returns the value TCL_OK. If an error
78 occurs while parsing the arguments, then TCL_ERROR is returned and
79 B<Tk_ParseArgv> will leave an error message in I<interp-E<gt>result>
80 in the standard Tcl fashion. In
81 the event of an error return, I<*argvPtr> will not have been
82 modified, but I<argv> could have been partially modified. The
83 possible causes of errors are explained below.
84
85 The I<argTable> array specifies the kinds of arguments that are
86 expected; each of its entries has the following structure:
87
88 typedef struct {
89 char *key;
90 int type;
91 char *src;
92 char *dst;
93 char *help;
94 } Tk_ArgvInfo;
95
96 The I<key> field is a string such as ``-display'' or ``-bg''
97 that is compared with the values in I<argv>. I<Type>
98 indicates how to process an argument that matches I<key>
99 (more on this below). I<Src> and I<dst> are additional
100 values used in processing the argument. Their exact usage
101 depends on I<type>, but typically I<src> indicates
102 a value and I<dst> indicates where to store the
103 value. The B<char *> declarations for I<src> and I<dst>
104 are placeholders: the actual types may be different. Lastly,
105 I<help> is a string giving a brief description
106 of this option; this string is printed when users ask for help
107 about command-line options.
108
109 When processing an argument in I<argv>, B<Tk_ParseArgv>
110 compares the argument to each of the I<key>'s in I<argTable>.
111 B<Tk_ParseArgv> selects the first specifier whose I<key> matches
112 the argument exactly, if such a specifier exists. Otherwise
113 B<Tk_ParseArgv> selects a specifier for which the argument
114 is a unique abbreviation. If the argument is a unique abbreviation
115 for more than one specifier, then an error is returned. If there
116 is no matching entry in I<argTable>, then the argument is
117 skipped and returned to the caller.
118
119 Once a matching argument specifier is found, B<Tk_ParseArgv>
120 processes the argument according to the I<type> field of the
121 specifier. The argument that matched I<key> is called ``the matching
122 argument'' in the descriptions below. As part of the processing,
123 B<Tk_ParseArgv> may also use the next argument in I<argv>
124 after the matching argument, which is called ``the following
125 argument''. The legal values for I<type>, and the processing
126 that they cause, are as follows:
127
128 =over 4
129
130 =item B<TK_ARGV_END>
131
132 Marks the end of the table. The last entry in I<argTable>
133 must have this type; all of its other fields are ignored and it
134 will never match any arguments.
135
136 =item B<TK_ARGV_CONSTANT>
137
138 I<Src> is treated as an integer and I<dst> is treated
139 as a pointer to an integer. I<Src> is stored at I<*dst>.
140 The matching argument is discarded.
141
142 =item B<TK_ARGV_INT>
143
144 The following argument must contain an
145 integer string in the format accepted by B<strtol> (e.g. ``0''
146 and ``0x'' prefixes may be used to specify octal or hexadecimal
147 numbers, respectively). I<Dst> is treated as a pointer to an
148 integer; the following argument is converted to an integer value
149 and stored at I<*dst>. I<Src> is ignored. The matching
150 and following arguments are discarded from I<argv>.
151
152 =item B<TK_ARGV_FLOAT>
153
154 The following argument must contain a floating-point number in
155 the format accepted by B<strtol>.
156 I<Dst> is treated as the address of an double-precision
157 floating point value; the following argument is converted to a
158 double-precision value and stored at I<*dst>. The matching
159 and following arguments are discarded from I<argv>.
160
161 =item B<TK_ARGV_STRING>
162
163 In this form, I<dst> is treated as a pointer to a (char *);
164 B<Tk_ParseArgv> stores at I<*dst> a pointer to the following
165 argument, and discards the matching and following arguments from
166 I<argv>. I<Src> is ignored.
167
168 =item B<TK_ARGV_UID>
169
170 This form is similar to TK_ARGV_STRING, except that the argument
171 is turned into a Tk_Uid by calling B<Tk_GetUid>.
172 I<Dst> is treated as a pointer to a
173 Tk_Uid; B<Tk_ParseArgv> stores at I<*dst> the Tk_Uid
174 corresponding to the following
175 argument, and discards the matching and following arguments from
176 I<argv>. I<Src> is ignored.
177
178 =item B<TK_ARGV_CONST_OPTION>
179
180 This form causes a Tk option to be set (as if the B<option>
181 command had been invoked). The I<src> field is treated as a
182 pointer to a string giving the value of an option, and I<dst>
183 is treated as a pointer to the name of the option. The matching
184 argument is discarded. If I<tkwin> is NULL, then argument
185 specifiers of this type are ignored (as if they did not exist).
186
187 =item B<TK_ARGV_OPTION_VALUE>
188
189 This form is similar to TK_ARGV_CONST_OPTION, except that the
190 value of the option is taken from the following argument instead
191 of from I<src>. I<Dst> is used as the name of the option.
192 I<Src> is ignored. The matching and following arguments
193 are discarded. If I<tkwin> is NULL, then argument
194 specifiers of this type are ignored (as if they did not exist).
195
196 =item B<TK_ARGV_OPTION_NAME_VALUE>
197
198 In this case the following argument is taken as the name of a Tk
199 option and the argument after that is taken as the value for that
200 option. Both I<src> and I<dst> are ignored. All three
201 arguments are discarded from I<argv>. If I<tkwin> is NULL,
202 then argument
203 specifiers of this type are ignored (as if they did not exist).
204
205 =item B<TK_ARGV_HELP>
206
207 When this kind of option is encountered, B<Tk_ParseArgv> uses the
208 I<help> fields of I<argTable> to format a message describing
209 all the valid arguments. The message is placed in I<interp-E<gt>result>
210 and B<Tk_ParseArgv> returns TCL_ERROR. When this happens, the
211 caller normally prints the help message and aborts. If the I<key>
212 field of a TK_ARGV_HELP specifier is NULL, then the specifier will
213 never match any arguments; in this case the specifier simply provides
214 extra documentation, which will be included when some other
215 TK_ARGV_HELP entry causes help information to be returned.
216
217 =item B<TK_ARGV_REST>
218
219 This option is used by programs or commands that allow the last
220 several of their options to be the name and/or options for some
221 other program. If a B<TK_ARGV_REST> argument is found, then
222 B<Tk_ParseArgv> doesn't process any
223 of the remaining arguments; it returns them all at
224 the beginning of I<argv> (along with any other unprocessed arguments).
225 In addition, B<Tk_ParseArgv> treats I<dst> as the address of an
226 integer value, and stores at I<*dst> the index of the first of the
227 B<TK_ARGV_REST> options in the returned I<argv>. This allows the
228 program to distinguish the B<TK_ARGV_REST> options from other
229 unprocessed options that preceded the B<TK_ARGV_REST>.
230
231 =item B<TK_ARGV_FUNC>
232
233 For this kind of argument, I<src> is treated as the address of
234 a procedure, which is invoked to process the following argument.
235 The procedure should have the following structure:
236
237 =over 4
238
239 int
240 func(dst, key, nextArg)
241 char *dst;
242 char *key;
243 char *nextArg;
244 {
245 }
246
247 The I<dst> and I<key> parameters will contain the
248 corresponding fields from the I<argTable> entry, and
249 I<nextArg> will point to the following argument from I<argv>
250 (or NULL if there aren't any more arguments left in I<argv>).
251 If I<func> uses I<nextArg> (so that
252 B<Tk_ParseArgv> should discard it), then it should return 1. Otherwise it
253 should return 0 and B<TkParseArgv> will process the following
254 argument in the normal fashion. In either event the matching argument
255 is discarded.
256
257 =back
258
259 =item B<TK_ARGV_GENFUNC>
260
261 This form provides a more general procedural escape. It treats
262 I<src> as the address of a procedure, and passes that procedure
263 all of the remaining arguments. The procedure should have the following
264 form:
265
266 =over 4
267
268 int
269 genfunc(dst, interp, key, argc, argv)
270 char *dst;
271 Tcl_Interp *interp;
272 char *key;
273 int argc;
274 char **argv;
275 {
276 }
277
278 The I<dst> and I<key> parameters will contain the
279 corresponding fields from the I<argTable> entry. I<Interp>
280 will be the same as the I<interp> argument to B<Tcl_ParseArgv>.
281 I<Argc> and I<argv> refer to all of the options after the
282 matching one. I<Genfunc> should behave in a fashion similar
283 to B<Tk_ParseArgv>: parse as many of the remaining arguments as it can,
284 then return any that are left by compacting them to the beginning of
285 I<argv> (starting at I<argv>[0]). I<Genfunc>
286 should return a count of how many arguments are left in I<argv>;
287 B<Tk_ParseArgv> will process them. If I<genfunc> encounters
288 an error then it should leave an error message in I<interp-E<gt>result>,
289 in the usual Tcl fashion, and return -1; when this happens
290 B<Tk_ParseArgv> will abort its processing and return TCL_ERROR.
291
292 =back
293
294 =back
295
296 =head1 FLAGS
297
298 =over 4
299
300 =item B<TK_ARGV_DONT_SKIP_FIRST_ARG>
301
302 B<Tk_ParseArgv> normally treats I<argv[0]> as a program
303 or command name, and returns it to the caller just as if it
304 hadn't matched I<argTable>. If this flag is given, then
305 I<argv[0]> is not given special treatment.
306
307 =item B<TK_ARGV_NO_ABBREV>
308
309 Normally, B<Tk_ParseArgv> accepts unique abbreviations for
310 I<key> values in I<argTable>. If this flag is given then
311 only exact matches will be acceptable.
312
313 =item B<TK_ARGV_NO_LEFTOVERS>
314
315 Normally, B<Tk_ParseArgv> returns unrecognized arguments to the
316 caller. If this bit is set in I<flags> then B<Tk_ParseArgv>
317 will return an error if it encounters any argument that doesn't
318 match I<argTable>. The only exception to this rule is I<argv[0]>,
319 which will be returned to the caller with no errors as
320 long as TK_ARGV_DONT_SKIP_FIRST_ARG isn't specified.
321
322 =item B<TK_ARGV_NO_DEFAULTS>
323
324 Normally, B<Tk_ParseArgv> searches an internal table of
325 standard argument specifiers in addition to I<argTable>. If
326 this bit is set in I<flags>, then B<Tk_ParseArgv> will
327 use only I<argTable> and not its default table.
328
329 =back
330
331 =head1 EXAMPLE
332
333 Here is an example definition of an I<argTable> and
334 some sample command lines that use the options. Note the effect
335 on I<argc> and I<argv>; arguments processed by B<Tk_ParseArgv>
336 are eliminated from I<argv>, and I<argc>
337 is updated to reflect reduced number of arguments.
338
339 /*
340 * Define and set default values for globals.
341 */
342 int debugFlag = 0;
343 int numReps = 100;
344 char defaultFileName[] = "out";
345 char *fileName = defaultFileName;
346 Boolean exec = FALSE;
347
348 /*
349 * Define option descriptions.
350 */
351 Tk_ArgvInfo argTable[] = {
352 {"-X", TK_ARGV_CONSTANT, (char *) 1, (char *) &debugFlag,
353 "Turn on debugging printfs"},
354 {"-N", TK_ARGV_INT, (char *) NULL, (char *) &numReps,
355 "Number of repetitions"},
356 {"-of", TK_ARGV_STRING, (char *) NULL, (char *) &fileName,
357 "Name of file for output"},
358 {"x", TK_ARGV_REST, (char *) NULL, (char *) &exec,
359 "File to exec, followed by any arguments (must be last argument)."},
360 {(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL,
361 (char *) NULL}
362 };
363
364 main(argc, argv)
365 int argc;
366 char *argv[];
367 {
368 ...
369
370 if (Tk_ParseArgv(interp, tkwin, &argc, argv, argTable, 0) != TCL_OK) {
371 fprintf(stderr, "%s\n", interp->result);
372 exit(1);
373 }
374
375 /*
376 * Remainder of the program.
377 */
378 }
379
380 Note that default values can be assigned to variables named in
381 I<argTable>: the variables will only be overwritten if the
382 particular arguments are present in I<argv>.
383 Here are some example command lines and their effects.
384
385 prog -N 200 infile # just sets the numReps variable to 200
386 prog -of out200 infile # sets fileName to reference "out200"
387 prog -XN 10 infile # sets the debug flag, also sets numReps
388
389 In all of the above examples, I<argc> will be set by B<Tk_ParseArgv> to 2,
390 I<argv>[0] will be ``prog'', I<argv>[1] will be ``infile'',
391 and I<argv>[2] will be NULL.
392
393 =head1 KEYWORDS
394
395 arguments, command line, options