📄 parseargv.3
字号:
'\"'\" Copyright (c) 1990-1992 The Regents of the University of California.'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" SCCS: @(#) ParseArgv.3 1.17 97/10/31 12:58:44'\" .so man.macros.TH Tk_ParseArgv 3 "" Tk "Tk Library Procedures".BS.SH NAMETk_ParseArgv \- process command-line options.SH SYNOPSIS.nf\fB#include <tk.h>\fR.spint\fBTk_ParseArgv\fR(\fIinterp, tkwin, argcPtr, argv, argTable, flags\fR).SH ARGUMENTS.AS Tk_ArgvInfo *argTable.AP Tcl_Interp *interp inInterpreter to use for returning error messages..AP Tk_Window tkwin inWindow to use when arguments specify Tk options. If NULL, thenno Tk options will be processed..AP int argcPtr in/outPointer to number of arguments in argv; gets modified to holdnumber of unprocessed arguments that remain after the call..AP char **argv in/outCommand line arguments passed to main program. Modified tohold unprocessed arguments that remain after the call..AP Tk_ArgvInfo *argTable inArray of argument descriptors, terminated by element withtype TK_ARGV_END..AP int flags inIf non-zero, then it specifies one or more flags that control theparsing of arguments. Different flags may be OR'ed together.The flags currently defined are TK_ARGV_DONT_SKIP_FIRST_ARG,TK_ARGV_NO_ABBREV, TK_ARGV_NO_LEFTOVERS, and TK_ARGV_NO_DEFAULTS..BE.SH DESCRIPTION.PP\fBTk_ParseArgv\fR processes an array of command-line arguments accordingto a table describing the kinds of arguments that are expected.Each of the arguments in \fIargv\fR is processed in turn: if it matchesone of the entries in \fIargTable\fR, the argument is processedaccording to that entry and discarded. The arguments that do notmatch anything in \fIargTable\fR are copied down to the beginningof \fIargv\fR (retaining their original order) and returned tothe caller. At the end of the call\fBTk_ParseArgv\fR sets \fI*argcPtr\fR to hold the number ofarguments that are left in \fIargv\fR, and \fIargv[*argcPtr]\fRwill hold the value NULL. Normally, \fBTk_ParseArgv\fRassumes that \fIargv[0]\fR is a command name, so it is treated likean argument that doesn't match \fIargTable\fR and returned to thecaller; however, if the TK_ARGV_DONT_SKIP_FIRST_ARG bit is set in\fIflags\fR then \fIargv[0]\fR will be processed just like the otherelements of \fIargv\fR..PP\fBTk_ParseArgv\fR normally returns the value TCL_OK. If an erroroccurs while parsing the arguments, then TCL_ERROR is returned and\fBTk_ParseArgv\fR will leave an error message in \fIinterp->result\fRin the standard Tcl fashion. Inthe event of an error return, \fI*argvPtr\fR will not have beenmodified, but \fIargv\fR could have been partially modified. Thepossible causes of errors are explained below..PPThe \fIargTable\fR array specifies the kinds of arguments that areexpected; each of its entries has the following structure:.CStypedef struct { char *\fIkey\fR; int \fItype\fR; char *\fIsrc\fR; char *\fIdst\fR; char *\fIhelp\fR;} Tk_ArgvInfo;.CEThe \fIkey\fR field is a string such as ``\-display'' or ``\-bg''that is compared with the values in \fIargv\fR. \fIType\fRindicates how to process an argument that matches \fIkey\fR(more on this below). \fISrc\fR and \fIdst\fR are additionalvalues used in processing the argument. Their exact usagedepends on \fItype\fR, but typically \fIsrc\fR indicatesa value and \fIdst\fR indicates where to store thevalue. The \fBchar *\fR declarations for \fIsrc\fR and \fIdst\fRare placeholders: the actual types may be different. Lastly,\fIhelp\fR is a string giving a brief descriptionof this option; this string is printed when users ask for helpabout command-line options..PPWhen processing an argument in \fIargv\fR, \fBTk_ParseArgv\fRcompares the argument to each of the \fIkey\fR's in \fIargTable\fR.\fBTk_ParseArgv\fR selects the first specifier whose \fIkey\fR matchesthe argument exactly, if such a specifier exists. Otherwise\fBTk_ParseArgv\fR selects a specifier for which the argumentis a unique abbreviation. If the argument is a unique abbreviationfor more than one specifier, then an error is returned. If thereis no matching entry in \fIargTable\fR, then the argument isskipped and returned to the caller..PPOnce a matching argument specifier is found, \fBTk_ParseArgv\fRprocesses the argument according to the \fItype\fR field of thespecifier. The argument that matched \fIkey\fR is called ``the matchingargument'' in the descriptions below. As part of the processing,\fBTk_ParseArgv\fR may also use the next argument in \fIargv\fRafter the matching argument, which is called ``the followingargument''. The legal values for \fItype\fR, and the processingthat they cause, are as follows:.TP\fBTK_ARGV_END\fRMarks the end of the table. The last entry in \fIargTable\fRmust have this type; all of its other fields are ignored and itwill never match any arguments..TP\fBTK_ARGV_CONSTANT\fR\fISrc\fR is treated as an integer and \fIdst\fR is treatedas a pointer to an integer. \fISrc\fR is stored at \fI*dst\fR.The matching argument is discarded..TP\fBTK_ARGV_INT\fRThe following argument must contain aninteger string in the format accepted by \fBstrtol\fR (e.g. ``0''and ``0x'' prefixes may be used to specify octal or hexadecimalnumbers, respectively). \fIDst\fR is treated as a pointer to aninteger; the following argument is converted to an integer valueand stored at \fI*dst\fR. \fISrc\fR is ignored. The matchingand following arguments are discarded from \fIargv\fR..TP\fBTK_ARGV_FLOAT\fRThe following argument must contain a floating-point number inthe format accepted by \fBstrtol\fR.\fIDst\fR is treated as the address of an double-precisionfloating point value; the following argument is converted to adouble-precision value and stored at \fI*dst\fR. The matchingand following arguments are discarded from \fIargv\fR..TP\fBTK_ARGV_STRING\fRIn this form, \fIdst\fR is treated as a pointer to a (char *);\fBTk_ParseArgv\fR stores at \fI*dst\fR a pointer to the followingargument, and discards the matching and following arguments from\fIargv\fR. \fISrc\fR is ignored..TP\fBTK_ARGV_UID\fRThis form is similar to TK_ARGV_STRING, except that the argumentis turned into a Tk_Uid by calling \fBTk_GetUid\fR.\fIDst\fR is treated as a pointer to aTk_Uid; \fBTk_ParseArgv\fR stores at \fI*dst\fR the Tk_Uidcorresponding to the followingargument, and discards the matching and following arguments from\fIargv\fR. \fISrc\fR is ignored..TP\fBTK_ARGV_CONST_OPTION\fRThis form causes a Tk option to be set (as if the \fBoption\fRcommand had been invoked). The \fIsrc\fR field is treated as apointer to a string giving the value of an option, and \fIdst\fRis treated as a pointer to the name of the option. The matchingargument is discarded. If \fItkwin\fR is NULL, then argumentspecifiers of this type are ignored (as if they did not exist)..TP\fBTK_ARGV_OPTION_VALUE\fRThis form is similar to TK_ARGV_CONST_OPTION, except that thevalue of the option is taken from the following argument insteadof from \fIsrc\fR. \fIDst\fR is used as the name of the option.\fISrc\fR is ignored. The matching and following argumentsare discarded. If \fItkwin\fR is NULL, then argumentspecifiers of this type are ignored (as if they did not exist)..TP\fBTK_ARGV_OPTION_NAME_VALUE\fRIn this case the following argument is taken as the name of a Tkoption and the argument after that is taken as the value for thatoption. Both \fIsrc\fR and \fIdst\fR are ignored. All threearguments are discarded from \fIargv\fR. If \fItkwin\fR is NULL,then argumentspecifiers of this type are ignored (as if they did not exist)..TP\fBTK_ARGV_HELP\fRWhen this kind of option is encountered, \fBTk_ParseArgv\fR uses the\fIhelp\fR fields of \fIargTable\fR to format a message describingall the valid arguments. The message is placed in \fIinterp->result\fRand \fBTk_ParseArgv\fR returns TCL_ERROR. When this happens, thecaller normally prints the help message and aborts. If the \fIkey\fRfield of a TK_ARGV_HELP specifier is NULL, then the specifier willnever match any arguments; in this case the specifier simply providesextra documentation, which will be included when some otherTK_ARGV_HELP entry causes help information to be returned..TP\fBTK_ARGV_REST\fRThis option is used by programs or commands that allow the lastseveral of their options to be the name and/or options for someother program. If a \fBTK_ARGV_REST\fR argument is found, then\fBTk_ParseArgv\fR doesn't process anyof the remaining arguments; it returns them all atthe beginning of \fIargv\fR (along with any other unprocessed arguments).In addition, \fBTk_ParseArgv\fR treats \fIdst\fR as the address of aninteger value, and stores at \fI*dst\fR the index of the first of the\fBTK_ARGV_REST\fR options in the returned \fIargv\fR. This allows theprogram to distinguish the \fBTK_ARGV_REST\fR options from otherunprocessed options that preceded the \fBTK_ARGV_REST\fR..TP\fBTK_ARGV_FUNC\fRFor this kind of argument, \fIsrc\fR is treated as the address ofa procedure, which is invoked to process the following argument.The procedure should have the following structure:.RS.CSint\fIfunc\fR(\fIdst\fR, \fIkey\fR, \fInextArg\fR) char *\fIdst\fR; char *\fIkey\fR; char *\fInextArg\fR;{}.CEThe \fIdst\fR and \fIkey\fR parameters will contain thecorresponding fields from the \fIargTable\fR entry, and\fInextArg\fR will point to the following argument from \fIargv\fR(or NULL if there aren't any more arguments left in \fIargv\fR).If \fIfunc\fR uses \fInextArg\fR (so that\fBTk_ParseArgv\fR should discard it), then it should return 1. Otherwise itshould return 0 and \fBTkParseArgv\fR will process the followingargument in the normal fashion. In either event the matching argumentis discarded..RE.TP\fBTK_ARGV_GENFUNC\fRThis form provides a more general procedural escape. It treats\fIsrc\fR as the address of a procedure, and passes that procedureall of the remaining arguments. The procedure should have the followingform:.RS.CSint\fIgenfunc\fR(dst, interp, key, argc, argv) char *\fIdst\fR; Tcl_Interp *\fIinterp\fR; char *\fIkey\fR; int \fIargc\fR; char **\fIargv\fR;{}.CEThe \fIdst\fR and \fIkey\fR parameters will contain thecorresponding fields from the \fIargTable\fR entry. \fIInterp\fRwill be the same as the \fIinterp\fR argument to \fBTcl_ParseArgv\fR.\fIArgc\fR and \fIargv\fR refer to all of the options after thematching one. \fIGenfunc\fR should behave in a fashion similarto \fBTk_ParseArgv\fR: parse as many of the remaining arguments as it can,then return any that are left by compacting them to the beginning of\fIargv\fR (starting at \fIargv\fR[0]). \fIGenfunc\fRshould return a count of how many arguments are left in \fIargv\fR;\fBTk_ParseArgv\fR will process them. If \fIgenfunc\fR encountersan error then it should leave an error message in \fIinterp->result\fR,in the usual Tcl fashion, and return -1; when this happens\fBTk_ParseArgv\fR will abort its processing and return TCL_ERROR..RE.SH "FLAGS".TP\fBTK_ARGV_DONT_SKIP_FIRST_ARG\fR\fBTk_ParseArgv\fR normally treats \fIargv[0]\fR as a programor command name, and returns it to the caller just as if ithadn't matched \fIargTable\fR. If this flag is given, then\fIargv[0]\fR is not given special treatment..TP\fBTK_ARGV_NO_ABBREV\fRNormally, \fBTk_ParseArgv\fR accepts unique abbreviations for\fIkey\fR values in \fIargTable\fR. If this flag is given thenonly exact matches will be acceptable..TP\fBTK_ARGV_NO_LEFTOVERS\fRNormally, \fBTk_ParseArgv\fR returns unrecognized arguments to thecaller. If this bit is set in \fIflags\fR then \fBTk_ParseArgv\fRwill return an error if it encounters any argument that doesn'tmatch \fIargTable\fR. The only exception to this rule is \fIargv[0]\fR,which will be returned to the caller with no errors aslong as TK_ARGV_DONT_SKIP_FIRST_ARG isn't specified..TP\fBTK_ARGV_NO_DEFAULTS\fRNormally, \fBTk_ParseArgv\fR searches an internal table ofstandard argument specifiers in addition to \fIargTable\fR. Ifthis bit is set in \fIflags\fR, then \fBTk_ParseArgv\fR willuse only \fIargTable\fR and not its default table..SH EXAMPLE.PPHere is an example definition of an \fIargTable\fR andsome sample command lines that use the options. Note the effecton \fIargc\fR and \fIargv\fR; arguments processed by \fBTk_ParseArgv\fRare eliminated from \fIargv\fR, and \fIargc\fRis updated to reflect reduced number of arguments..CS/* * Define and set default values for globals. */int debugFlag = 0;int numReps = 100;char defaultFileName[] = "out";char *fileName = defaultFileName;Boolean exec = FALSE;/* * Define option descriptions. */Tk_ArgvInfo argTable[] = { {"-X", TK_ARGV_CONSTANT, (char *) 1, (char *) &debugFlag, "Turn on debugging printfs"}, {"-N", TK_ARGV_INT, (char *) NULL, (char *) &numReps, "Number of repetitions"}, {"-of", TK_ARGV_STRING, (char *) NULL, (char *) &fileName, "Name of file for output"}, {"x", TK_ARGV_REST, (char *) NULL, (char *) &exec, "File to exec, followed by any arguments (must be last argument)."}, {(char *) NULL, TK_ARGV_END, (char *) NULL, (char *) NULL, (char *) NULL}};main(argc, argv) int argc; char *argv[];{ \&... if (Tk_ParseArgv(interp, tkwin, &argc, argv, argTable, 0) != TCL_OK) { fprintf(stderr, "%s\en", interp->result); exit(1); } /* * Remainder of the program. */}.CE.PPNote that default values can be assigned to variables named in\fIargTable\fR: the variables will only be overwritten if theparticular arguments are present in \fIargv\fR.Here are some example command lines and their effects..CSprog -N 200 infile # just sets the numReps variable to 200prog -of out200 infile # sets fileName to reference "out200"prog -XN 10 infile # sets the debug flag, also sets numReps.CEIn all of the above examples, \fIargc\fR will be set by \fBTk_ParseArgv\fR to 2,\fIargv\fR[0] will be ``prog'', \fIargv\fR[1] will be ``infile'',and \fIargv\fR[2] will be NULL..SH KEYWORDSarguments, command line, options
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -