4cad91b2549c108c9e880249212c5ec76ecc56ac
[gnulib.git] / doc / posix-functions / getopt.texi
1 @node getopt
2 @section @code{getopt}
3 @findex getopt
4
5 POSIX specification:@* @url{http://www.opengroup.org/onlinepubs/9699919799/functions/getopt.html}
6
7 Gnulib module: getopt-posix or getopt-gnu
8
9 The module @code{getopt-gnu} has support for ``long options'' and for
10 ``options that take optional arguments''.  Compared to the API defined by POSIX,
11 it adds a header file @code{<getopt.h>} and a function @code{getopt_long}.
12
13 Portability problems fixed by either Gnulib module @code{getopt-posix} or @code{getopt-gnu}:
14 @itemize
15 @item
16 The @code{getopt} function keeps some internal state that cannot be explicitly
17 reset on some platforms:
18 mingw.
19 @item
20 The value of @code{optind} after a missing required argument is wrong
21 on some platforms:
22 Mac OS X 10.5, AIX 7.1.
23 @end itemize
24
25 Portability problems fixed by Gnulib module @code{getopt-gnu}:
26 @itemize
27 @item
28 This function is missing on some platforms:
29 MSVC 9.
30 @item
31 The function @code{getopt} does not support the @samp{+} flag in the options
32 string on some platforms:
33 Mac OS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 11 2010-11.
34 @item
35 The function @code{getopt} does not obey the combination of @samp{+}
36 and @samp{:} flags in the options string on some platforms:
37 glibc 2.11.
38 @item
39 The function @code{getopt} does not obey the @samp{-} flag in the options
40 string when @env{POSIXLY_CORRECT} is set on some platforms:
41 Cygwin 1.7.0.
42 @item
43 The function @code{getopt} does not support options with optional arguments
44 on some platforms:
45 Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11, IRIX 6.5, OSF/1 5.1,
46 Solaris 11 2010-11, Cygwin 1.5.x.
47 @item
48 The function @code{getopt_long} is missing on some platforms:
49 AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 9, MSVC 9, Interix 3.5.
50 @item
51 The function @code{getopt_long} does not support abbreviated long options
52 where all disambiguations are equivalent on some platforms:
53 OpenBSD 5.0.
54 @item
55 The function @code{getopt_long_only} is missing on some platforms:
56 Mac OS X 10.3, FreeBSD 5.2.1, NetBSD 5.0, AIX 5.1, HP-UX 11, IRIX 6.5,
57 OSF/1 5.1, Solaris 9, mingw, MSVC 9, Interix 3.5.
58 @item
59 This function crashes if the option string includes @code{W;} on some
60 platforms:
61 glibc 2.14.
62 @end itemize
63
64 Portability problems not fixed by Gnulib:
65 @itemize
66 @item
67 The default behavior of the glibc implementation of @code{getopt} allows
68 mixing option and non-option arguments on the command line in any order.
69 Other implementations, such as the one in Cygwin, enforce strict POSIX
70 compliance: they require that the option arguments precede the non-option
71 arguments.  This is something to watch out in your program's
72 testsuite.
73 @item
74 The glibc implementation allows a complete reset of the environment,
75 including re-checking for @env{POSIXLY_CORRECT}, by setting
76 @code{optind} to 0.  Several BSD implementations provide @code{optreset},
77 causing a reset by setting it non-zero, although it does not
78 necessarily re-read @env{POSIXLY_CORRECT}.  Solaris @code{getopt} does
79 not support either reset method, but does not maintain state that
80 needs the extra level of reset.
81 @item
82 On some platforms, this function does not set the stream error
83 indicator on attempts to write to a read-only stream:
84 glibc 2.13, Cygwin 1.7.9.
85 @end itemize