Blame manual/getopt.texi

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