summaryrefslogtreecommitdiffstats
path: root/man1p/getopts.1p
blob: 8c96c5730a7784572c3a229fc52ec5d4ffd12150 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved 
.TH "GETOPTS" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\" getopts 
.SH NAME
getopts \- parse utility options
.SH SYNOPSIS
.LP
\fBgetopts\fP \fIoptstring name\fP \fB[\fP\fIarg\fP\fB...\fP\fB]\fP
.SH DESCRIPTION
.LP
The \fIgetopts\fP utility shall retrieve options and option-arguments
from a list of parameters. It shall support the Utility
Syntax Guidelines 3 to 10, inclusive, described in the Base Definitions
volume of IEEE\ Std\ 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
.LP
Each time it is invoked, the \fIgetopts\fP utility shall place the
value of the next option in the shell variable specified by
the \fIname\fP operand and the index of the next argument to be processed
in the shell variable \fIOPTIND .\fP Whenever the shell
is invoked, \fIOPTIND\fP shall be initialized to 1.
.LP
When the option requires an option-argument, the \fIgetopts\fP utility
shall place it in the shell variable \fIOPTARG .\fP If
no option was found, or if the option that was found does not have
an option-argument, \fIOPTARG\fP shall be unset.
.LP
If an option character not contained in the \fIoptstring\fP operand
is found where an option character is expected, the shell
variable specified by \fIname\fP shall be set to the question-mark
( \fB'?'\fP ) character. In this case, if the first
character in \fIoptstring\fP is a colon ( \fB':'\fP ), the shell variable
\fIOPTARG\fP shall be set to the option character
found, but no output shall be written to standard error; otherwise,
the shell variable \fIOPTARG\fP shall be unset and a
diagnostic message shall be written to standard error. This condition
shall be considered to be an error detected in the way
arguments were presented to the invoking application, but shall not
be an error in \fIgetopts\fP processing.
.LP
If an option-argument is missing:
.IP " *" 3
If the first character of \fIoptstring\fP is a colon, the shell variable
specified by \fIname\fP shall be set to the colon
character and the shell variable \fIOPTARG\fP shall be set to the
option character found.
.LP
.IP " *" 3
Otherwise, the shell variable specified by \fIname\fP shall be set
to the question-mark character, the shell variable
\fIOPTARG\fP shall be unset, and a diagnostic message shall be written
to standard error. This condition shall be considered to be
an error detected in the way arguments were presented to the invoking
application, but shall not be an error in \fIgetopts\fP
processing; a diagnostic message shall be written as stated, but the
exit status shall be zero.
.LP
.LP
When the end of options is encountered, the \fIgetopts\fP utility
shall exit with a return value greater than zero; the shell
variable \fIOPTIND\fP shall be set to the index of the first non-option-argument,
where the first \fB"--"\fP argument is
considered to be an option-argument if there are no other non-option-arguments
appearing before it, or the value \fB"$#"\fP +1
if there are no non-option-arguments; the \fIname\fP variable shall
be set to the question-mark character. Any of the following
shall identify the end of options: the special option \fB"--"\fP ,
finding an argument that does not begin with a \fB'-'\fP ,
or encountering an error.
.LP
The shell variables \fIOPTIND\fP and \fIOPTARG\fP shall be local to
the caller of \fIgetopts\fP and shall not be exported by
default.
.LP
The shell variable specified by the \fIname\fP operand, \fIOPTIND
,\fP and \fIOPTARG\fP shall affect the current shell
execution environment; see \fIShell Execution Environment\fP .
.LP
If the application sets \fIOPTIND\fP to the value 1, a new set of
parameters can be used: either the current positional
parameters or new \fIarg\fP values. Any other attempt to invoke \fIgetopts\fP
multiple times in a single shell execution
environment with parameters (positional parameters or \fIarg\fP operands)
that are not the same in all invocations, or with an
\fIOPTIND\fP value modified to be a value other than 1, produces unspecified
results.
.SH OPTIONS
.LP
None.
.SH OPERANDS
.LP
The following operands shall be supported:
.TP 7
\fIoptstring\fP
A string containing the option characters recognized by the utility
invoking \fIgetopts\fP. If a character is followed by a
colon, the option shall be expected to have an argument, which should
be supplied as a separate argument. Applications should
specify an option character and its option-argument as separate arguments,
but \fIgetopts\fP shall interpret the characters
following an option character requiring arguments as an argument whether
or not this is done. An explicit null option-argument need
not be recognized if it is not supplied as a separate argument when
\fIgetopts\fP is invoked. (See also the \fIgetopt\fP() function defined
in the System Interfaces volume of IEEE\ Std\ 1003.1-2001.)
The characters question-mark and colon shall not be used as option
characters by an application. The use of other option characters
that are not alphanumeric produces unspecified results. If the option-argument
is not supplied as a separate argument from the
option character, the value in \fIOPTARG\fP shall be stripped of the
option character and the \fB'-'\fP . The first character
in \fIoptstring\fP determines how \fIgetopts\fP behaves if an option
character is not known or an option-argument is
missing.
.TP 7
\fIname\fP
The name of a shell variable that shall be set by the \fIgetopts\fP
utility to the option character that was found.
.sp
.LP
The \fIgetopts\fP utility by default shall parse positional parameters
passed to the invoking shell procedure. If \fIarg\fPs
are given, they shall be parsed instead of the positional parameters.
.SH STDIN
.LP
Not used.
.SH INPUT FILES
.LP
None.
.SH ENVIRONMENT VARIABLES
.LP
The following environment variables shall affect the execution of
\fIgetopts\fP:
.TP 7
\fILANG\fP
Provide a default value for the internationalization variables that
are unset or null. (See the Base Definitions volume of
IEEE\ Std\ 1003.1-2001, Section 8.2, Internationalization Variables
for
the precedence of internationalization variables used to determine
the values of locale categories.)
.TP 7
\fILC_ALL\fP
If set to a non-empty string value, override the values of all the
other internationalization variables.
.TP 7
\fILC_CTYPE\fP
Determine the locale for the interpretation of sequences of bytes
of text data as characters (for example, single-byte as
opposed to multi-byte characters in arguments and input files).
.TP 7
\fILC_MESSAGES\fP
Determine the locale that should be used to affect the format and
contents of diagnostic messages written to standard
error.
.TP 7
\fINLSPATH\fP
Determine the location of message catalogs for the processing of \fILC_MESSAGES
\&.\fP 
.TP 7
\fIOPTIND\fP
This variable shall be used by the \fIgetopts\fP utility as the index
of the next argument to be processed.
.sp
.SH ASYNCHRONOUS EVENTS
.LP
Default.
.SH STDOUT
.LP
Not used.
.SH STDERR
.LP
Whenever an error is detected and the first character in the \fIoptstring\fP
operand is not a colon ( \fB':'\fP ), a
diagnostic message shall be written to standard error with the following
information in an unspecified format:
.IP " *" 3
The invoking program name shall be identified in the message. The
invoking program name shall be the value of the shell special
parameter 0 (see \fISpecial Parameters\fP ) at the time the \fIgetopts\fP
utility is
invoked. A name equivalent to:
.sp
.RS
.nf

