2004-02-24 08:07:26 +00:00
|
|
|
.\" $OpenBSD: getopt_long.3,v 1.10 2004/01/06 23:44:28 fgsch Exp $
|
|
|
|
.\" $NetBSD: getopt_long.3,v 1.14 2003/08/07 16:43:40 agc Exp $
|
2002-09-29 04:14:37 +00:00
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1988, 1991, 1993
|
|
|
|
.\" The Regents of the University of California. All rights reserved.
|
|
|
|
.\"
|
|
|
|
.\" Redistribution and use in source and binary forms, with or without
|
|
|
|
.\" modification, are permitted provided that the following conditions
|
|
|
|
.\" are met:
|
|
|
|
.\" 1. Redistributions of source code must retain the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer.
|
|
|
|
.\" 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
.\" notice, this list of conditions and the following disclaimer in the
|
|
|
|
.\" documentation and/or other materials provided with the distribution.
|
2004-02-24 08:07:26 +00:00
|
|
|
.\" 3. Neither the name of the University nor the names of its contributors
|
2002-09-29 04:14:37 +00:00
|
|
|
.\" may be used to endorse or promote products derived from this software
|
|
|
|
.\" without specific prior written permission.
|
|
|
|
.\"
|
|
|
|
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
.\" SUCH DAMAGE.
|
|
|
|
.\"
|
|
|
|
.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
|
2002-12-09 13:54:29 +00:00
|
|
|
.\" $FreeBSD$
|
2002-09-29 04:14:37 +00:00
|
|
|
.\"
|
2023-02-11 05:31:13 +00:00
|
|
|
.Dd December 24, 2022
|
2002-09-29 04:14:37 +00:00
|
|
|
.Dt GETOPT_LONG 3
|
|
|
|
.Os
|
|
|
|
.Sh NAME
|
2004-02-24 08:07:26 +00:00
|
|
|
.Nm getopt_long ,
|
|
|
|
.Nm getopt_long_only
|
2002-09-29 04:14:37 +00:00
|
|
|
.Nd get long options from command line argument list
|
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
|
|
|
.Sh SYNOPSIS
|
2002-12-09 13:54:29 +00:00
|
|
|
.In getopt.h
|
2004-02-23 05:07:11 +00:00
|
|
|
.Vt extern char *optarg ;
|
2004-02-24 08:07:26 +00:00
|
|
|
.Vt extern int optind ;
|
|
|
|
.Vt extern int optopt ;
|
|
|
|
.Vt extern int opterr ;
|
|
|
|
.Vt extern int optreset ;
|
2002-09-29 04:14:37 +00:00
|
|
|
.Ft int
|
2002-12-09 13:54:29 +00:00
|
|
|
.Fo getopt_long
|
|
|
|
.Fa "int argc" "char * const *argv" "const char *optstring"
|
2004-02-24 08:07:26 +00:00
|
|
|
.Fa "const struct option *longopts" "int *longindex"
|
|
|
|
.Fc
|
|
|
|
.Ft int
|
|
|
|
.Fo getopt_long_only
|
|
|
|
.Fa "int argc" "char * const *argv" "const char *optstring"
|
|
|
|
.Fa "const struct option *longopts" "int *longindex"
|
2002-12-09 13:54:29 +00:00
|
|
|
.Fc
|
2002-09-29 04:14:37 +00:00
|
|
|
.Sh DESCRIPTION
|
|
|
|
The
|
|
|
|
.Fn getopt_long
|
|
|
|
function is similar to
|
|
|
|
.Xr getopt 3
|
2002-12-09 13:54:29 +00:00
|
|
|
but it accepts options in two forms: words and characters.
|
|
|
|
The
|
2002-09-29 04:14:37 +00:00
|
|
|
.Fn getopt_long
|
2002-12-09 13:54:29 +00:00
|
|
|
function provides a superset of the functionality of
|
2002-09-29 04:14:37 +00:00
|
|
|
.Xr getopt 3 .
|
2002-12-18 12:45:11 +00:00
|
|
|
The
|
2002-09-29 04:14:37 +00:00
|
|
|
.Fn getopt_long
|
2002-12-18 12:45:11 +00:00
|
|
|
function
|
2002-12-09 13:54:29 +00:00
|
|
|
can be used in two ways.
|
|
|
|
In the first way, every long option understood
|
2002-09-29 04:14:37 +00:00
|
|
|
by the program has a corresponding short option, and the option
|
|
|
|
structure is only used to translate from long options to short
|
2002-12-09 13:54:29 +00:00
|
|
|
options.
|
|
|
|
When used in this fashion,
|
2002-09-29 04:14:37 +00:00
|
|
|
.Fn getopt_long
|
|
|
|
behaves identically to
|
|
|
|
.Xr getopt 3 .
|
|
|
|
This is a good way to add long option processing to an existing program
|
|
|
|
with the minimum of rewriting.
|
|
|
|
.Pp
|
|
|
|
In the second mechanism, a long option sets a flag in the
|
2002-12-09 13:54:29 +00:00
|
|
|
.Vt option
|
2002-09-29 04:14:37 +00:00
|
|
|
structure passed, or will store a pointer to the command line argument
|
|
|
|
in the
|
2002-12-09 13:54:29 +00:00
|
|
|
.Vt option
|
|
|
|
structure passed to it for options that take arguments.
|
|
|
|
Additionally,
|
2002-09-29 04:14:37 +00:00
|
|
|
the long option's argument may be specified as a single argument with
|
2002-12-09 13:54:29 +00:00
|
|
|
an equal sign, e.g.,
|
|
|
|
.Pp
|
|
|
|
.Dl "myprogram --myoption=somevalue"
|
2002-09-29 04:14:37 +00:00
|
|
|
.Pp
|
2002-12-09 13:54:29 +00:00
|
|
|
When a long option is processed, the call to
|
2002-09-29 04:14:37 +00:00
|
|
|
.Fn getopt_long
|
2002-12-09 13:54:29 +00:00
|
|
|
will return 0.
|
|
|
|
For this reason, long option processing without
|
2002-09-29 04:14:37 +00:00
|
|
|
shortcuts is not backwards compatible with
|
|
|
|
.Xr getopt 3 .
|
|
|
|
.Pp
|
|
|
|
It is possible to combine these methods, providing for long options
|
2002-12-09 13:54:29 +00:00
|
|
|
processing with short option equivalents for some options.
|
|
|
|
Less
|
2002-09-29 04:14:37 +00:00
|
|
|
frequently used options would be processed as long options only.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn getopt_long
|
|
|
|
call requires a structure to be initialized describing the long
|
2002-12-09 13:54:29 +00:00
|
|
|
options.
|
|
|
|
The structure is:
|
|
|
|
.Bd -literal -offset indent
|
2002-09-29 04:14:37 +00:00
|
|
|
struct option {
|
|
|
|
char *name;
|
|
|
|
int has_arg;
|
|
|
|
int *flag;
|
|
|
|
int val;
|
|
|
|
};
|
|
|
|
.Ed
|
|
|
|
.Pp
|
|
|
|
The
|
2002-12-09 13:54:29 +00:00
|
|
|
.Va name
|
2002-09-29 04:14:37 +00:00
|
|
|
field should contain the option name without the leading double dash.
|
|
|
|
.Pp
|
|
|
|
The
|
2002-12-09 13:54:29 +00:00
|
|
|
.Va has_arg
|
2002-09-29 04:14:37 +00:00
|
|
|
field should be one of:
|
2002-12-09 13:54:29 +00:00
|
|
|
.Pp
|
|
|
|
.Bl -tag -width ".Dv optional_argument" -offset indent -compact
|
|
|
|
.It Dv no_argument
|
2013-12-13 21:50:03 +00:00
|
|
|
no argument to the option is expected
|
2002-12-09 13:54:29 +00:00
|
|
|
.It Dv required_argument
|
|
|
|
an argument to the option is required
|
2005-01-14 21:07:56 +00:00
|
|
|
.It Dv optional_argument
|
2013-12-13 21:50:03 +00:00
|
|
|
an argument to the option may be presented
|
2002-09-29 04:14:37 +00:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
If
|
2002-12-09 13:54:29 +00:00
|
|
|
.Va flag
|
|
|
|
is not
|
|
|
|
.Dv NULL ,
|
|
|
|
then the integer pointed to by it will be set to the
|
2002-09-29 04:14:37 +00:00
|
|
|
value in the
|
2002-12-09 13:54:29 +00:00
|
|
|
.Va val
|
|
|
|
field.
|
|
|
|
If the
|
|
|
|
.Va flag
|
|
|
|
field is
|
|
|
|
.Dv NULL ,
|
|
|
|
then the
|
|
|
|
.Va val
|
|
|
|
field will be returned.
|
|
|
|
Setting
|
|
|
|
.Va flag
|
|
|
|
to
|
|
|
|
.Dv NULL
|
|
|
|
and setting
|
|
|
|
.Va val
|
2002-09-29 04:14:37 +00:00
|
|
|
to the corresponding short option will make this function act just
|
|
|
|
like
|
|
|
|
.Xr getopt 3 .
|
2004-02-24 08:07:26 +00:00
|
|
|
.Pp
|
|
|
|
If the
|
|
|
|
.Fa longindex
|
|
|
|
field is not
|
|
|
|
.Dv NULL ,
|
|
|
|
then the integer pointed to by it will be set to the index of the long
|
|
|
|
option relative to
|
|
|
|
.Fa longopts .
|
|
|
|
.Pp
|
|
|
|
The last element of the
|
|
|
|
.Fa longopts
|
|
|
|
array has to be filled with zeroes.
|
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn getopt_long_only
|
|
|
|
function behaves identically to
|
|
|
|
.Fn getopt_long
|
|
|
|
with the exception that long options may start with
|
|
|
|
.Ql -
|
|
|
|
in addition to
|
|
|
|
.Ql -- .
|
|
|
|
If an option starting with
|
|
|
|
.Ql -
|
|
|
|
does not match a long option but does match a single-character option,
|
|
|
|
the single-character option is returned.
|
|
|
|
.Sh RETURN VALUES
|
|
|
|
If the
|
|
|
|
.Fa flag
|
|
|
|
field in
|
2005-01-14 21:07:56 +00:00
|
|
|
.Vt "struct option"
|
2004-02-24 08:07:26 +00:00
|
|
|
is
|
|
|
|
.Dv NULL ,
|
|
|
|
.Fn getopt_long
|
|
|
|
and
|
|
|
|
.Fn getopt_long_only
|
|
|
|
return the value specified in the
|
|
|
|
.Fa val
|
|
|
|
field, which is usually just the corresponding short option.
|
|
|
|
If
|
|
|
|
.Fa flag
|
|
|
|
is not
|
|
|
|
.Dv NULL ,
|
|
|
|
these functions return 0 and store
|
|
|
|
.Fa val
|
|
|
|
in the location pointed to by
|
|
|
|
.Fa flag .
|
2018-08-12 00:08:14 +00:00
|
|
|
.Pp
|
2004-02-24 08:07:26 +00:00
|
|
|
These functions return
|
|
|
|
.Ql \&:
|
2018-08-12 00:08:14 +00:00
|
|
|
if there was a missing option argument and error messages are suppressed,
|
2004-02-24 08:07:26 +00:00
|
|
|
.Ql \&?
|
|
|
|
if the user specified an unknown or ambiguous option, and
|
|
|
|
\-1 when the argument list has been exhausted.
|
2018-08-12 00:08:14 +00:00
|
|
|
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 \&+
|
2020-05-19 11:05:27 +00:00
|
|
|
is to permute non-option arguments to the end of
|
2018-08-12 00:08:14 +00:00
|
|
|
.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').
|
2005-01-20 09:17:07 +00:00
|
|
|
.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
|
2002-09-29 04:14:37 +00:00
|
|
|
.Sh EXAMPLES
|
|
|
|
.Bd -literal -compact
|
|
|
|
int bflag, ch, fd;
|
|
|
|
int daggerset;
|
|
|
|
|
|
|
|
/* options descriptor */
|
|
|
|
static struct option longopts[] = {
|
2004-02-24 08:07:26 +00:00
|
|
|
{ "buffy", no_argument, NULL, 'b' },
|
|
|
|
{ "fluoride", required_argument, NULL, 'f' },
|
2002-09-29 04:14:37 +00:00
|
|
|
{ "daggerset", no_argument, \*[Am]daggerset, 1 },
|
2004-02-24 08:07:26 +00:00
|
|
|
{ NULL, 0, NULL, 0 }
|
2002-09-29 04:14:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bflag = 0;
|
2011-12-26 03:14:37 +00:00
|
|
|
while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {
|
2004-02-24 08:07:26 +00:00
|
|
|
switch (ch) {
|
2002-09-29 04:14:37 +00:00
|
|
|
case 'b':
|
|
|
|
bflag = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
2004-02-24 08:07:26 +00:00
|
|
|
if ((fd = open(optarg, O_RDONLY, 0)) == -1)
|
|
|
|
err(1, "unable to open %s", optarg);
|
2002-09-29 04:14:37 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
2004-02-24 08:07:26 +00:00
|
|
|
if (daggerset) {
|
2002-09-29 04:14:37 +00:00
|
|
|
fprintf(stderr,"Buffy will use her dagger to "
|
2004-02-24 08:07:26 +00:00
|
|
|
"apply fluoride to dracula's teeth\en");
|
2002-09-29 04:14:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2011-12-26 03:14:37 +00:00
|
|
|
}
|
2002-09-29 04:14:37 +00:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
.Ed
|
|
|
|
.Sh IMPLEMENTATION DIFFERENCES
|
2002-12-09 13:54:29 +00:00
|
|
|
This section describes differences to the
|
|
|
|
.Tn GNU
|
|
|
|
implementation
|
2002-09-29 04:14:37 +00:00
|
|
|
found in glibc-2.1.3:
|
2002-12-09 13:54:29 +00:00
|
|
|
.Bl -bullet
|
2004-03-01 10:12:45 +00:00
|
|
|
.\" .It
|
|
|
|
.\" Handling of
|
|
|
|
.\" .Ql -
|
|
|
|
.\" as first char of option string in presence of
|
|
|
|
.\" environment variable
|
|
|
|
.\" .Ev POSIXLY_CORRECT :
|
|
|
|
.\" .Bl -tag -width ".Bx"
|
|
|
|
.\" .It Tn GNU
|
|
|
|
.\" ignores
|
|
|
|
.\" .Ev POSIXLY_CORRECT
|
|
|
|
.\" and returns non-options as
|
|
|
|
.\" arguments to option '\e1'.
|
|
|
|
.\" .It Bx
|
|
|
|
.\" honors
|
|
|
|
.\" .Ev POSIXLY_CORRECT
|
|
|
|
.\" and stops at the first non-option.
|
|
|
|
.\" .El
|
2004-03-06 14:24:10 +00:00
|
|
|
.\" .It
|
|
|
|
.\" Handling of
|
|
|
|
.\" .Ql -
|
|
|
|
.\" within the option string (not the first character):
|
|
|
|
.\" .Bl -tag -width ".Bx"
|
|
|
|
.\" .It Tn GNU
|
|
|
|
.\" treats a
|
|
|
|
.\" .Ql -
|
|
|
|
.\" on the command line as a non-argument.
|
|
|
|
.\" .It Bx
|
|
|
|
.\" a
|
|
|
|
.\" .Ql -
|
|
|
|
.\" within the option string matches a
|
|
|
|
.\" .Ql -
|
|
|
|
.\" (single dash) on the command line.
|
|
|
|
.\" This functionality is provided for backward compatibility with
|
|
|
|
.\" programs, such as
|
|
|
|
.\" .Xr su 1 ,
|
|
|
|
.\" that use
|
|
|
|
.\" .Ql -
|
|
|
|
.\" as an option flag.
|
|
|
|
.\" This practice is wrong, and should not be used in any current development.
|
|
|
|
.\" .El
|
2004-03-01 17:57:05 +00:00
|
|
|
.\" .It
|
|
|
|
.\" Handling of
|
|
|
|
.\" .Ql ::
|
|
|
|
.\" in options string in presence of
|
|
|
|
.\" .Ev POSIXLY_CORRECT :
|
|
|
|
.\" .Bl -tag -width ".Bx"
|
|
|
|
.\" .It Both
|
|
|
|
.\" .Tn GNU
|
|
|
|
.\" and
|
|
|
|
.\" .Bx
|
|
|
|
.\" ignore
|
|
|
|
.\" .Ev POSIXLY_CORRECT
|
|
|
|
.\" here and take
|
|
|
|
.\" .Ql ::
|
|
|
|
.\" to
|
|
|
|
.\" mean the preceding option takes an optional argument.
|
|
|
|
.\" .El
|
2004-03-03 08:29:00 +00:00
|
|
|
.\" .It
|
|
|
|
.\" Return value in case of missing argument if first character
|
|
|
|
.\" (after
|
|
|
|
.\" .Ql +
|
|
|
|
.\" or
|
|
|
|
.\" .Ql - )
|
|
|
|
.\" in option string is not
|
|
|
|
.\" .Ql \&: :
|
|
|
|
.\" .Bl -tag -width ".Bx"
|
|
|
|
.\" .It Tn GNU
|
|
|
|
.\" returns
|
|
|
|
.\" .Ql \&?
|
|
|
|
.\" .It Bx
|
|
|
|
.\" returns
|
|
|
|
.\" .Ql \&:
|
|
|
|
.\" (since
|
|
|
|
.\" .Bx Ns 's
|
|
|
|
.\" .Fn getopt
|
|
|
|
.\" does).
|
|
|
|
.\" .El
|
2004-03-01 10:59:23 +00:00
|
|
|
.\" .It
|
|
|
|
.\" Handling of
|
|
|
|
.\" .Ql --a
|
|
|
|
.\" in getopt:
|
|
|
|
.\" .Bl -tag -width ".Bx"
|
|
|
|
.\" .It Tn GNU
|
|
|
|
.\" parses this as option
|
|
|
|
.\" .Ql - ,
|
|
|
|
.\" option
|
|
|
|
.\" .Ql a .
|
|
|
|
.\" .It Bx
|
|
|
|
.\" parses this as
|
|
|
|
.\" .Ql -- ,
|
|
|
|
.\" and returns \-1 (ignoring the
|
|
|
|
.\" .Ql a ) .
|
|
|
|
.\" (Because the original
|
|
|
|
.\" .Fn getopt
|
|
|
|
.\" does.)
|
|
|
|
.\" .El
|
2002-12-09 13:54:29 +00:00
|
|
|
.It
|
|
|
|
Setting of
|
|
|
|
.Va optopt
|
|
|
|
for long options with
|
|
|
|
.Va flag
|
|
|
|
!=
|
|
|
|
.Dv NULL :
|
2004-02-24 08:07:26 +00:00
|
|
|
.Bl -tag -width ".Bx"
|
2002-12-09 13:54:29 +00:00
|
|
|
.It Tn GNU
|
|
|
|
sets
|
|
|
|
.Va optopt
|
|
|
|
to
|
|
|
|
.Va val .
|
2004-02-24 08:07:26 +00:00
|
|
|
.It Bx
|
2002-12-09 13:54:29 +00:00
|
|
|
sets
|
|
|
|
.Va optopt
|
|
|
|
to 0 (since
|
|
|
|
.Va val
|
|
|
|
would never be returned).
|
2002-09-29 04:14:37 +00:00
|
|
|
.El
|
2004-03-01 10:59:23 +00:00
|
|
|
.\" .It
|
|
|
|
.\" Handling of
|
|
|
|
.\" .Ql -W
|
|
|
|
.\" with
|
|
|
|
.\" .Ql W;
|
|
|
|
.\" in option string in
|
|
|
|
.\" .Fn getopt
|
|
|
|
.\" (not
|
|
|
|
.\" .Fn getopt_long ) :
|
|
|
|
.\" .Bl -tag -width ".Bx"
|
|
|
|
.\" .It Tn GNU
|
|
|
|
.\" causes a segfault.
|
|
|
|
.\" .It Bx
|
|
|
|
.\" no special handling is done;
|
|
|
|
.\" .Ql W;
|
|
|
|
.\" is interpreted as two separate options, neither of which take an argument.
|
|
|
|
.\" .El
|
2002-12-09 13:54:29 +00:00
|
|
|
.It
|
|
|
|
Setting of
|
|
|
|
.Va optarg
|
|
|
|
for long options without an argument that are
|
|
|
|
invoked via
|
|
|
|
.Ql -W
|
2004-02-24 08:07:26 +00:00
|
|
|
.Ql ( W;
|
2002-12-09 13:54:29 +00:00
|
|
|
in option string):
|
2004-02-24 08:07:26 +00:00
|
|
|
.Bl -tag -width ".Bx"
|
2002-12-09 13:54:29 +00:00
|
|
|
.It Tn GNU
|
|
|
|
sets
|
|
|
|
.Va optarg
|
|
|
|
to the option name (the argument of
|
|
|
|
.Ql -W ) .
|
2004-02-24 08:07:26 +00:00
|
|
|
.It Bx
|
2002-12-09 13:54:29 +00:00
|
|
|
sets
|
|
|
|
.Va optarg
|
|
|
|
to
|
|
|
|
.Dv NULL
|
|
|
|
(the argument of the long option).
|
2002-09-29 04:14:37 +00:00
|
|
|
.El
|
2002-12-09 13:54:29 +00:00
|
|
|
.It
|
|
|
|
Handling of
|
|
|
|
.Ql -W
|
|
|
|
with an argument that is not (a prefix to) a known
|
|
|
|
long option
|
2004-03-06 14:47:49 +00:00
|
|
|
.Ql ( W;
|
2002-12-09 13:54:29 +00:00
|
|
|
in option string):
|
2004-02-24 08:07:26 +00:00
|
|
|
.Bl -tag -width ".Bx"
|
2002-12-09 13:54:29 +00:00
|
|
|
.It Tn GNU
|
|
|
|
returns
|
|
|
|
.Ql -W
|
|
|
|
with
|
|
|
|
.Va optarg
|
|
|
|
set to the unknown option.
|
2004-02-24 08:07:26 +00:00
|
|
|
.It Bx
|
2002-12-09 13:54:29 +00:00
|
|
|
treats this as an error (unknown option) and returns
|
|
|
|
.Ql \&?
|
|
|
|
with
|
|
|
|
.Va optopt
|
|
|
|
set to 0 and
|
|
|
|
.Va optarg
|
|
|
|
set to
|
|
|
|
.Dv NULL
|
|
|
|
(as
|
|
|
|
.Tn GNU Ns 's
|
|
|
|
man page documents).
|
2002-09-29 04:14:37 +00:00
|
|
|
.El
|
2004-03-01 17:57:05 +00:00
|
|
|
.\" .It
|
|
|
|
.\" The error messages are different.
|
2002-12-09 13:54:29 +00:00
|
|
|
.It
|
2004-02-24 08:07:26 +00:00
|
|
|
.Bx
|
2002-12-09 13:54:29 +00:00
|
|
|
does not permute the argument vector at the same points in
|
|
|
|
the calling sequence as
|
|
|
|
.Tn GNU
|
|
|
|
does.
|
|
|
|
The aspects normally used by
|
|
|
|
the caller (ordering after \-1 is returned, value of
|
|
|
|
.Va optind
|
|
|
|
relative
|
|
|
|
to current positions) are the same, though.
|
|
|
|
(We do fewer variable swaps.)
|
2002-09-29 04:14:37 +00:00
|
|
|
.El
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr getopt 3
|
|
|
|
.Sh HISTORY
|
|
|
|
The
|
|
|
|
.Fn getopt_long
|
2004-02-24 08:07:26 +00:00
|
|
|
and
|
|
|
|
.Fn getopt_long_only
|
2010-08-16 15:18:30 +00:00
|
|
|
functions first appeared in the
|
2002-12-09 13:54:29 +00:00
|
|
|
.Tn GNU
|
2010-08-16 15:18:30 +00:00
|
|
|
libiberty library.
|
2002-12-09 13:54:29 +00:00
|
|
|
The first
|
2004-02-24 08:07:26 +00:00
|
|
|
.Bx
|
|
|
|
implementation of
|
|
|
|
.Fn getopt_long
|
|
|
|
appeared in
|
|
|
|
.Nx 1.5 ,
|
|
|
|
the first
|
|
|
|
.Bx
|
|
|
|
implementation of
|
|
|
|
.Fn getopt_long_only
|
|
|
|
in
|
|
|
|
.Ox 3.3 .
|
|
|
|
.Fx
|
|
|
|
first included
|
|
|
|
.Fn getopt_long
|
|
|
|
in
|
|
|
|
.Fx 5.0 ,
|
|
|
|
.Fn getopt_long_only
|
|
|
|
in
|
|
|
|
.Fx 5.2 .
|
2002-09-29 04:14:37 +00:00
|
|
|
.Sh BUGS
|
2004-02-24 08:07:26 +00:00
|
|
|
The
|
2005-01-14 21:07:56 +00:00
|
|
|
.Fa argv
|
2004-02-24 08:07:26 +00:00
|
|
|
argument is not really
|
2005-01-14 21:07:56 +00:00
|
|
|
.Vt const
|
2004-02-24 08:07:26 +00:00
|
|
|
as its elements may be permuted (unless
|
|
|
|
.Ev POSIXLY_CORRECT
|
|
|
|
is set).
|
|
|
|
.Pp
|
2002-12-09 13:54:29 +00:00
|
|
|
The implementation can completely replace
|
2002-09-29 04:14:37 +00:00
|
|
|
.Xr getopt 3 ,
|
|
|
|
but right now we are using separate code.
|
2023-02-11 05:31:13 +00:00
|
|
|
.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.
|