📄 opt.man
字号:
'\" Copyright 1991 Regents of the University of California'\" Permission to use, copy, modify, and distribute this'\" documentation for any purpose and without fee is hereby'\" granted, provided that this notice appears in all copies.'\" The University of California makes no representations about'\" the suitability of this material for any purpose. It is'\" provided "as is" without express or implied warranty.'\" '\" $Header: /export/d/stolcke/project/srilm/src/misc/RCS/Opt.man,v 1.4 1991/01/28 17:00:56 kupfer Exp $ SPRITE (Berkeley)'/" .so \*(]ltmac.sprite.HS Opt lib.BS.SH NAMEOpt_Parse, Opt_PrintUsage \- Manage command line options.SH SYNOPSIS\fB#include <option.h>\fR.spint\fBOpt_Parse\fR(\fIargc, argv, optionArray, numOptions, flags\fR).spvoid\fBOpt_PrintUsage\fR(\fIcommandName, optionArray, numOptions\fR).SH ARGUMENTS.AS Option *optionArray.AP int argc inNumber of arguments on command line..AP char **argv in/outCommand line arguments passed to main program..AP char *commandName inName of the program being run, usually \fIargv\fR[0]..AP Option *optionArray inAn array of option descriptions..AP int numOptions inNumber of elements in optionArray, usually obtained using the\fBOpt_Number\fR macro, e.g. \fBOpt_Number\fR(optionArray)..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. Theonly flags currently defined are OPT_ALLOW_CLUSTERING and OPT_OPTIONS_FIRST. .BE.SH DESCRIPTION.PP\fBOpt_Parse\fR parses the command line arguments of a program accordingto an array of option descriptions. Starting with \fIargv\fR[1], it parsesas many options as it can and returns the rest of the options inthe \fIargv\fR array, compacted to the beginning of the array (startingwith \fIargv\fR[1]). The return value indicates how many options arereturned in the \fIargv\fR array (including \fIargv\fR[0]).Opt_Parse returns options that don't start with ``-'' unless theyare arguments for options that it parses. \fBOpt_Parse\fR also returnsany options following an \fBOPT_REST\fR option (see below for more details)..PPEach element of the array\fIoptionArray\fR has the following structure:.DS.ta 2c\fBtypedef struct\fR Option { \fBint\fR \fItype\fR; \fBchar\fR *\fIkey\fR; \fBchar\fR *\fIvaluePtr\fR; \fBchar\fR *\fIdocMsg\fR;} Option;.DE.LPThe \fIkey\fR field is a string that identifies this option. Forexample, if \fIkey\fR is \fBfoo\fR, then this Option will matcha \fB\-foo\fR command-line option. If \fIkey\fR is the emptystring (``'') then it matches a \fB\-\fR command-line option.If \fIkey\fR is NULL, the Optionwill not match any command-line options (this feature is only usefulfor OPT_DOC options).\fIDocMsg\fR is a documentation string to print out as part of ahelp message.The \fItype\fR field determines what to do when this Option ismatched. It also determines the meaning of the \fIvaluePtr\fRfield. \fIType\fR should always be specified using one of thefollowing macros:.TP\fBOPT_TRUE\fRTreats \fIvaluePtr\fR as the address of an integer, and storesthe value 1 in that integer..TP\fBOPT_FALSE\fRTreats \fIvaluePtr\fR as the address of an integer and storesthe value 0 in that integer..TP\fBOPT_CONSTANT\fR(\fIvalue\fR)This is a generalized form of OPT_TRUE and OPT_FALSE. Treats\fIvaluePtr\fR as the address of an integer and stores \fIvalue\fRin that integer. \fIValue\fR must be a positive integer..TP\fBOPT_INT\fRThe next argument after the one matching \fIkey\fR 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). \fIValuePtr\fRwill be treated as the address of an integer, and the value givenby the next argument will be stored there..TP\fBOPT_TIME\fRThe next argument after the one matching \fIkey\fR must contain astring that is parsable as a date and time. Currently, only twoformats are recognized:.DS\fIseconds\fR.DE.IPand.DS\fIyy\fB.\fImm\fB.\fIdd\fB.\fIhh\fB.\fImm\fB.\fIss\fR.DE.IPThe first form is simply the number of seconds since the start of theepoch (1 January 1970, 0 hours GMT). The second form specifies theyear (e.g., 91 or 1991),month (1-12), day of the month, hours (0-23), minutes (0-59), andseconds (0-59). All fields must be specified.\fIValuePtr\fRwill be treated as the address of a.B time_t(defined in.BR <time.h> ), and the given time will be stored there.All times are in terms of the current timezone and daylight savingsrules..IPNote that this flavor can clobber the static buffer used by the .B localtimelibrary routine..TP\fBOPT_FLOAT\fRThe next argument after the one matching \fIkey\fR must contain afloating-point number in the format accepted by \fBstrtol\fR.\fIValuePtr\fR will be treated as the address of an double-precisionfloating point value, and thevalue given by the next argument will be stored there..TP\fBOPT_STRING\fRTreats \fIvaluePtr\fR as the address of a (char *), and stores a pointerto the next argument in the locationpointed to by \fIvaluePtr\fR..TP\fBOPT_DOC\fRThis option is intended primarily as a way of printing extra documentationduring help message printouts. It isn't normally used as an actualoption (and normally its \fIkey\fR field is NULL).If it is invoked as an option, then the same thing happens as forthe ``-?'' option: descriptions get printed for all options in\fIoptionArray\fR and \fBOpt_Parse\fR calls exit(0) to terminate the process..TP\fBOPT_REST\fRThis option is used by programs that allow the last several of theiroptions to be the name and/or options for some other program. Ifan \fBOPT_REST\fR option is found, then \fBOpt_Parse\fR doesn't process anyof the remaining arguments; it returns them all at the beginning of \fIargv\fR.In addition, \fBOpt_Parse\fR treats \fIvaluePtr\fR as the address of aninteger value, and stores in that value the index of the first of the\fBOPT_REST\fR options in the returned \fIargv\fR. This allows theprogram to distinguish the \fBOPT_REST\fR options from otherunprocessed options that preceeded the \fBOPT_REST\fR..TP\fBOPT_FUNC\fRWhen one of these options is encountered, \fIvaluePtr\fR is treatedas the address of a function which is then calledwith the following calling sequence:.DS.ta 1c 2c 3c 4c 5c 6c\fBint\fIfunc(optString, nextArg) \fBchar\fR *\fIoptString\fR; \fBchar\fR *\fInextArg\fR;{}.DE.IPThe \fIoptString\fR parameter points to the current option, and\fInextArg\fR points to the next option from \fIargv\fR (or NULLif there aren't any more options in \fIargv\fR. If \fIfunc\fRuses \fInextArg\fR as an argument to the current option (so thatOpt_Parse should skip it), then it should return 1. Otherwise itshould return 0..TP\fBOPT_GENFUNC\fRTreat \fIvaluePtr\fR as the address of a function and pass all of theremaining arguments to it in the following way:.DS.ta 1c 2c 3c 4c 5c 6c\fBint\fIgenfunc(optString, argc, argv) \fBchar\fR *\fIoptString\fR; \fBint\fR \fIargc\fR; \fBchar\fR **\fIargv\fR;{}.DE.IP\fIArgc\fR and \fIargv\fR refer to all of the options after theone that triggered the call, and \fIoptString\fR points to thetriggering option. \fIGenfunc\fR should behave in a fashion similarto \fBOpt_Parse\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;\fBOpt_Parse\fR will process them..SH "FLAGS".IP \fBOPT_ALLOW_CLUSTERING\fRThis willpermit several options to be clustered together with asingle ``-'', e.g., ``foo -abc'' will be handled the same way as``foo -a -b -c''. OPT_ALLOW_CLUSTERING is likely to cause confusingbehavior unless each option is identified with a single character..IP \fBOPT_OPTIONS_FIRST\fRThis causes \fBOpt_Parse\fR to stop parsing the command line anytime somethingthat is not an option is detected. Thus, a program that takes some options followed by a command to execute (along with that command's options) can parse its own options using OPT_OPTIONS_FIRST. When the command to execute is encountered, assuming it does not begin with a hyphen, \fBOpt_Parse\fR will return the command and its arguments starting at \fIargv\fR[1], ignoringany arguments with hyphens following the first non-option..SH "USAGE MESSAGE".PP\fBOpt_PrintUsage\fR may be invoked to print out all the documentation strings(plus option names and default values) for a command's options. If\fBOpt_Parse\fR encounters an option ``-?'' or ``-help'', then it will call\fBOpt_PrintUsage\fRand exit. Note: in some shells the question-mark must be escaped(e.g., ``foo -\e?'' in \fIcsh\fR)..SH EXAMPLE.PPHere is an example definition of a set of option descriptions andsome sample command lines that use the options. Note the effecton argc and argv; command arguments that get interpreted asoptions or option values are eliminated from argv, and argcis updated to reflect reduced number of arguments..DS/* * Define and set default values for globals. */Boolean debugFlag = FALSE;int numReps = 100;char defaultFileName[] = "out";char *fileName = defaultFileName;Boolean exec = FALSE;/* * Define option descriptions. */Option optionArray[] = { OPT_TRUE, "X", (char *) &debugFlag, "Turn on debugging printfs", OPT_INT, "N", (char *) &numReps, "Number of repetitions", OPT_STRING, "of", (char *) &fileName, "Output filename", OPT_REST, "x", (char *) &exec, "File to exec, followed by any arguments (must be last argument).",};main(argc, argv) int argc; char *argv[];{ Opt_Parse(argc, argv, optionArray, Opt_Number(optionArray), OPT_ALLOW_CLUSTERING); /* * the rest of the program. */}.DE.PPNote that default values can be assigned to option variables.Also, numOptions gets calculated by the compiler in this example.Here are some example command lines and their effects..DSprog -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.DEIn all of the above examples, the return value from Opt_Parse will be 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 + -