\fBbasename "$0"
\fP
.fi
.RE
.LP
may be used.
.LP
.IP " *" 3
If an option is found that was not specified in \fIoptstring\fP, this
error is identified and the invalid option character
shall be identified in the message.
.LP
.IP " *" 3
If an option requiring an option-argument is found, but an option-argument
is not found, this error shall be identified and the
invalid option character shall be identified in the message.
.LP
.SH OUTPUT FILES
.LP
None.
.SH EXTENDED DESCRIPTION
.LP
None.
.SH EXIT STATUS
.LP
The following exit values shall be returned:
.TP 7
\ 0
An option, specified or unspecified by \fIoptstring\fP, was found.
.TP 7
>0
The end of options was encountered or an error occurred.
.sp
.SH CONSEQUENCES OF ERRORS
.LP
Default.
.LP
\fIThe following sections are informative.\fP
.SH APPLICATION USAGE
.LP
Since \fIgetopts\fP affects the current shell execution environment,
it is generally provided as a shell regular built-in. If
it is called in a subshell or separate utility execution environment,
such as one of the following:
.sp
.RS
.nf

\fB(getopts abc value "$@")
nohup getopts ...
find . -exec getopts ... \\;
\fP
.fi
.RE
.LP
it does not affect the shell variables in the caller's environment.
.LP
Note that shell functions share \fIOPTIND\fP with the calling shell
even though the positional parameters are changed. If the
calling shell and any of its functions uses \fIgetopts\fP to parse
arguments, the results are unspecified.
.SH EXAMPLES
.LP
The following example script parses and displays its arguments:
.sp
.RS
.nf

