📄 getopt.1
字号:
.TH GETOPT 1 "May 31, 1997" Linux "".SH NAMEgetopt \- parse command options (enhanced).SH SYNOPSIS.BR getopt " optstring parameters".BR getopt " [options] [" \-\- "] optstring parameters".BR getopt " [options] " \-o | \-\-options " optstring [options] [" \-\- "] parameters".SH DESCRIPTION.B getoptis used to break up .RI ( parse )options in command lines for easy parsing byshell procedures, and to check for legal options.It uses the .SM GNU.BR getopt (3) routines to do this.The parameters .B getoptis called with can be divided into two parts: optionswhich modify the way getopt will parse.RI ( optionsand.I \-o|\-\-options optstringin the .BR SYNOPSIS), and the parameters which are to beparsed.RI ( parametersin the .BR SYNOPSIS).The second part will start at the first non\-option parameterthat is not an option argument, or after the first occurence of .RB ` \-\- '.If no .RB ` \-o ' or .RB ` \-\-options ' option is found in the first part, the firstparameter of the second part is used as the short options string.If the environment variable.B GETOPT_COMPATIBLEis set, or if its first parameter is not an option (does not start with a.RB ` \- ',this is the first format in the .BR SYNOPSIS),.B getoptwill generate output that is compatible with that of other versions of .BR getopt (1). It will still do parameter shuffling and recognize optionalarguments (see section.B COMPATIBILITYfor more information). Traditional implementations of.BR getopt (1)are unable to cope with whitespace and other (shell\-specific) special charactersin arguments and non\-option parameters. To solve this problem, this implementation can generatequoted output which must once again be interpreted by the shell (usuallyby using the.B evalcommand). This has the effect of preserving those characters, butyou must call .B getoptin a way that is no longer compatible with other versions (the second or third format in the .BR SYNOPSIS). To determine whether this enhanced version of.BR getopt (1)is installed, a special test option.RB ( \-T ) can be used..SH OPTIONS.IP "\-a, \-\-alternative"Allow long options to start with a single .RB ` \- '..IP "\-h, \-\-help"Output a small usage guide and exit succesfully. No other output is generated. .IP "\-l, \-\-longoptions longopts"The long (multi\-character) options to be recognized. More than one option namemay be specified at once, by separating the names with commas. This option may be given more than once, the .I longopts are cumulative.Each long option namein .I longopts may be followed by one colon to indicate it has a required argument,and by two colons to indicate it has an optional argument..IP "\-n, \-\-name progname"The name that will be used by the .BR getopt (3)routines when it reports errors. Note that errors of.BR getopt (1)are still reported as coming from getopt..IP "\-o, \-\-options shortopts"The short (one\-character) options to be recognized. If this option is notfound, the first parameter of .B getopt that does not start witha .RB ` \- ' (and is not an option argument) is used as the short options string.Each short option characterin .I shortopts may be followed by one colon to indicate it has a required argument,and by two colons to indicate it has an optional argument.The first character of shortopts may be .RB ` + ' or.RB ` \- ' to influence the wayoptions are parsed and output is generated (see section .B SCANNING MODESfor details)..IP "\-q, \-\-quiet"Disable error reporting by getopt(3)..IP "\-Q, \-\-quiet\-output"Do not generate normal output. Errors are still reported by.BR getopt (3), unless you also use .IR \-q ..IP "\-s, \-\-shell shell"Set quoting conventions to those of shell. If no \-s argument is found,the.SM BASHconventions are used. Valid arguments are currently.RB ` sh '.RB ` bash ',.RB ` csh ',and.RB ` tcsh '..IP "\-u, \-\-unquoted"Do not quote the output. Note that whitespace and special (shell\-dependent)characters can cause havoc in this mode (like they do with other.BR getopt (1)implementations)..IP "\-T \-\-test"Test if your .BR getopt (1) is this enhanced version or an old version. This generates no output, and sets the error status to 4. Other implementations of .BR getopt (1),and this version if the environment variable.B GETOPT_COMPATIBLEis set,will return .RB ` \-\- ' and error status 0..IP "\-V, \-\-version"Output version information and exit succesfully. No other output is generated. .SH PARSINGThis section specifies the format of the second part of the parameters of.B getopt(the .I parameters in the .BR SYNOPSIS ). The next section .RB ( OUTPUT ) describes the output that is generated. These parameters were typically the parameters a shell functionwas called with. Care must be taken that each parameter the shell function wascalled with corresponds to exactly one parameter in the parameter list of.B getopt (see the .BR EXAMPLES ). All parsing is done by the GNU .BR getopt (3) routines. The parameters are parsed from left to right. Each parameter is classified as ashort option, a long option, an argument to an option,or a non\-option parameter.A simple short option is a .RB ` \- ' followed by a short option character. Ifthe option has a required argument, it may be written directly after the optioncharacter or as the next parameter (ie. separated by whitespace on the command line). If theoption has an optional argument, it must be written directly after theoption character if present.It is possible to specify several short options after one .RB ` \- ', as long as all (except possibly the last) do not have required or optionalarguments.A long option normally begins with .RB ` \-\- ' followed by the long option name.If the option has a required argument, it may be written directly afterthe long option name, separated by .RB ` = ', or as the next argument (ie. separated by whitespace on the command line). If the option has an optional argument, it mustbe written directly after the long option name, separated by .RB ` = ', if present (if you add the .RB ` = ' but nothing behind it, it is interpretedas if no argument was present; this is a slight bug, see the .BR BUGS ).Long options may be abbreviated, as long as the abbreviation is notambiguous.Each parameter not starting with a .RB ` \- ', and not a required argument ofa previous option, is a non\-option parameter. Each parameter aftera .RB ` \-\- ' parameter is always interpreted as a non\-option parameter.If the environment variable .B POSIXLY_CORRECT is set, or if the shortoption string started with a .RB ` + ', all remaining parameters are interpretedas non\-option parameters as soon as the first non\-option parameter isfound..SH OUTPUTOutput is generated for each element described in the previous section. Output is donein the same order as the elements are specified in the input, exceptfor non\-option parameters. Output can be done in .I compatible .RI ( unquoted )mode, or in such way that whitespace and other special characters withinarguments and non\-option parameters are preserved (see .BR QUOTING ).When the output is processed in the shell script, it will seem to becomposed of distinct elements that can be processed one by one (by using theshift command in most shell languages). This is imperfect in unquoted mode,as elements can be split at unexpected places if they contain whitespaceor special characters.If there are problems parsing the parameters, for example because arequired argument is not found or an option is not recognized, an errorwill be reported on stderr, there will be no output for the offendingelement, and a non\-zero error status is returned.For a short option, a single .RB ` \- ' and the option character are generatedas one parameter. If the option has an argument, the nextparameter will be the argument. If the option takes an optional argument,but none was found, the next parameter will be generated but be empty inquoting mode,but no second parameter will be generated in unquoted (compatible) mode.Note that many other .BR getopt (1) implemetations do not support optional arguments.If several short options were specified after a single .RB ` \- ', each will be present in the output as a separate parameter.For a long option, .RB ` \-\- ' and the full option name are generated as oneparameter. This is done regardless whether the option was abbreviated orspecified with a single .RB ` \- ' in the input. Arguments are handled as with short options.Normally, no non\-option parameters output is generated until all optionsand their arguments have been generated. Then .RB ` \-\- ' is generated as asingle parameter, and after it the non\-option parameters in the orderthey were found, each as a separate parameter.Only if the first character of the short options string was a .RB ` \- ',non\-option parameter output is generated at the place they are found in the input (this is not supported if the first format of the .B SYNOPSISis used; in that case all preceding occurences of.RB ` \- 'and .RB ` + 'are ignored). .SH QUOTINGIn compatible mode, whitespace or 'special' characters in arguments ornon\-option parameters are not handled correctly. As the output is fed to the shell script, the script does not know how it is supposed to break the output into separate parameters. To circumvent thisproblem, this implementation offers quoting. The idea is that outputis generated with quotes around each parameter. When this output is onceagain fed to the shell (usually by a shell .B eval command), it is split correctly into separate parameters.Quoting is not enabled if the environment variable.B GETOPT_COMPATIBLEis set, if the first form of the.B SYNOPSISis used, or if the option.RB ` \-u 'is found.Different shells use different quoting conventions. You can use the .RB ` \-s 'option to select the shell you are using. The following shells arecurrently supported:.RB ` sh ',.RB ` bash ',.RB ` csh ' and.RB ` tcsh '.Actually, only two `flavors' are distinguished: sh\-like quoting conventions and csh\-like quoting conventions. Chances are that if you use another shellscript language, one of these flavors can still be used..SH "SCANNING MODES"The first character of the short options string may be a.RB ` \- 'or a.RB ` + 'to indicate a special scanning mode. If the first calling formin the.B SYNOPSIS is used they are ignored; the environment variable.B POSIXLY_CORRECTis still examined, though.If the first character is .RB ` + ',or if the environment variable .B POSIXLY_CORRECTis set, parsing stops as soon as the first non\-option parameter (ie. a parameter that does not start with a .RB ` \- ')is found thatis not an option argument. The remaining parameters are all interpreted asnon\-option parameters.If the first character is a.RB ` \- ',non\-option parameters are outputed at the place where they are found; in normaloperation, they are all collected at the end of output after a .RB ` \-\- ' parameter has been generated. Note that this.RB ` \-\- 'parameter is still generated, but it will always be the last parameter inthis mode..SH COMPATIBILITYThis version of .BR getopt (1)is written to be as compatible as possible to other versions. Usually you can just replace them with this versionwithout any modifications, and with some advantages.If the first character of the first parameter of getopt is not a .RB ` \- ', getopt goes into compatibility mode. It will interpret its first parameter asthe string of short options, and all other arguments will be parsed. Itwill still do parameter shuffling (ie. all non\-option parameters are outputedat the end), unless the environment variable .B POSIXLY_CORRECT is set.The environment variable .B GETOPT_COMPATIBLE forces .B getoptinto compatibility mode. Setting both this environment variable and.B POSIXLY_CORRECToffers 100% compatibility for `difficult' programs. Usually, though,neither is needed.In compatibility mode, leading .RB ` \- 'and .RB ` + 'characters in the short options string are ignored..SH RETURN CODES.B getoptreturns error code .B 0 for succesful parsing, .B 1if.BR getopt (3)returns errors,.B 2 if it does not understand its own parameters,.B 3if an internal error occurs like out\-of\-memory, and.B 4if it is called with .BR \-T ..SH EXAMPLESExample scripts for (ba)sh and (t)csh are provided with the.BR getopt (1)distribution, and are optionally installed in .B /usr/local/lib/getopt or .BR /usr/lib/getopt ..SH ENVIRONMENT.IP POSIXLY_CORRECTThis environment variable is examined by the.BR getopt (3)routines.If it is set, parsing stops as soon as a parameteris found that is not an option or an option argument. All remaining parameters are also interpreted as non\-option parameters, regardlesswhether they start with a .RB ` \- '..IP GETOPT_COMPATIBLEForces.B getoptto use the first calling format as specified in the.BR SYNOPSIS ..SH BUGS.BR getopt (3)can parse long options with optional arguments that are given an empty optionalargument (but can not do this for short options). This .BR getopt (1)treats optional arguments that are empty as if they were not present.The syntax if you do not want any short option variables at all isnot very intuitive (you have to set them explicitely to the emptystring)..SH AUTHORFrodo Looijaard <frodol@dds.nl>.SH "SEE ALSO".BR getopt (3),.BR bash (1),.BR tcsh (1).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -