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