]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/getopt.1.adoc
flock: initialize timevals [-Werror=maybe-uninitialized]
[thirdparty/util-linux.git] / misc-utils / getopt.1.adoc
CommitLineData
295b3979 1//po4a: entry man manual
6c64d12d
MB
2= getopt(1)
3:doctype: manpage
4:man manual: User Commands
5:man source: util-linux {release-version}
6:page-layout: base
7:command: getopt
2c646c80 8:plus: +
6c64d12d
MB
9
10== NAME
11
12getopt - parse command options (enhanced)
13
14== SYNOPSIS
15
16*getopt* _optstring_ _parameters_
55bcc409 17
6c64d12d 18*getopt* [options] [*--*] _optstring_ _parameters_
55bcc409 19
6c64d12d
MB
20*getopt* [options] *-o*|*--options* _optstring_ [options] [*--*] _parameters_
21
22== DESCRIPTION
23
24*getopt* is used to break up (_parse_) options in command lines for easy parsing by shell procedures, and to check for valid options. It uses the GNU *getopt*(3) routines to do this.
25
26The parameters *getopt* is called with can be divided into two parts: options which modify the way *getopt* will do the parsing (the _options_ and the _optstring_ in the *SYNOPSIS*), and the parameters which are to be parsed (_parameters_ in the *SYNOPSIS*). The second part will start at the first non-option parameter that is not an option argument, or after the first occurrence of '*--*'. If no '*-o*' or '*--options*' option is found in the first part, the first parameter of the second part is used as the short options string.
27
c83a52f0 28If the environment variable *GETOPT_COMPATIBLE* is set, or if the first _parameter_ is not an option (does not start with a '*-*', the first format in the *SYNOPSIS*), *getopt* will generate output that is compatible with that of other versions of *getopt*(1). It will still do parameter shuffling and recognize optional arguments (see the *COMPATIBILITY* section for more information).
6c64d12d
MB
29
30Traditional implementations of *getopt*(1) are unable to cope with whitespace and other (shell-specific) special characters in arguments and non-option parameters. To solve this problem, this implementation can generate quoted output which must once again be interpreted by the shell (usually by using the *eval* command). This has the effect of preserving those characters, but you must call *getopt* in a way that is no longer compatible with other versions (the second or third format in the *SYNOPSIS*). To determine whether this enhanced version of *getopt*(1) is installed, a special test option (*-T*) can be used.
31
32== OPTIONS
33
34*-a*, *--alternative*::
2c646c80 35Allow long options to start with a single '*-*'.
6c64d12d 36
6c64d12d 37*-l*, *--longoptions* _longopts_::
2c646c80 38The long (multi-character) options to be recognized. More than one option name may be specified at once, by separating the names with commas. This option may be given more than once, the _longopts_ are cumulative. Each long option name in _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.
6c64d12d
MB
39
40*-n*, *--name* _progname_::
2c646c80 41The name that will be used by the *getopt*(3) routines when it reports errors. Note that errors of *getopt*(1) are still reported as coming from getopt.
6c64d12d
MB
42
43*-o*, *--options* _shortopts_::
c83a52f0 44The short (one-character) options to be recognized. If this option is not found, the first parameter of *getopt* that does not start with a '*-*' (and is not an option argument) is used as the short options string. Each short option character in _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 '*{plus}*' or '*-*' to influence the way options are parsed and output is generated (see the *SCANNING MODES* section for details).
2c646c80 45//TRANSLATORS: Keep {plus} untranslated.
6c64d12d
MB
46
47*-q*, *--quiet*::
2c646c80 48Disable error reporting by *getopt*(3).
6c64d12d
MB
49
50*-Q*, *--quiet-output*::
2c646c80 51Do not generate normal output. Errors are still reported by *getopt*(3), unless you also use *-q*.
6c64d12d
MB
52
53*-s*, *--shell* _shell_::
bd67ca44 54Set quoting conventions to those of _shell_. If the *-s* option is not given, the *BASH* conventions are used. Valid arguments are currently '*sh*', '*bash*', '*csh*', and '*tcsh*'.
6c64d12d
MB
55
56*-T*, *--test*::
2c646c80 57Test if your *getopt*(1) is this enhanced version or an old version. This generates no output, and sets the error status to 4. Other implementations of *getopt*(1), and this version if the environment variable *GETOPT_COMPATIBLE* is set, will return '*--*' and error status 0.
6c64d12d
MB
58
59*-u*, *--unquoted*::
2c646c80 60Do not quote the output. Note that whitespace and special (shell-dependent) characters can cause havoc in this mode (like they do with other *getopt*(1) implementations).
6c64d12d 61
2b2d3172 62include::man-common/help-version.adoc[]
6c64d12d
MB
63
64== PARSING
65
66This section specifies the format of the second part of the parameters of *getopt* (the _parameters_ in the *SYNOPSIS*). The next section (*OUTPUT*) describes the output that is generated. These parameters were typically the parameters a shell function was called with. Care must be taken that each parameter the shell function was called with corresponds to exactly one parameter in the parameter list of *getopt* (see the *EXAMPLES*). All parsing is done by the GNU *getopt*(3) routines.
67
68The parameters are parsed from left to right. Each parameter is classified as a short option, a long option, an argument to an option, or a non-option parameter.
69
70A simple short option is a '*-*' followed by a short option character. If the option has a required argument, it may be written directly after the option character or as the next parameter (i.e., separated by whitespace on the command line). If the option has an optional argument, it must be written directly after the option character if present.
71
72It is possible to specify several short options after one '*-*', as long as all (except possibly the last) do not have required or optional arguments.
73
74A long option normally begins with '*--*' followed by the long option name. If the option has a required argument, it may be written directly after the long option name, separated by '*=*', or as the next argument (i.e., separated by whitespace on the command line). If the option has an optional argument, it must be written directly after the long option name, separated by '*=*', if present (if you add the '*=*' but nothing behind it, it is interpreted as if no argument was present; this is a slight bug, see the *BUGS*). Long options may be abbreviated, as long as the abbreviation is not ambiguous.
75
2c646c80
MB
76Each parameter not starting with a '*-*', and not a required argument of a previous option, is a non-option parameter. Each parameter after a '*--*' parameter is always interpreted as a non-option parameter. If the environment variable *POSIXLY_CORRECT* is set, or if the short option string started with a '*{plus}*', all remaining parameters are interpreted as non-option parameters as soon as the first non-option parameter is found.
77//TRANSLATORS: Keep {plus} untranslated.
6c64d12d
MB
78
79== OUTPUT
80
81Output is generated for each element described in the previous section. Output is done in the same order as the elements are specified in the input, except for non-option parameters. Output can be done in _compatible_ (_unquoted_) mode, or in such way that whitespace and other special characters within arguments and non-option parameters are preserved (see *QUOTING*). When the output is processed in the shell script, it will seem to be composed of distinct elements that can be processed one by one (by using the shift command in most shell languages). This is imperfect in unquoted mode, as elements can be split at unexpected places if they contain whitespace or special characters.
82
83If there are problems parsing the parameters, for example because a required argument is not found or an option is not recognized, an error will be reported on stderr, there will be no output for the offending element, and a non-zero error status is returned.
84
85For a short option, a single '*-*' and the option character are generated as one parameter. If the option has an argument, the next parameter will be the argument. If the option takes an optional argument, but none was found, the next parameter will be generated but be empty in quoting mode, but no second parameter will be generated in unquoted (compatible) mode. Note that many other *getopt*(1) implementations do not support optional arguments.
86
87If several short options were specified after a single '*-*', each will be present in the output as a separate parameter.
88
89For a long option, '*--*' and the full option name are generated as one parameter. This is done regardless whether the option was abbreviated or specified with a single '*-*' in the input. Arguments are handled as with short options.
90
2c646c80
MB
91Normally, no non-option parameters output is generated until all options and their arguments have been generated. Then '*--*' is generated as a single parameter, and after it the non-option parameters in the order they were found, each as a separate parameter. Only if the first character of the short options string was a '*-*', 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 *SYNOPSIS* is used; in that case all preceding occurrences of '*-*' and '*{plus}*' are ignored).
92//TRANSLATORS: Keep {plus} untranslated.
6c64d12d
MB
93
94== QUOTING
95
96In compatibility mode, whitespace or 'special' characters in arguments or non-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 this problem, this implementation offers quoting. The idea is that output is generated with quotes around each parameter. When this output is once again fed to the shell (usually by a shell *eval* command), it is split correctly into separate parameters.
97
98Quoting is not enabled if the environment variable *GETOPT_COMPATIBLE* is set, if the first form of the *SYNOPSIS* is used, or if the option '*-u*' is found.
99
100Different shells use different quoting conventions. You can use the '*-s*' option to select the shell you are using. The following shells are currently supported: '*sh*', '*bash*', '*csh*' and '*tcsh*'. Actually, only two 'flavors' are distinguished: sh-like quoting conventions and csh-like quoting conventions. Chances are that if you use another shell script language, one of these flavors can still be used.
101
102== SCANNING MODES
103
2c646c80
MB
104The first character of the short options string may be a '*-*' or a '*{plus}*' to indicate a special scanning mode. If the first calling form in the *SYNOPSIS* is used they are ignored; the environment variable *POSIXLY_CORRECT* is still examined, though.
105//TRANSLATORS: Keep {plus} untranslated.
6c64d12d 106
2c646c80
MB
107If the first character is '*{plus}*', or if the environment variable *POSIXLY_CORRECT* is set, parsing stops as soon as the first non-option parameter (i.e., a parameter that does not start with a '*-*') is found that is not an option argument. The remaining parameters are all interpreted as non-option parameters.
108//TRANSLATORS: Keep {plus} untranslated.
6c64d12d
MB
109
110If the first character is a '*-*', non-option parameters are outputted at the place where they are found; in normal operation, they are all collected at the end of output after a '*--*' parameter has been generated. Note that this '*--*' parameter is still generated, but it will always be the last parameter in this mode.
111
112== COMPATIBILITY
113
114This version of *getopt*(1) is written to be as compatible as possible to other versions. Usually you can just replace them with this version without any modifications, and with some advantages.
115
2c646c80
MB
116If the first character of the first parameter of getopt is not a '*-*', *getopt* goes into compatibility mode. It will interpret its first parameter as the string of short options, and all other arguments will be parsed. It will still do parameter shuffling (i.e., all non-option parameters are output at the end), unless the environment variable *POSIXLY_CORRECT* is set, in which case, *getopt* will prepend a '*{plus}*' before short options automatically.
117//TRANSLATORS: Keep {plus} untranslated.
6c64d12d
MB
118
119The environment variable *GETOPT_COMPATIBLE* forces *getopt* into compatibility mode. Setting both this environment variable and *POSIXLY_CORRECT* offers 100% compatibility for 'difficult' programs. Usually, though, neither is needed.
120
2c646c80
MB
121In compatibility mode, leading '*-*' and '*{plus}*' characters in the short options string are ignored.
122//TRANSLATORS: Keep {plus} untranslated.
6c64d12d
MB
123
124== RETURN CODES
125
126*getopt* returns error code *0* for successful parsing, *1* if *getopt*(3) returns errors, *2* if it does not understand its own parameters, *3* if an internal error occurs like out-of-memory, and *4* if it is called with *-T*.
127
128== EXAMPLES
129
14ae6d5a
KZ
130// TRANSLATORS: Don't translate _{package-docdir}_.
131Example scripts for (ba)sh and (t)csh are provided with the *getopt*(1) distribution, and are installed in _{package-docdir}_ directory.
6c64d12d
MB
132
133== ENVIRONMENT
134
135*POSIXLY_CORRECT*::
2c646c80 136This environment variable is examined by the *getopt*(3) routines. If it is set, parsing stops as soon as a parameter is found that is not an option or an option argument. All remaining parameters are also interpreted as non-option parameters, regardless whether they start with a '*-*'.
6c64d12d
MB
137
138*GETOPT_COMPATIBLE*::
2c646c80 139Forces *getopt* to use the first calling format as specified in the *SYNOPSIS*.
6c64d12d
MB
140
141== BUGS
142
143*getopt*(3) can parse long options with optional arguments that are given an empty optional argument (but cannot do this for short options). This *getopt*(1) treats optional arguments that are empty as if they were not present.
144
145The syntax if you do not want any short option variables at all is not very intuitive (you have to set them explicitly to the empty string).
146
147== AUTHOR
148
149mailto:frodo@frodo.looijaard.name[Frodo Looijaard]
150
151== SEE ALSO
152
153*bash*(1),
154*tcsh*(1),
155*getopt*(3)
156
625e9c61 157include::man-common/bugreports.adoc[]
6c64d12d 158
625e9c61 159include::man-common/footer.adoc[]
6c64d12d
MB
160
161ifdef::translation[]
625e9c61 162include::man-common/translation.adoc[]
6c64d12d 163endif::[]