Loading...
--- Libc/Libc-583/stdlib/FreeBSD/getopt_long.3
+++ Libc/Libc-1583.60.2/stdlib/FreeBSD/getopt_long.3
@@ -29,9 +29,9 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
-.\" $FreeBSD: src/lib/libc/stdlib/getopt_long.3,v 1.11 2004/03/06 14:47:49 ache Exp $
+.\" $FreeBSD$
.\"
-.Dd April 1, 2000
+.Dd December 24, 2022
.Dt GETOPT_LONG 3
.Os
.Sh NAME
@@ -130,11 +130,11 @@
.Pp
.Bl -tag -width ".Dv optional_argument" -offset indent -compact
.It Dv no_argument
-no argument to the option is expect
+no argument to the option is expected
.It Dv required_argument
an argument to the option is required
-.It Li optional_argument
-an argument to the option may be presented.
+.It Dv optional_argument
+an argument to the option may be presented
.El
.Pp
If
@@ -190,7 +190,7 @@
If the
.Fa flag
field in
-.Li struct option
+.Vt "struct option"
is
.Dv NULL ,
.Fn getopt_long
@@ -207,12 +207,65 @@
.Fa val
in the location pointed to by
.Fa flag .
+.Pp
These functions return
.Ql \&:
-if there was a missing option argument,
+if there was a missing option argument and error messages are suppressed,
.Ql \&?
if the user specified an unknown or ambiguous option, and
\-1 when the argument list has been exhausted.
+The default behavior when a missing option argument is encountered is to write
+an error and return
+.Ql \&? .
+Specifying
+.Ql \&:
+in
+.Fa optstr
+will cause the error message to be suppressed and
+.Ql \&:
+to be returned instead.
+.Pp
+In addition to
+.Ql \&: ,
+a leading
+.Ql \&+
+or
+.Ql \&-
+in
+.Fa optstr
+also has special meaning.
+If either of these are specified, they must appear before
+.Ql \&: .
+.Pp
+A leading
+.Ql \&+
+indicates that processing should be halted at the first non-option argument,
+matching the default behavior of
+.Xr getopt 3 .
+The default behavior without
+.Ql \&+
+is to permute non-option arguments to the end of
+.Fa argv .
+.Pp
+A leading
+.Ql \&-
+indicates that all non-option arguments should be treated as if they are
+arguments to a literal
+.Ql \&1
+flag (i.e., the function call will return the value 1, rather than the char
+literal '1').
+.Sh ENVIRONMENT
+.Bl -tag -width ".Ev POSIXLY_CORRECT"
+.It Ev POSIXLY_CORRECT
+If set, option processing stops when the first non-option is found and
+a leading
+.Ql -
+or
+.Ql +
+in the
+.Fa optstring
+is ignored.
+.El
.Sh EXAMPLES
.Bd -literal -compact
int bflag, ch, fd;
@@ -227,7 +280,7 @@
};
bflag = 0;
-while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
+while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
switch (ch) {
case 'b':
bflag = 1;
@@ -244,6 +297,7 @@
break;
default:
usage();
+ }
}
argc -= optind;
argv += optind;
@@ -450,18 +504,6 @@
to current positions) are the same, though.
(We do fewer variable swaps.)
.El
-.Sh ENVIRONMENT
-.Bl -tag -width POSIXLY_CORRECT
-.It Ev POSIXLY_CORRECT
-If set, option processing stops when the first non-option is found and
-a leading
-.Ql -
-or
-.Ql +
-in the
-.Ar optstring
-is ignored.
-.El
.Sh SEE ALSO
.Xr getopt 3
.Sh HISTORY
@@ -469,9 +511,9 @@
.Fn getopt_long
and
.Fn getopt_long_only
-functions first appeared in
+functions first appeared in the
.Tn GNU
-libiberty.
+libiberty library.
The first
.Bx
implementation of
@@ -494,9 +536,9 @@
.Fx 5.2 .
.Sh BUGS
The
-.Ar argv
+.Fa argv
argument is not really
-.Dv const
+.Vt const
as its elements may be permuted (unless
.Ev POSIXLY_CORRECT
is set).
@@ -504,3 +546,19 @@
The implementation can completely replace
.Xr getopt 3 ,
but right now we are using separate code.
+.Pp
+.Nm
+makes the assumption that the first argument should always be skipped because
+it's typically the program name.
+As a result, setting
+.Va optind
+to 0 will indicate that
+.Nm
+should reset, and
+.Va optind
+will be set to 1 in the process.
+This behavior differs from
+.Xr getopt 3 ,
+which will handle an
+.Va optind
+value of 0 as expected and process the first element.