getopt
¶POSIX specification:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/getopt.html
LSB specification:
https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/libutil-getopt-3.html
Gnulib module: getopt-posix or getopt-gnu
The module getopt-gnu
has support for “long options” and for
“options that take optional arguments”. Compared to the API defined by POSIX,
it adds a header file <getopt.h>
and a function getopt_long
.
Portability problems fixed by either Gnulib module getopt-posix
or getopt-gnu
:
optind
after a missing required argument is wrong
on some platforms:
macOS 14, AIX 7.1, mingw.
Portability problems fixed by Gnulib module getopt-gnu
:
getopt
does not support the ‘+’ flag in the options
string on some platforms:
macOS 14, AIX 5.2, HP-UX 11, Solaris 11 2010-11.
getopt
does not obey the combination of ‘+’
and ‘:’ flags in the options string on some platforms:
glibc 2.11.
getopt
does not obey the ‘-’ flag in the options
string when POSIXLY_CORRECT
is set on some platforms:
Cygwin 1.7.0.
getopt
does not support options with optional arguments
on some platforms:
macOS 14, OpenBSD 4.0, AIX 5.2, HP-UX 11, Solaris 11 2010-11, Cygwin 1.5.x.
getopt_long
is missing on some platforms:
AIX 5.1, HP-UX 11, Solaris 9, MSVC 14.
getopt_long
does not support abbreviated long options
where all disambiguations are equivalent on some platforms:
OpenBSD 5.0.
getopt_long_only
is missing on some platforms:
FreeBSD 5.2.1, NetBSD 10.0, AIX 5.1, HP-UX 11, Solaris 9, mingw, MSVC 14.
W;
on some
platforms:
glibc 2.14.
Portability problems not fixed by Gnulib:
getopt
allows
mixing option and non-option arguments on the command line in any order.
Other implementations, such as the one in Cygwin, enforce strict POSIX
compliance: they require that the option arguments precede the non-option
arguments. This is something to watch out in your program’s
testsuite.
POSIXLY_CORRECT
, by setting
optind
to 0. Several BSD implementations provide optreset
,
causing a reset by setting it non-zero, although it does not
necessarily re-read POSIXLY_CORRECT
. Solaris getopt
does
not support either reset method, but does not maintain state that
needs the extra level of reset.