]> git.ipfire.org Git - thirdparty/util-linux.git/blame - getopt/getopt.1
mount: use loop= option when mounting by /sbin/mount.<type>
[thirdparty/util-linux.git] / getopt / getopt.1
CommitLineData
2b6fc908
KZ
1.TH GETOPT 1 "May 31, 1997" Linux ""
2.SH NAME
3getopt \- parse command options (enhanced)
4.SH SYNOPSIS
5.BR getopt " optstring parameters"
6
df1dddf9 7.BR getopt " [options] [" \-\- "] optstring parameters"
2b6fc908 8
df1dddf9 9.BR getopt " [options] " \-o | \-\-options " optstring [options] [" \-\- "] parameters"
2b6fc908
KZ
10.SH DESCRIPTION
11.B getopt
12is used to break up
13.RI ( parse )
14options in command lines for easy parsing by
15shell procedures, and to check for legal options.
16It uses the
17.SM GNU
18.BR getopt (3)
19routines to do this.
20
21The parameters
22.B getopt
23is called with can be divided into two parts: options
24which modify the way getopt will parse
25.RI ( options
26and
df1dddf9 27.I \-o|\-\-options optstring
2b6fc908
KZ
28in the
29.BR SYNOPSIS),
30and the parameters which are to be
31parsed
32.RI ( parameters
33in the
34.BR SYNOPSIS).
df1dddf9 35The second part will start at the first non\-option parameter
b22550fa 36that is not an option argument, or after the first occurrence of
df1dddf9 37.RB ` \-\- '.
2b6fc908 38If no
df1dddf9 39.RB ` \-o '
2b6fc908 40or
df1dddf9 41.RB ` \-\-options '
2b6fc908
KZ
42option is found in the first part, the first
43parameter of the second part is used as the short options string.
44
45If the environment variable
46.B GETOPT_COMPATIBLE
47is set, or if its first parameter
48is not an option (does not start with a
df1dddf9 49.RB ` \- ',
2b6fc908
KZ
50this is the first format in the
51.BR SYNOPSIS),
52.B getopt
53will generate output that is compatible with that of other versions of
54.BR getopt (1).
55It will still do parameter shuffling and recognize optional
56arguments (see section
57.B COMPATIBILITY
58for more information).
59
60Traditional implementations of
61.BR getopt (1)
df1dddf9
KZ
62are unable to cope with whitespace and other (shell\-specific) special characters
63in arguments and non\-option parameters. To solve this problem, this
2b6fc908
KZ
64implementation can generate
65quoted output which must once again be interpreted by the shell (usually
66by using the
67.B eval
68command). This has the effect of preserving those characters, but
69you must call
70.B getopt
71in a way that is no longer compatible with other versions (the second
72or third format in the
73.BR SYNOPSIS).
74To determine whether this enhanced version of
75.BR getopt (1)
76is installed, a special test option
df1dddf9 77.RB ( \-T )
2b6fc908
KZ
78can be used.
79.SH OPTIONS
df1dddf9 80.IP "\-a, \-\-alternative"
2b6fc908 81Allow long options to start with a single
df1dddf9
KZ
82.RB ` \- '.
83.IP "\-h, \-\-help"
2b6fc908 84Output a small usage guide and exit succesfully. No other output is generated.
df1dddf9
KZ
85.IP "\-l, \-\-longoptions longopts"
86The long (multi\-character) options to be recognized.
2b6fc908
KZ
87More than one option name
88may be specified at once, by separating the names with commas. This option
89may be given more than once, the
90.I longopts
ffc43748 91are cumulative.
2b6fc908
KZ
92Each long option name
93in
94.I longopts
95may be followed by one colon to indicate it has a required argument,and by two colons to indicate it has an optional argument.
df1dddf9 96.IP "\-n, \-\-name progname"
2b6fc908
KZ
97The name that will be used by the
98.BR getopt (3)
99routines when it reports errors. Note that errors of
100.BR getopt (1)
101are still reported as coming from getopt.
df1dddf9
KZ
102.IP "\-o, \-\-options shortopts"
103The short (one\-character) options to be recognized. If this option is not
2b6fc908
KZ
104found, the first parameter of
105.B getopt
106that does not start with
107a
df1dddf9 108.RB ` \- '
2b6fc908
KZ
109(and is not an option argument) is used as the short options string.
110Each short option character
111in
112.I shortopts
113may be followed by one colon to indicate it has a required argument,
114and by two colons to indicate it has an optional argument.
115The first character of shortopts may be
116.RB ` + '
117or
df1dddf9 118.RB ` \- '
2b6fc908
KZ
119to influence the way
120options are parsed and output is generated (see section
121.B SCANNING MODES
122for details).
df1dddf9 123.IP "\-q, \-\-quiet"
2b6fc908 124Disable error reporting by getopt(3).
df1dddf9 125.IP "\-Q, \-\-quiet\-output"
2b6fc908
KZ
126Do not generate normal output. Errors are still reported by
127.BR getopt (3),
128unless you also use
df1dddf9
KZ
129.IR \-q .
130.IP "\-s, \-\-shell shell"
131Set quoting conventions to those of shell. If no \-s argument is found,
2b6fc908
KZ
132the
133.SM BASH
134conventions are used. Valid arguments are currently
135.RB ` sh '
136.RB ` bash ',
137.RB ` csh ',
138and
139.RB ` tcsh '.
df1dddf9
KZ
140.IP "\-u, \-\-unquoted"
141Do not quote the output. Note that whitespace and special (shell\-dependent)
2b6fc908
KZ
142characters can cause havoc in this mode (like they do with other
143.BR getopt (1)
144implementations).
df1dddf9 145.IP "\-T \-\-test"
2b6fc908
KZ
146Test if your
147.BR getopt (1)
148is this enhanced version or an old version. This generates no output,
149and sets the error status to 4. Other implementations of
150.BR getopt (1),
151and this version if the environment variable
152.B GETOPT_COMPATIBLE
153is set,
154will return
df1dddf9 155.RB ` \-\- '
2b6fc908 156and error status 0.
df1dddf9 157.IP "\-V, \-\-version"
2b6fc908
KZ
158Output version information and exit succesfully. No other output is generated.
159.SH PARSING
160This section specifies the format of the second part of the parameters of
161.B getopt
162(the
163.I parameters
164in the
165.BR SYNOPSIS ).
166The next section
167.RB ( OUTPUT )
168describes the output that is
169generated. These parameters were typically the parameters a shell function
170was called with.
171Care must be taken that each parameter the shell function was
172called with corresponds to exactly one parameter in the parameter list of
173.B getopt
174(see the
175.BR EXAMPLES ).
176All parsing is done by the GNU
177.BR getopt (3)
178routines.
179
180The parameters are parsed from left to right. Each parameter is classified as a
181short option, a long option, an argument to an option,
df1dddf9 182or a non\-option parameter.
2b6fc908
KZ
183
184A simple short option is a
df1dddf9 185.RB ` \- '
2b6fc908
KZ
186followed by a short option character. If
187the option has a required argument, it may be written directly after the option
188character or as the next parameter (ie. separated by whitespace on the
189command line). If the
190option has an optional argument, it must be written directly after the
191option character if present.
192
193It is possible to specify several short options after one
df1dddf9 194.RB ` \- ',
2b6fc908
KZ
195as long as all (except possibly the last) do not have required or optional
196arguments.
197
198A long option normally begins with
df1dddf9 199.RB ` \-\- '
2b6fc908
KZ
200followed by the long option name.
201If the option has a required argument, it may be written directly after
202the long option name, separated by
203.RB ` = ',
204or as the next argument (ie. separated by whitespace on the command line).
205If the option has an optional argument, it must
206be written directly after the long option name, separated by
207.RB ` = ',
208if present (if you add the
209.RB ` = '
210but nothing behind it, it is interpreted
211as if no argument was present; this is a slight bug, see the
212.BR BUGS ).
213Long options may be abbreviated, as long as the abbreviation is not
214ambiguous.
215
216Each parameter not starting with a
df1dddf9 217.RB ` \- ',
2b6fc908 218and not a required argument of
df1dddf9 219a previous option, is a non\-option parameter. Each parameter after
2b6fc908 220a
df1dddf9
KZ
221.RB ` \-\- '
222parameter is always interpreted as a non\-option parameter.
2b6fc908
KZ
223If the environment variable
224.B POSIXLY_CORRECT
225is set, or if the short
226option string started with a
227.RB ` + ',
228all remaining parameters are interpreted
df1dddf9 229as non\-option parameters as soon as the first non\-option parameter is
2b6fc908
KZ
230found.
231.SH OUTPUT
232Output is generated for each element described in the previous section.
233Output is done
234in the same order as the elements are specified in the input, except
df1dddf9 235for non\-option parameters. Output can be done in
2b6fc908
KZ
236.I compatible
237.RI ( unquoted )
238mode, or in such way that whitespace and other special characters within
df1dddf9 239arguments and non\-option parameters are preserved (see
2b6fc908
KZ
240.BR QUOTING ).
241When the output is processed in the shell script, it will seem to be
242composed of distinct elements that can be processed one by one (by using the
243shift command in most shell languages). This is imperfect in unquoted mode,
244as elements can be split at unexpected places if they contain whitespace
245or special characters.
246
247If there are problems parsing the parameters, for example because a
248required argument is not found or an option is not recognized, an error
249will be reported on stderr, there will be no output for the offending
df1dddf9 250element, and a non\-zero error status is returned.
2b6fc908
KZ
251
252For a short option, a single
df1dddf9 253.RB ` \- '
2b6fc908
KZ
254and the option character are generated
255as one parameter. If the option has an argument, the next
256parameter will be the argument. If the option takes an optional argument,
257but none was found, the next parameter will be generated but be empty in
258quoting mode,
259but no second parameter will be generated in unquoted (compatible) mode.
260Note that many other
261.BR getopt (1)
262implemetations do not support optional arguments.
263
264If several short options were specified after a single
df1dddf9 265.RB ` \- ',
2b6fc908
KZ
266each will be present in the output as a separate parameter.
267
268For a long option,
df1dddf9 269.RB ` \-\- '
2b6fc908
KZ
270and the full option name are generated as one
271parameter. This is done regardless whether the option was abbreviated or
272specified with a single
df1dddf9 273.RB ` \- '
2b6fc908
KZ
274in the input. Arguments are handled as with short options.
275
df1dddf9 276Normally, no non\-option parameters output is generated until all options
2b6fc908 277and their arguments have been generated. Then
df1dddf9 278.RB ` \-\- '
2b6fc908 279is generated as a
df1dddf9 280single parameter, and after it the non\-option parameters in the order
2b6fc908
KZ
281they were found, each as a separate parameter.
282Only if the first character of the short options string was a
df1dddf9
KZ
283.RB ` \- ',
284non\-option parameter output is generated at the place they are found in the
2b6fc908
KZ
285input (this is not supported if the first format of the
286.B SYNOPSIS
b22550fa 287is used; in that case all preceding occurrences of
df1dddf9 288.RB ` \- '
2b6fc908
KZ
289and
290.RB ` + '
291are ignored).
292.SH QUOTING
293In compatible mode, whitespace or 'special' characters in arguments or
df1dddf9 294non\-option parameters are not handled correctly. As the output is
2b6fc908
KZ
295fed to the shell script, the script does not know how it is supposed to break
296the output into separate parameters. To circumvent this
297problem, this implementation offers quoting. The idea is that output
298is generated with quotes around each parameter. When this output is once
299again fed to the shell (usually by a shell
300.B eval
301command), it is split correctly into separate parameters.
302
303Quoting is not enabled if the environment variable
304.B GETOPT_COMPATIBLE
305is set, if the first form of the
306.B SYNOPSIS
307is used, or if the option
df1dddf9 308.RB ` \-u '
2b6fc908
KZ
309is found.
310
311Different shells use different quoting conventions. You can use the
df1dddf9 312.RB ` \-s '
2b6fc908
KZ
313option to select the shell you are using. The following shells are
314currently supported:
315.RB ` sh ',
316.RB ` bash ',
317.RB ` csh '
318and
319.RB ` tcsh '.
df1dddf9
KZ
320Actually, only two `flavors' are distinguished: sh\-like quoting conventions
321and csh\-like quoting conventions. Chances are that if you use another shell
2b6fc908
KZ
322script language, one of these flavors can still be used.
323
324.SH "SCANNING MODES"
325The first character of the short options string may be a
df1dddf9 326.RB ` \- '
2b6fc908
KZ
327or a
328.RB ` + '
329to indicate a special scanning mode. If the first calling form
330in the
331.B SYNOPSIS
332is used they are ignored; the environment variable
333.B POSIXLY_CORRECT
334is still examined, though.
335
336If the first character is
337.RB ` + ',
338or if the environment variable
339.B POSIXLY_CORRECT
df1dddf9 340is set, parsing stops as soon as the first non\-option parameter
2b6fc908 341(ie. a parameter that does not start with a
df1dddf9 342.RB ` \- ')
2b6fc908
KZ
343is found that
344is not an option argument. The remaining parameters are all interpreted as
df1dddf9 345non\-option parameters.
2b6fc908
KZ
346
347If the first character is a
df1dddf9
KZ
348.RB ` \- ',
349non\-option parameters are outputed at the place where they are found; in normal
2b6fc908 350operation, they are all collected at the end of output after a
df1dddf9 351.RB ` \-\- '
2b6fc908 352parameter has been generated. Note that this
df1dddf9 353.RB ` \-\- '
2b6fc908
KZ
354parameter is still generated, but it will always be the last parameter in
355this mode.
356.SH COMPATIBILITY
357This version of
358.BR getopt (1)
359is written to be as compatible as possible to
360other versions. Usually you can just replace them with this version
361without any modifications, and with some advantages.
362
363If the first character of the first parameter of getopt is not a
df1dddf9 364.RB ` \- ',
2b6fc908
KZ
365getopt goes into compatibility mode. It will interpret its first parameter as
366the string of short options, and all other arguments will be parsed. It
df1dddf9 367will still do parameter shuffling (ie. all non\-option parameters are outputed
2b6fc908
KZ
368at the end), unless the environment variable
369.B POSIXLY_CORRECT
370is set.
371
372The environment variable
373.B GETOPT_COMPATIBLE
374forces
375.B getopt
376into compatibility mode. Setting both this environment variable and
377.B POSIXLY_CORRECT
378offers 100% compatibility for `difficult' programs. Usually, though,
379neither is needed.
380
381In compatibility mode, leading
df1dddf9 382.RB ` \- '
2b6fc908
KZ
383and
384.RB ` + '
385characters in the short options string are ignored.
386.SH RETURN CODES
387.B getopt
388returns error code
389.B 0
390for succesful parsing,
391.B 1
392if
393.BR getopt (3)
394returns errors,
395.B 2
396if it does not understand its own parameters,
397.B 3
df1dddf9 398if an internal error occurs like out\-of\-memory, and
2b6fc908
KZ
399.B 4
400if it is called with
df1dddf9 401.BR \-T .
2b6fc908
KZ
402.SH EXAMPLES
403Example scripts for (ba)sh and (t)csh are provided with the
404.BR getopt (1)
405distribution, and are optionally installed in
406.B /usr/local/lib/getopt
407or
408.BR /usr/lib/getopt .
409.SH ENVIRONMENT
410.IP POSIXLY_CORRECT
411This environment variable is examined by the
412.BR getopt (3)
413routines.
414If it is set, parsing stops as soon as a parameter
415is found that is not an option or an option argument. All remaining
df1dddf9 416parameters are also interpreted as non\-option parameters, regardless
2b6fc908 417whether they start with a
df1dddf9 418.RB ` \- '.
2b6fc908
KZ
419.IP GETOPT_COMPATIBLE
420Forces
421.B getopt
422to use the first calling format as specified in the
423.BR SYNOPSIS .
424.SH BUGS
425.BR getopt (3)
426can parse long options with optional arguments that are given an empty optional
427argument (but can not do this for short options). This
428.BR getopt (1)
429treats optional arguments that are empty as if they were not present.
66ee8158
KZ
430
431The syntax if you do not want any short option variables at all is
432not very intuitive (you have to set them explicitely to the empty
433string).
434
2b6fc908 435.SH AUTHOR
cf6d7fae 436Frodo Looijaard <frodo@frodo.looijaard.name>
2b6fc908
KZ
437.SH "SEE ALSO"
438.BR getopt (3),
439.BR bash (1),
440.BR tcsh (1).
441