summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2003-09-22 23:47:26 +0000
committermillert <>2003-09-22 23:47:26 +0000
commitadcc29a58e1b4c60196e548e543281b24b076c98 (patch)
tree2e658f55e3b77900e56dd64d3d0a17f3b611d966
parentfb596a0bc3c3e40564f75af219aaf48a059a9b32 (diff)
downloadopenbsd-adcc29a58e1b4c60196e548e543281b24b076c98.tar.gz
openbsd-adcc29a58e1b4c60196e548e543281b24b076c98.tar.bz2
openbsd-adcc29a58e1b4c60196e548e543281b24b076c98.zip
Document the optional argument support and as well as where we differ from
POSIX and/or GNU. deraadt@ and jmc@ OK
-rw-r--r--src/lib/libc/stdlib/getopt.3115
1 files changed, 83 insertions, 32 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
index 50b756bfbb..d210852c6b 100644
--- a/src/lib/libc/stdlib/getopt.3
+++ b/src/lib/libc/stdlib/getopt.3
@@ -25,7 +25,7 @@
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE. 26.\" SUCH DAMAGE.
27.\" 27.\"
28.\" $OpenBSD: getopt.3,v 1.27 2003/09/18 09:29:27 jmc Exp $ 28.\" $OpenBSD: getopt.3,v 1.28 2003/09/22 23:47:26 millert Exp $
29.\" 29.\"
30.Dd December 17, 2002 30.Dd December 17, 2002
31.Dt GETOPT 3 31.Dt GETOPT 3
@@ -57,9 +57,13 @@ if it has been specified in the string of accepted option characters,
57.Pp 57.Pp
58The option string 58The option string
59.Fa optstring 59.Fa optstring
60may contain the following elements: individual characters and 60may contain the following elements: individual characters,
61characters followed by a colon to indicate an option argument 61characters followed by a colon, and characters followed by two colons.
62is to follow. 62A character followed by a single colon indicates that an argument
63is to follow the option on the command line.
64Two colons indicates that the argument is optional--this is an
65extension not covered by
66.Px .
63For example, an option string 67For example, an option string
64.Qq x 68.Qq x
65recognizes an option 69recognizes an option
@@ -147,6 +151,15 @@ is set to the character that caused the error.
147The 151The
148.Fn getopt 152.Fn getopt
149function returns \-1 when the argument list is exhausted. 153function returns \-1 when the argument list is exhausted.
154.Sh ENVIRONMENT
155.Bl -tag -width POSIXLY_CORRECTXX
156.It Ev POSIXLY_CORRECT
157If set, a leading
158.Sq -
159in
160.Ar optstring
161is ignored.
162.El
150.Sh EXAMPLES 163.Sh EXAMPLES
151.Bd -literal -compact 164.Bd -literal -compact
152extern char *optarg; 165extern char *optarg;
@@ -204,31 +217,38 @@ this is reasonable but reduces the amount of error checking possible.
204.Xr getsubopt 3 217.Xr getsubopt 3
205.Sh STANDARDS 218.Sh STANDARDS
206The 219The
220.Fn getopt
221function implements a superset of the functionality specified by
222.St -p1003.1 .
223.Pp
224The following extensions are supported:
225.Bl -tag -width "xxx"
226.It Li o
227The
207.Va optreset 228.Va optreset
208variable was added to make it possible to call the 229variable was added to make it possible to call the
209.Fn getopt 230.Fn getopt
210function multiple times. 231function multiple times.
211This is an extension to the 232.It Li o
212.St -p1003.2 233If the first character of
213specification. 234.Fa optstring
214.Sh HISTORY 235is a plus sign
215The 236.Pq Ql + ,
216.Fn getopt 237it will be ignored.
217function appeared in 238This is for compatibility with
218.Bx 4.3 . 239.Tn GNU
219.Sh BUGS 240.Fn getopt .
220The 241.It Li o
221.Fn getopt 242If the first character of
222function was once specified to return 243.Fa optstring
223.Dv EOF 244is a dash
224instead of \-1. 245.Pq Ql - ,
225This was changed by 246non-options will be returned as arguments to the option character
226.St -p1003.2-92 247.Ql \e1 .
227to decouple 248This is for compatibility with
228.Fn getopt 249.Tn GNU
229from 250.Fn getopt .
230.Aq Pa stdio.h . 251.It Li o
231.Pp
232A single dash 252A single dash
233.Pq Ql - 253.Pq Ql -
234may be specified as a character in 254may be specified as a character in
@@ -250,20 +270,51 @@ as the first character in
250.Fa optstring 270.Fa optstring
251to avoid a semantic conflict with 271to avoid a semantic conflict with
252.Tn GNU 272.Tn GNU
253.Fn getopt , 273.Fn getopt
254which assigns different meaning to an 274semantics (see above).
255.Fa optstring
256that begins with a
257.Ql - .
258By default, a single dash causes 275By default, a single dash causes
259.Fn getopt 276.Fn getopt
260to return \-1. 277to return \-1.
278.El
279.Pp
280Unlike
281.Tn GNU
282.Fn getopt ,
283.Ox
284does not permute the argument vector to allow non-options to be
285interspersed with options on the command line.
286Programs requiring this behavior should use
287.Xr getopt_long 3
288instead.
289Because of this (and unlike
290.Tn GNU ) ,
291the
292.Ox
293.Fn getopt
294supports optional arguments separated by whitespace.
295.Sh HISTORY
296The
297.Fn getopt
298function appeared in
299.Bx 4.3 .
300.Sh BUGS
301The
302.Fn getopt
303function was once specified to return
304.Dv EOF
305instead of \-1.
306This was changed by
307.St -p1003.2-92
308to decouple
309.Fn getopt
310from
311.Aq Pa stdio.h .
261.Pp 312.Pp
262It is also possible to handle digits as option letters. 313It is possible to handle digits as option letters.
263This allows 314This allows
264.Fn getopt 315.Fn getopt
265to be used with programs that expect a number 316to be used with programs that expect a number
266.Pq Dq Li \&-\&3 317.Pq Dq Li \-3
267as an option. 318as an option.
268This practice is wrong, and should not be used in any current development. 319This practice is wrong, and should not be used in any current development.
269It is provided for backward compatibility 320It is provided for backward compatibility