\fBaflag=
bflag=
while getopts ab: name
do
    case $name in
    a)    aflag=1;;
    b)    bflag=1
          bval="$OPTARG";;
    ?)   printf "Usage: %s: [-a] [-b value] args\\n" $0
          exit 2;;
    esac
done
if [ ! -z "$aflag" ]; then
    printf "Option -a specified\\n"
fi
if [ ! -z "$bflag" ]; then
    printf 'Option -b "%s" specified\\n' "$bval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\\n" "$*"
\fP
.fi
.RE
.SH RATIONALE
.LP
The \fIgetopts\fP utility was chosen in preference to the System V
\fIgetopt\fP utility because \fIgetopts\fP handles
option-arguments containing <blank>s.
.LP
The \fIOPTARG\fP variable is not mentioned in the ENVIRONMENT VARIABLES
section because it does not affect the execution of
\fIgetopts\fP; it is one of the few "output-only" variables used by
the standard utilities.
.LP
The colon is not allowed as an option character because that is not
historical behavior, and it violates the Utility Syntax
Guidelines. The colon is now specified to behave as in the KornShell
version of the \fIgetopts\fP utility; when used as the first
character in the \fIoptstring\fP operand, it disables diagnostics
concerning missing option-arguments and unexpected option
characters. This replaces the use of the \fIOPTERR\fP variable that
was specified in an early proposal.
.LP
The formats of the diagnostic messages produced by the \fIgetopts\fP
utility and the \fIgetopt\fP() function are not fully specified because
implementations with superior
(``friendlier") formats objected to the formats used by some historical
implementations. The standard developers considered it
important that the information in the messages used be uniform between
\fIgetopts\fP and \fIgetopt\fP(). Exact duplication of the messages
might not be possible, particularly if a utility
is built on another system that has a different \fIgetopt\fP() function,
but the messages
must have specific information included so that the program name,
invalid option character, and type of error can be distinguished
by a user.
.LP
Only a rare application program intercepts a \fIgetopts\fP standard
error message and wants to parse it. Therefore,
implementations are free to choose the most usable messages they can
devise. The following formats are used by many historical
implementations:
.sp
.RS
.nf

\fB"%s: illegal option -- %c\\n", <\fP\fIprogram name\fP\fB>, <\fP\fIoption character\fP\fB>
.sp

"%s: option requires an argument -- %c\\n", <\fP\fIprogram name\fP\fB>, \\
    <\fP\fIoption character\fP\fB>
\fP
.fi
.RE
.LP
Historical shells with built-in versions of \fIgetopt\fP() or \fIgetopts\fP
have used
different formats, frequently not even indicating the option character
found in error.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fISpecial Parameters\fP , the System Interfaces volume of
IEEE\ Std\ 1003.1-2001, \fIgetopt\fP()
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .