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