/[public]/getopt/trunk/getopt.1
ViewVC logotype

Annotation of /getopt/trunk/getopt.1

Parent Directory Parent Directory | Revision Log Revision Log


Revision 365 - (hide annotations)
Thu Nov 20 20:15:01 2014 UTC (9 years, 4 months ago) by frodo
File size: 14132 byte(s)
(Frodo) Sync man page with util-linux (git 20141120).
There are some slight differences, mostly because it is not a part of
util-linux by default.

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

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26