Blame manual/getopt.texi

Packit Service 82fcde
@node Getopt, Argp, , Parsing Program Arguments
Packit Service 82fcde
@section Parsing program options using @code{getopt}
Packit Service 82fcde
Packit Service 82fcde
The @code{getopt} and @code{getopt_long} functions automate some of the
Packit Service 82fcde
chore involved in parsing typical unix command line options.
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Using Getopt::                Using the @code{getopt} function.
Packit Service 82fcde
* Example of Getopt::           An example of parsing options with @code{getopt}.
Packit Service 82fcde
* Getopt Long Options::         GNU suggests utilities accept long-named
Packit Service 82fcde
                                 options; here is one way to do.
Packit Service 82fcde
* Getopt Long Option Example::  An example of using @code{getopt_long}.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Using Getopt, Example of Getopt, , Getopt
Packit Service 82fcde
@subsection Using the @code{getopt} function
Packit Service 82fcde
Packit Service 82fcde
Here are the details about how to call the @code{getopt} function.  To
Packit Service 82fcde
use this facility, your program must include the header file
Packit Service 82fcde
@file{unistd.h}.
Packit Service 82fcde
@pindex unistd.h
Packit Service 82fcde
Packit Service 82fcde
@deftypevar int opterr
Packit Service 82fcde
@standards{POSIX.2, unistd.h}
Packit Service 82fcde
If the value of this variable is nonzero, then @code{getopt} prints an
Packit Service 82fcde
error message to the standard error stream if it encounters an unknown
Packit Service 82fcde
option character or an option with a missing required argument.  This is
Packit Service 82fcde
the default behavior.  If you set this variable to zero, @code{getopt}
Packit Service 82fcde
does not print any messages, but it still returns the character @code{?}
Packit Service 82fcde
to indicate an error.
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
@deftypevar int optopt
Packit Service 82fcde
@standards{POSIX.2, unistd.h}
Packit Service 82fcde
When @code{getopt} encounters an unknown option character or an option
Packit Service 82fcde
with a missing required argument, it stores that option character in
Packit Service 82fcde
this variable.  You can use this for providing your own diagnostic
Packit Service 82fcde
messages.
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
@deftypevar int optind
Packit Service 82fcde
@standards{POSIX.2, unistd.h}
Packit Service 82fcde
This variable is set by @code{getopt} to the index of the next element
Packit Service 82fcde
of the @var{argv} array to be processed.  Once @code{getopt} has found
Packit Service 82fcde
all of the option arguments, you can use this variable to determine
Packit Service 82fcde
where the remaining non-option arguments begin.  The initial value of
Packit Service 82fcde
this variable is @code{1}.
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
@deftypevar {char *} optarg
Packit Service 82fcde
@standards{POSIX.2, unistd.h}
Packit Service 82fcde
This variable is set by @code{getopt} to point at the value of the
Packit Service 82fcde
option argument, for those options that accept arguments.
Packit Service 82fcde
@end deftypevar
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int getopt (int @var{argc}, char *const *@var{argv}, const char *@var{options})
Packit Service 82fcde
@standards{POSIX.2, unistd.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
Packit Service 82fcde
@c Swapping elements of passed-in argv may be partial in case of
Packit Service 82fcde
@c cancellation.  Gettext brings about a whole lot of AS and AC safety
Packit Service 82fcde
@c issues.  The getopt API involves returning values in the
Packit Service 82fcde
@c non-thread-specific optarg variable, which adds another thread-safety
Packit Service 82fcde
@c issue.  Given print_errors, it may output errors to stderr, which may
Packit Service 82fcde
@c self-deadlock, leak locks, or encounter (in a signal handler) or
Packit Service 82fcde
@c leave (in case of cancellation) stderr in an inconsistent state.
Packit Service 82fcde
@c Various implicit, indirect uses of malloc, in uses of memstream and
Packit Service 82fcde
@c asprintf for error-printing, bring about the usual malloc issues.
Packit Service 82fcde
@c
Packit Service 82fcde
@c _getopt_internal
Packit Service 82fcde
@c  _getopt_internal_r
Packit Service 82fcde
@c   gettext
Packit Service 82fcde
@c   _getopt_initialize
Packit Service 82fcde
@c    getenv
Packit Service 82fcde
@c   open_memstream
Packit Service 82fcde
@c   lockfile, unlockfile, __fxprintf -> stderr
Packit Service 82fcde
@c   asprintf
Packit Service 82fcde
The @code{getopt} function gets the next option argument from the
Packit Service 82fcde
argument list specified by the @var{argv} and @var{argc} arguments.
Packit Service 82fcde
Normally these values come directly from the arguments received by
Packit Service 82fcde
@code{main}.
Packit Service 82fcde
Packit Service 82fcde
The @var{options} argument is a string that specifies the option
Packit Service 82fcde
characters that are valid for this program.  An option character in this
Packit Service 82fcde
string can be followed by a colon (@samp{:}) to indicate that it takes a
Packit Service 82fcde
required argument.  If an option character is followed by two colons
Packit Service 82fcde
(@samp{::}), its argument is optional; this is a GNU extension.
Packit Service 82fcde
Packit Service 82fcde
@code{getopt} has three ways to deal with options that follow
Packit Service 82fcde
non-options @var{argv} elements.  The special argument @samp{--} forces
Packit Service 82fcde
in all cases the end of option scanning.
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
The default is to permute the contents of @var{argv} while scanning it
Packit Service 82fcde
so that eventually all the non-options are at the end.  This allows
Packit Service 82fcde
options to be given in any order, even with programs that were not
Packit Service 82fcde
written to expect this.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
If the @var{options} argument string begins with a hyphen (@samp{-}), this
Packit Service 82fcde
is treated specially.  It permits arguments that are not options to be
Packit Service 82fcde
returned as if they were associated with option character @samp{\1}.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
POSIX demands the following behavior: the first non-option stops option
Packit Service 82fcde
processing.  This mode is selected by either setting the environment
Packit Service 82fcde
variable @code{POSIXLY_CORRECT} or beginning the @var{options} argument
Packit Service 82fcde
string with a plus sign (@samp{+}).
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
The @code{getopt} function returns the option character for the next
Packit Service 82fcde
command line option.  When no more option arguments are available, it
Packit Service 82fcde
returns @code{-1}.  There may still be more non-option arguments; you
Packit Service 82fcde
must compare the external variable @code{optind} against the @var{argc}
Packit Service 82fcde
parameter to check this.
Packit Service 82fcde
Packit Service 82fcde
If the option has an argument, @code{getopt} returns the argument by
Packit Service 82fcde
storing it in the variable @var{optarg}.  You don't ordinarily need to
Packit Service 82fcde
copy the @code{optarg} string, since it is a pointer into the original
Packit Service 82fcde
@var{argv} array, not into a static area that might be overwritten.
Packit Service 82fcde
Packit Service 82fcde
If @code{getopt} finds an option character in @var{argv} that was not
Packit Service 82fcde
included in @var{options}, or a missing option argument, it returns
Packit Service 82fcde
@samp{?} and sets the external variable @code{optopt} to the actual
Packit Service 82fcde
option character.  If the first character of @var{options} is a colon
Packit Service 82fcde
(@samp{:}), then @code{getopt} returns @samp{:} instead of @samp{?} to
Packit Service 82fcde
indicate a missing option argument.  In addition, if the external
Packit Service 82fcde
variable @code{opterr} is nonzero (which is the default), @code{getopt}
Packit Service 82fcde
prints an error message.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Example of Getopt
Packit Service 82fcde
@subsection Example of Parsing Arguments with @code{getopt}
Packit Service 82fcde
Packit Service 82fcde
Here is an example showing how @code{getopt} is typically used.  The
Packit Service 82fcde
key points to notice are:
Packit Service 82fcde
Packit Service 82fcde
@itemize @bullet
Packit Service 82fcde
@item
Packit Service 82fcde
Normally, @code{getopt} is called in a loop.  When @code{getopt} returns
Packit Service 82fcde
@code{-1}, indicating no more options are present, the loop terminates.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A @code{switch} statement is used to dispatch on the return value from
Packit Service 82fcde
@code{getopt}.  In typical use, each case just sets a variable that
Packit Service 82fcde
is used later in the program.
Packit Service 82fcde
Packit Service 82fcde
@item
Packit Service 82fcde
A second loop is used to process the remaining non-option arguments.
Packit Service 82fcde
@end itemize
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@include testopt.c.texi
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Here are some examples showing what this program prints with different
Packit Service 82fcde
combinations of arguments:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
% testopt
Packit Service 82fcde
aflag = 0, bflag = 0, cvalue = (null)
Packit Service 82fcde
Packit Service 82fcde
% testopt -a -b
Packit Service 82fcde
aflag = 1, bflag = 1, cvalue = (null)
Packit Service 82fcde
Packit Service 82fcde
% testopt -ab
Packit Service 82fcde
aflag = 1, bflag = 1, cvalue = (null)
Packit Service 82fcde
Packit Service 82fcde
% testopt -c foo
Packit Service 82fcde
aflag = 0, bflag = 0, cvalue = foo
Packit Service 82fcde
Packit Service 82fcde
% testopt -cfoo
Packit Service 82fcde
aflag = 0, bflag = 0, cvalue = foo
Packit Service 82fcde
Packit Service 82fcde
% testopt arg1
Packit Service 82fcde
aflag = 0, bflag = 0, cvalue = (null)
Packit Service 82fcde
Non-option argument arg1
Packit Service 82fcde
Packit Service 82fcde
% testopt -a arg1
Packit Service 82fcde
aflag = 1, bflag = 0, cvalue = (null)
Packit Service 82fcde
Non-option argument arg1
Packit Service 82fcde
Packit Service 82fcde
% testopt -c foo arg1
Packit Service 82fcde
aflag = 0, bflag = 0, cvalue = foo
Packit Service 82fcde
Non-option argument arg1
Packit Service 82fcde
Packit Service 82fcde
% testopt -a -- -b
Packit Service 82fcde
aflag = 1, bflag = 0, cvalue = (null)
Packit Service 82fcde
Non-option argument -b
Packit Service 82fcde
Packit Service 82fcde
% testopt -a -
Packit Service 82fcde
aflag = 1, bflag = 0, cvalue = (null)
Packit Service 82fcde
Non-option argument -
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@node Getopt Long Options
Packit Service 82fcde
@subsection Parsing Long Options with @code{getopt_long}
Packit Service 82fcde
Packit Service 82fcde
To accept GNU-style long options as well as single-character options,
Packit Service 82fcde
use @code{getopt_long} instead of @code{getopt}.  This function is
Packit Service 82fcde
declared in @file{getopt.h}, not @file{unistd.h}.  You should make every
Packit Service 82fcde
program accept long options if it uses any options, for this takes
Packit Service 82fcde
little extra work and helps beginners remember how to use the program.
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} {struct option}
Packit Service 82fcde
@standards{GNU, getopt.h}
Packit Service 82fcde
This structure describes a single long option name for the sake of
Packit Service 82fcde
@code{getopt_long}.  The argument @var{longopts} must be an array of
Packit Service 82fcde
these structures, one for each long option.  Terminate the array with an
Packit Service 82fcde
element containing all zeros.
Packit Service 82fcde
Packit Service 82fcde
The @code{struct option} structure has these fields:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item const char *name
Packit Service 82fcde
This field is the name of the option.  It is a string.
Packit Service 82fcde
Packit Service 82fcde
@item int has_arg
Packit Service 82fcde
This field says whether the option takes an argument.  It is an integer,
Packit Service 82fcde
and there are three legitimate values: @w{@code{no_argument}},
Packit Service 82fcde
@code{required_argument} and @code{optional_argument}.
Packit Service 82fcde
Packit Service 82fcde
@item int *flag
Packit Service 82fcde
@itemx int val
Packit Service 82fcde
These fields control how to report or act on the option when it occurs.
Packit Service 82fcde
Packit Service 82fcde
If @code{flag} is a null pointer, then the @code{val} is a value which
Packit Service 82fcde
identifies this option.  Often these values are chosen to uniquely
Packit Service 82fcde
identify particular long options.
Packit Service 82fcde
Packit Service 82fcde
If @code{flag} is not a null pointer, it should be the address of an
Packit Service 82fcde
@code{int} variable which is the flag for this option.  The value in
Packit Service 82fcde
@code{val} is the value to store in the flag to indicate that the option
Packit Service 82fcde
was seen.
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int getopt_long (int @var{argc}, char *const *@var{argv}, const char *@var{shortopts}, const struct option *@var{longopts}, int *@var{indexptr})
Packit Service 82fcde
@standards{GNU, getopt.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
Packit Service 82fcde
@c Same issues as getopt.
Packit Service 82fcde
Decode options from the vector @var{argv} (whose length is @var{argc}).
Packit Service 82fcde
The argument @var{shortopts} describes the short options to accept, just as
Packit Service 82fcde
it does in @code{getopt}.  The argument @var{longopts} describes the long
Packit Service 82fcde
options to accept (see above).
Packit Service 82fcde
Packit Service 82fcde
When @code{getopt_long} encounters a short option, it does the same
Packit Service 82fcde
thing that @code{getopt} would do: it returns the character code for the
Packit Service 82fcde
option, and stores the option's argument (if it has one) in @code{optarg}.
Packit Service 82fcde
Packit Service 82fcde
When @code{getopt_long} encounters a long option, it takes actions based
Packit Service 82fcde
on the @code{flag} and @code{val} fields of the definition of that
Packit Service 82fcde
option.
Packit Service 82fcde
Packit Service 82fcde
If @code{flag} is a null pointer, then @code{getopt_long} returns the
Packit Service 82fcde
contents of @code{val} to indicate which option it found.  You should
Packit Service 82fcde
arrange distinct values in the @code{val} field for options with
Packit Service 82fcde
different meanings, so you can decode these values after
Packit Service 82fcde
@code{getopt_long} returns.  If the long option is equivalent to a short
Packit Service 82fcde
option, you can use the short option's character code in @code{val}.
Packit Service 82fcde
Packit Service 82fcde
If @code{flag} is not a null pointer, that means this option should just
Packit Service 82fcde
set a flag in the program.  The flag is a variable of type @code{int}
Packit Service 82fcde
that you define.  Put the address of the flag in the @code{flag} field.
Packit Service 82fcde
Put in the @code{val} field the value you would like this option to
Packit Service 82fcde
store in the flag.  In this case, @code{getopt_long} returns @code{0}.
Packit Service 82fcde
Packit Service 82fcde
For any long option, @code{getopt_long} tells you the index in the array
Packit Service 82fcde
@var{longopts} of the options definition, by storing it into
Packit Service 82fcde
@code{*@var{indexptr}}.  You can get the name of the option with
Packit Service 82fcde
@code{@var{longopts}[*@var{indexptr}].name}.  So you can distinguish among
Packit Service 82fcde
long options either by the values in their @code{val} fields or by their
Packit Service 82fcde
indices.  You can also distinguish in this way among long options that
Packit Service 82fcde
set flags.
Packit Service 82fcde
Packit Service 82fcde
When a long option has an argument, @code{getopt_long} puts the argument
Packit Service 82fcde
value in the variable @code{optarg} before returning.  When the option
Packit Service 82fcde
has no argument, the value in @code{optarg} is a null pointer.  This is
Packit Service 82fcde
how you can tell whether an optional argument was supplied.
Packit Service 82fcde
Packit Service 82fcde
When @code{getopt_long} has no more options to handle, it returns
Packit Service 82fcde
@code{-1}, and leaves in the variable @code{optind} the index in
Packit Service 82fcde
@var{argv} of the next remaining argument.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Since long option names were used before @code{getopt_long}
Packit Service 82fcde
was invented there are program interfaces which require programs
Packit Service 82fcde
to recognize options like @w{@samp{-option value}} instead of
Packit Service 82fcde
@w{@samp{--option value}}.  To enable these programs to use the GNU
Packit Service 82fcde
getopt functionality there is one more function available.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int getopt_long_only (int @var{argc}, char *const *@var{argv}, const char *@var{shortopts}, const struct option *@var{longopts}, int *@var{indexptr})
Packit Service 82fcde
@standards{GNU, getopt.h}
Packit Service 82fcde
@safety{@prelim{}@mtunsafe{@mtasurace{:getopt} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
Packit Service 82fcde
@c Same issues as getopt.
Packit Service 82fcde
Packit Service 82fcde
The @code{getopt_long_only} function is equivalent to the
Packit Service 82fcde
@code{getopt_long} function but it allows the user of the
Packit Service 82fcde
application to pass long options with only @samp{-} instead of
Packit Service 82fcde
@samp{--}.  The @samp{--} prefix is still recognized but instead of
Packit Service 82fcde
looking through the short options if a @samp{-} is seen it is first
Packit Service 82fcde
tried whether this parameter names a long option.  If not, it is parsed
Packit Service 82fcde
as a short option.
Packit Service 82fcde
Packit Service 82fcde
Assuming @code{getopt_long_only} is used starting an application with
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
  app -foo
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
the @code{getopt_long_only} will first look for a long option named
Packit Service 82fcde
@samp{foo}.  If this is not found, the short options @samp{f}, @samp{o},
Packit Service 82fcde
and again @samp{o} are recognized.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Getopt Long Option Example
Packit Service 82fcde
@subsection Example of Parsing Long Options with @code{getopt_long}
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
@include longopt.c.texi
Packit Service 82fcde
@end smallexample