Blame manual/argp.texi

Packit 6c4009
@node Argp, Suboptions, Getopt, Parsing Program Arguments
Packit 6c4009
@need 5000
Packit 6c4009
@section Parsing Program Options with Argp
Packit 6c4009
@cindex argp (program argument parser)
Packit 6c4009
@cindex argument parsing with argp
Packit 6c4009
@cindex option parsing with argp
Packit 6c4009
Packit 6c4009
@dfn{Argp} is an interface for parsing unix-style argument vectors.
Packit 6c4009
@xref{Program Arguments}.
Packit 6c4009
Packit 6c4009
Argp provides features unavailable in the more commonly used
Packit 6c4009
@code{getopt} interface.  These features include automatically producing
Packit 6c4009
output in response to the @samp{--help} and @samp{--version} options, as
Packit 6c4009
described in the GNU coding standards.  Using argp makes it less likely
Packit 6c4009
that programmers will neglect to implement these additional options or
Packit 6c4009
keep them up to date.
Packit 6c4009
Packit 6c4009
Argp also provides the ability to merge several independently defined
Packit 6c4009
option parsers into one, mediating conflicts between them and making the
Packit 6c4009
result appear seamless.  A library can export an argp option parser that
Packit 6c4009
user programs might employ in conjunction with their own option parsers,
Packit 6c4009
resulting in less work for the user programs.  Some programs may use only
Packit 6c4009
argument parsers exported by libraries, thereby achieving consistent and
Packit 6c4009
efficient option-parsing for abstractions implemented by the libraries.
Packit 6c4009
Packit 6c4009
@pindex argp.h
Packit 6c4009
The header file @file{<argp.h>} should be included to use argp.
Packit 6c4009
Packit 6c4009
@subsection The @code{argp_parse} Function
Packit 6c4009
Packit 6c4009
The main interface to argp is the @code{argp_parse} function.  In many
Packit 6c4009
cases, calling @code{argp_parse} is the only argument-parsing code
Packit 6c4009
needed in @code{main}.
Packit 6c4009
@xref{Program Arguments}.
Packit 6c4009
Packit 6c4009
@deftypefun {error_t} argp_parse (const struct argp *@var{argp}, int @var{argc}, char **@var{argv}, unsigned @var{flags}, int *@var{arg_index}, void *@var{input})
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtslocale{} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
Packit 6c4009
@c Optionally alloca()tes standard help options, initializes the parser,
Packit 6c4009
@c then parses individual args in a loop, and then finalizes.
Packit 6c4009
@c  parser_init
Packit 6c4009
@c   calc_sizes ok
Packit 6c4009
@c    option_is_end ok
Packit 6c4009
@c   malloc @ascuheap @acsmem
Packit 6c4009
@c   parser_convert @mtslocale
Packit 6c4009
@c    convert_options @mtslocale
Packit 6c4009
@c     option_is_end ok
Packit 6c4009
@c     option_is_short ok
Packit 6c4009
@c      isprint, but locale may change within the loop
Packit 6c4009
@c     find_long_option ok
Packit 6c4009
@c   group_parse
Packit 6c4009
@c    group->parser (from argp->parser)
Packit 6c4009
@c  parser_parse_next
Packit 6c4009
@c   getopt_long(_only)_r many issues, same as non_r minus @mtasurace
Packit 6c4009
@c   parser_parse_arg
Packit 6c4009
@c    group_parse dup
Packit 6c4009
@c   parser_parse_opt
Packit 6c4009
@c    group_parse dup
Packit 6c4009
@c    argp_error dup @mtasurace:argpbuf @mtsenv @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c    dgettext (bad key error) dup @mtsenv @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c  parser_finalize
Packit 6c4009
@c   group_parse
Packit 6c4009
@c   fprintf dup @mtslocale @asucorrupt @aculock @acucorrupt [no @ascuheap @acsmem]
Packit 6c4009
@c   dgettext dup @mtsenv @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsfd @acsmem
Packit 6c4009
@c   arg_state_help
Packit 6c4009
@c   free dup @ascuhelp @acsmem
Packit 6c4009
The @code{argp_parse} function parses the arguments in @var{argv}, of
Packit 6c4009
length @var{argc}, using the argp parser @var{argp}.  @xref{Argp
Packit 6c4009
Parsers}.  Passing a null pointer for @var{argp} is the same as using
Packit 6c4009
a @code{struct argp} containing all zeros.
Packit 6c4009
Packit 6c4009
@var{flags} is a set of flag bits that modify the parsing behavior.
Packit 6c4009
@xref{Argp Flags}.  @var{input} is passed through to the argp parser
Packit 6c4009
@var{argp}, and has meaning defined by @var{argp}.  A typical usage is
Packit 6c4009
to pass a pointer to a structure which is used for specifying
Packit 6c4009
parameters to the parser and passing back the results.
Packit 6c4009
Packit 6c4009
Unless the @code{ARGP_NO_EXIT} or @code{ARGP_NO_HELP} flags are included
Packit 6c4009
in @var{flags}, calling @code{argp_parse} may result in the program
Packit 6c4009
exiting.  This behavior is true if an error is detected, or when an
Packit 6c4009
unknown option is encountered.  @xref{Program Termination}.
Packit 6c4009
Packit 6c4009
If @var{arg_index} is non-null, the index of the first unparsed option
Packit 6c4009
in @var{argv} is returned as a value.
Packit 6c4009
Packit 6c4009
The return value is zero for successful parsing, or an error code
Packit 6c4009
(@pxref{Error Codes}) if an error is detected.  Different argp parsers
Packit 6c4009
may return arbitrary error codes, but the standard error codes are:
Packit 6c4009
@code{ENOMEM} if a memory allocation error occurred, or @code{EINVAL} if
Packit 6c4009
an unknown option or option argument is encountered.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Globals: Argp Global Variables.  Global argp parameters.
Packit 6c4009
* Parsers: Argp Parsers.        Defining parsers for use with @code{argp_parse}.
Packit 6c4009
* Flags: Argp Flags.            Flags that modify the behavior of @code{argp_parse}.
Packit 6c4009
* Help: Argp Help.              Printing help messages when not parsing.
Packit 6c4009
* Examples: Argp Examples.      Simple examples of programs using argp.
Packit 6c4009
* Customization: Argp User Customization.
Packit 6c4009
                                Users may control the @samp{--help} output format.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argp Global Variables, Argp Parsers, , Argp
Packit 6c4009
@subsection Argp Global Variables
Packit 6c4009
Packit 6c4009
These variables make it easy for user programs to implement the
Packit 6c4009
@samp{--version} option and provide a bug-reporting address in the
Packit 6c4009
@samp{--help} output.  These are implemented in argp by default.
Packit 6c4009
Packit 6c4009
@deftypevar {const char *} argp_program_version
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
If defined or set by the user program to a non-zero value, then a
Packit 6c4009
@samp{--version} option is added when parsing with @code{argp_parse},
Packit 6c4009
which will print the @samp{--version} string followed by a newline and
Packit 6c4009
exit.  The exception to this is if the @code{ARGP_NO_EXIT} flag is used.
Packit 6c4009
@end deftypevar
Packit 6c4009
Packit 6c4009
@deftypevar {const char *} argp_program_bug_address
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
If defined or set by the user program to a non-zero value,
Packit 6c4009
@code{argp_program_bug_address} should point to a string that will be
Packit 6c4009
printed at the end of the standard output for the @samp{--help} option,
Packit 6c4009
embedded in a sentence that says @samp{Report bugs to @var{address}.}.
Packit 6c4009
@end deftypevar
Packit 6c4009
Packit 6c4009
@need 1500
Packit 6c4009
@defvar argp_program_version_hook
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
If defined or set by the user program to a non-zero value, a
Packit 6c4009
@samp{--version} option is added when parsing with @code{arg_parse},
Packit 6c4009
which prints the program version and exits with a status of zero.  This
Packit 6c4009
is not the case if the @code{ARGP_NO_HELP} flag is used.  If the
Packit 6c4009
@code{ARGP_NO_EXIT} flag is set, the exit behavior of the program is
Packit 6c4009
suppressed or modified, as when the argp parser is going to be used by
Packit 6c4009
other programs.
Packit 6c4009
Packit 6c4009
It should point to a function with this type of signature:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
void @var{print-version} (FILE *@var{stream}, struct argp_state *@var{state})
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
@xref{Argp Parsing State}, for an explanation of @var{state}.
Packit 6c4009
Packit 6c4009
This variable takes precedence over @code{argp_program_version}, and is
Packit 6c4009
useful if a program has version information not easily expressed in a
Packit 6c4009
simple string.
Packit 6c4009
@end defvar
Packit 6c4009
Packit 6c4009
@deftypevar error_t argp_err_exit_status
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This is the exit status used when argp exits due to a parsing error.  If
Packit 6c4009
not defined or set by the user program, this defaults to:
Packit 6c4009
@code{EX_USAGE} from @file{<sysexits.h>}.
Packit 6c4009
@end deftypevar
Packit 6c4009
Packit 6c4009
@node Argp Parsers, Argp Flags, Argp Global Variables, Argp
Packit 6c4009
@subsection Specifying Argp Parsers
Packit 6c4009
Packit 6c4009
The first argument to the @code{argp_parse} function is a pointer to a
Packit 6c4009
@code{struct argp}, which is known as an @dfn{argp parser}:
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct argp}
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This structure specifies how to parse a given set of options and
Packit 6c4009
arguments, perhaps in conjunction with other argp parsers.  It has the
Packit 6c4009
following fields:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item const struct argp_option *options
Packit 6c4009
A pointer to a vector of @code{argp_option} structures specifying which
Packit 6c4009
options this argp parser understands; it may be zero if there are no
Packit 6c4009
options at all.  @xref{Argp Option Vectors}.
Packit 6c4009
Packit 6c4009
@item argp_parser_t parser
Packit 6c4009
A pointer to a function that defines actions for this parser; it is
Packit 6c4009
called for each option parsed, and at other well-defined points in the
Packit 6c4009
parsing process.  A value of zero is the same as a pointer to a function
Packit 6c4009
that always returns @code{ARGP_ERR_UNKNOWN}.  @xref{Argp Parser
Packit 6c4009
Functions}.
Packit 6c4009
Packit 6c4009
@item const char *args_doc
Packit 6c4009
If non-zero, a string describing what non-option arguments are called by
Packit 6c4009
this parser.  This is only used to print the @samp{Usage:} message.  If
Packit 6c4009
it contains newlines, the strings separated by them are considered
Packit 6c4009
alternative usage patterns and printed on separate lines.  Lines after
Packit 6c4009
the first are prefixed by @samp{ or: } instead of @samp{Usage:}.
Packit 6c4009
Packit 6c4009
@item const char *doc
Packit 6c4009
If non-zero, a string containing extra text to be printed before and
Packit 6c4009
after the options in a long help message, with the two sections
Packit 6c4009
separated by a vertical tab (@code{'\v'}, @code{'\013'}) character.  By
Packit 6c4009
convention, the documentation before the options is just a short string
Packit 6c4009
explaining what the program does.  Documentation printed after the
Packit 6c4009
options describe behavior in more detail.
Packit 6c4009
Packit 6c4009
@item const struct argp_child *children
Packit 6c4009
A pointer to a vector of @code{argp_child} structures.  This pointer
Packit 6c4009
specifies which additional argp parsers should be combined with this
Packit 6c4009
one.  @xref{Argp Children}.
Packit 6c4009
Packit 6c4009
@item char *(*help_filter)(int @var{key}, const char *@var{text}, void *@var{input})
Packit 6c4009
If non-zero, a pointer to a function that filters the output of help
Packit 6c4009
messages.  @xref{Argp Help Filtering}.
Packit 6c4009
Packit 6c4009
@item const char *argp_domain
Packit 6c4009
If non-zero, the strings used in the argp library are translated using
Packit 6c4009
the domain described by this string.  If zero, the current default domain
Packit 6c4009
is used.
Packit 6c4009
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
Of the above group, @code{options}, @code{parser}, @code{args_doc}, and
Packit 6c4009
the @code{doc} fields are usually all that are needed.  If an argp
Packit 6c4009
parser is defined as an initialized C variable, only the fields used
Packit 6c4009
need be specified in the initializer.  The rest will default to zero due
Packit 6c4009
to the way C structure initialization works.  This design is exploited in
Packit 6c4009
most argp structures; the most-used fields are grouped near the
Packit 6c4009
beginning, the unused fields left unspecified.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Options: Argp Option Vectors.   Specifying options in an argp parser.
Packit 6c4009
* Argp Parser Functions::         Defining actions for an argp parser.
Packit 6c4009
* Children: Argp Children.        Combining multiple argp parsers.
Packit 6c4009
* Help Filtering: Argp Help Filtering.  Customizing help output for an argp parser.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argp Option Vectors, Argp Parser Functions, Argp Parsers, Argp Parsers
Packit 6c4009
@subsection Specifying Options in an Argp Parser
Packit 6c4009
Packit 6c4009
The @code{options} field in a @code{struct argp} points to a vector of
Packit 6c4009
@code{struct argp_option} structures, each of which specifies an option
Packit 6c4009
that the argp parser supports.  Multiple entries may be used for a single
Packit 6c4009
option provided it has multiple names.  This should be terminated by an
Packit 6c4009
entry with zero in all fields.  Note that when using an initialized C
Packit 6c4009
array for options, writing @code{@{ 0 @}} is enough to achieve this.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct argp_option}
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This structure specifies a single option that an argp parser
Packit 6c4009
understands, as well as how to parse and document that option.  It has
Packit 6c4009
the following fields:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item const char *name
Packit 6c4009
The long name for this option, corresponding to the long option
Packit 6c4009
@samp{--@var{name}}; this field may be zero if this option @emph{only}
Packit 6c4009
has a short name.  To specify multiple names for an option, additional
Packit 6c4009
entries may follow this one, with the @code{OPTION_ALIAS} flag
Packit 6c4009
set.  @xref{Argp Option Flags}.
Packit 6c4009
Packit 6c4009
@item int key
Packit 6c4009
The integer key provided by the current option to the option parser.  If
Packit 6c4009
@var{key} has a value that is a printable @sc{ascii} character (i.e.,
Packit 6c4009
@code{isascii (@var{key})} is true), it @emph{also} specifies a short
Packit 6c4009
option @samp{-@var{char}}, where @var{char} is the @sc{ascii} character
Packit 6c4009
with the code @var{key}.
Packit 6c4009
Packit 6c4009
@item const char *arg
Packit 6c4009
If non-zero, this is the name of an argument associated with this
Packit 6c4009
option, which must be provided (e.g., with the
Packit 6c4009
@samp{--@var{name}=@var{value}} or @samp{-@var{char} @var{value}}
Packit 6c4009
syntaxes), unless the @code{OPTION_ARG_OPTIONAL} flag (@pxref{Argp
Packit 6c4009
Option Flags}) is set, in which case it @emph{may} be provided.
Packit 6c4009
Packit 6c4009
@item int flags
Packit 6c4009
Flags associated with this option, some of which are referred to above.
Packit 6c4009
@xref{Argp Option Flags}.
Packit 6c4009
Packit 6c4009
@item const char *doc
Packit 6c4009
A documentation string for this option, for printing in help messages.
Packit 6c4009
Packit 6c4009
If both the @code{name} and @code{key} fields are zero, this string
Packit 6c4009
will be printed tabbed left from the normal option column, making it
Packit 6c4009
useful as a group header.  This will be the first thing printed in its
Packit 6c4009
group.  In this usage, it's conventional to end the string with a
Packit 6c4009
@samp{:} character.
Packit 6c4009
Packit 6c4009
@item int group
Packit 6c4009
Group identity for this option.
Packit 6c4009
Packit 6c4009
In a long help message, options are sorted alphabetically within each
Packit 6c4009
group, and the groups presented in the order 0, 1, 2, @dots{}, @var{n},
Packit 6c4009
@minus{}@var{m}, @dots{}, @minus{}2, @minus{}1.
Packit 6c4009
Packit 6c4009
Every entry in an options array with this field 0 will inherit the group
Packit 6c4009
number of the previous entry, or zero if it's the first one.  If it's a
Packit 6c4009
group header with @code{name} and @code{key} fields both zero, the
Packit 6c4009
previous entry + 1 is the default.  Automagic options such as
Packit 6c4009
@samp{--help} are put into group @minus{}1.
Packit 6c4009
Packit 6c4009
Note that because of C structure initialization rules, this field often
Packit 6c4009
need not be specified, because 0 is the correct value.
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Flags: Argp Option Flags.     Flags for options.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argp Option Flags, , , Argp Option Vectors
Packit 6c4009
@subsubsection Flags for Argp Options
Packit 6c4009
Packit 6c4009
The following flags may be or'd together in the @code{flags} field of a
Packit 6c4009
@code{struct argp_option}.  These flags control various aspects of how
Packit 6c4009
that option is parsed or displayed in help messages:
Packit 6c4009
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item OPTION_ARG_OPTIONAL
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The argument associated with this option is optional.
Packit 6c4009
Packit 6c4009
@item OPTION_HIDDEN
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This option isn't displayed in any help messages.
Packit 6c4009
Packit 6c4009
@item OPTION_ALIAS
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This option is an alias for the closest previous non-alias option.  This
Packit 6c4009
means that it will be displayed in the same help entry, and will inherit
Packit 6c4009
fields other than @code{name} and @code{key} from the option being
Packit 6c4009
aliased.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@item OPTION_DOC
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This option isn't actually an option and should be ignored by the actual
Packit 6c4009
option parser.  It is an arbitrary section of documentation that should
Packit 6c4009
be displayed in much the same manner as the options.  This is known as a
Packit 6c4009
@dfn{documentation option}.
Packit 6c4009
Packit 6c4009
If this flag is set, then the option @code{name} field is displayed
Packit 6c4009
unmodified (e.g., no @samp{--} prefix is added) at the left-margin where
Packit 6c4009
a @emph{short} option would normally be displayed, and this
Packit 6c4009
documentation string is left in its usual place.  For purposes of
Packit 6c4009
sorting, any leading whitespace and punctuation is ignored, unless the
Packit 6c4009
first non-whitespace character is @samp{-}.  This entry is displayed
Packit 6c4009
after all options, after @code{OPTION_DOC} entries with a leading
Packit 6c4009
@samp{-}, in the same group.
Packit 6c4009
Packit 6c4009
@item OPTION_NO_USAGE
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This option shouldn't be included in `long' usage messages, but should
Packit 6c4009
still be included in other help messages.  This is intended for options
Packit 6c4009
that are completely documented in an argp's @code{args_doc}
Packit 6c4009
field.  @xref{Argp Parsers}.  Including this option in the generic usage
Packit 6c4009
list would be redundant, and should be avoided.
Packit 6c4009
Packit 6c4009
For instance, if @code{args_doc} is @code{"FOO BAR\n-x BLAH"}, and the
Packit 6c4009
@samp{-x} option's purpose is to distinguish these two cases, @samp{-x}
Packit 6c4009
should probably be marked @code{OPTION_NO_USAGE}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Argp Parser Functions, Argp Children, Argp Option Vectors, Argp Parsers
Packit 6c4009
@subsection Argp Parser Functions
Packit 6c4009
Packit 6c4009
The function pointed to by the @code{parser} field in a @code{struct
Packit 6c4009
argp} (@pxref{Argp Parsers}) defines what actions take place in response
Packit 6c4009
to each option or argument parsed.  It is also used as a hook, allowing a
Packit 6c4009
parser to perform tasks at certain other points during parsing.
Packit 6c4009
Packit 6c4009
@need 2000
Packit 6c4009
Argp parser functions have the following type signature:
Packit 6c4009
Packit 6c4009
@cindex argp parser functions
Packit 6c4009
@smallexample
Packit 6c4009
error_t @var{parser} (int @var{key}, char *@var{arg}, struct argp_state *@var{state})
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
where the arguments are as follows:
Packit 6c4009
Packit 6c4009
@table @var
Packit 6c4009
@item key
Packit 6c4009
For each option that is parsed, @var{parser} is called with a value of
Packit 6c4009
@var{key} from that option's @code{key} field in the option
Packit 6c4009
vector.  @xref{Argp Option Vectors}.  @var{parser} is also called at
Packit 6c4009
other times with special reserved keys, such as @code{ARGP_KEY_ARG} for
Packit 6c4009
non-option arguments.  @xref{Argp Special Keys}.
Packit 6c4009
Packit 6c4009
@item arg
Packit 6c4009
If @var{key} is an option, @var{arg} is its given value.  This defaults
Packit 6c4009
to zero if no value is specified.  Only options that have a non-zero
Packit 6c4009
@code{arg} field can ever have a value.  These must @emph{always} have a
Packit 6c4009
value unless the @code{OPTION_ARG_OPTIONAL} flag is specified.  If the
Packit 6c4009
input being parsed specifies a value for an option that doesn't allow
Packit 6c4009
one, an error results before @var{parser} ever gets called.
Packit 6c4009
Packit 6c4009
If @var{key} is @code{ARGP_KEY_ARG}, @var{arg} is a non-option
Packit 6c4009
argument.  Other special keys always have a zero @var{arg}.
Packit 6c4009
Packit 6c4009
@item state
Packit 6c4009
@var{state} points to a @code{struct argp_state}, containing useful
Packit 6c4009
information about the current parsing state for use by
Packit 6c4009
@var{parser}.  @xref{Argp Parsing State}.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
When @var{parser} is called, it should perform whatever action is
Packit 6c4009
appropriate for @var{key}, and return @code{0} for success,
Packit 6c4009
@code{ARGP_ERR_UNKNOWN} if the value of @var{key} is not handled by this
Packit 6c4009
parser function, or a unix error code if a real error
Packit 6c4009
occurred.  @xref{Error Codes}.
Packit 6c4009
Packit 6c4009
@deftypevr Macro int ARGP_ERR_UNKNOWN
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Argp parser functions should return @code{ARGP_ERR_UNKNOWN} for any
Packit 6c4009
@var{key} value they do not recognize, or for non-option arguments
Packit 6c4009
(@code{@var{key} == ARGP_KEY_ARG}) that they are not equipped to handle.
Packit 6c4009
@end deftypevr
Packit 6c4009
Packit 6c4009
@need 3000
Packit 6c4009
A typical parser function uses a switch statement on @var{key}:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
error_t
Packit 6c4009
parse_opt (int key, char *arg, struct argp_state *state)
Packit 6c4009
@{
Packit 6c4009
  switch (key)
Packit 6c4009
    @{
Packit 6c4009
    case @var{option_key}:
Packit 6c4009
      @var{action}
Packit 6c4009
      break;
Packit 6c4009
    @dots{}
Packit 6c4009
    default:
Packit 6c4009
      return ARGP_ERR_UNKNOWN;
Packit 6c4009
    @}
Packit 6c4009
  return 0;
Packit 6c4009
@}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Keys: Argp Special Keys.           Special values for the @var{key} argument.
Packit 6c4009
* State: Argp Parsing State.         What the @var{state} argument refers to.
Packit 6c4009
* Functions: Argp Helper Functions.  Functions to help during argp parsing.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argp Special Keys, Argp Parsing State, , Argp Parser Functions
Packit 6c4009
@subsubsection Special Keys for Argp Parser Functions
Packit 6c4009
Packit 6c4009
In addition to key values corresponding to user options, the @var{key}
Packit 6c4009
argument to argp parser functions may have a number of other special
Packit 6c4009
values.  In the following example @var{arg} and @var{state} refer to
Packit 6c4009
parser function arguments.  @xref{Argp Parser Functions}.
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item ARGP_KEY_ARG
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This is not an option at all, but rather a command line argument, whose
Packit 6c4009
value is pointed to by @var{arg}.
Packit 6c4009
Packit 6c4009
When there are multiple parser functions in play due to argp parsers
Packit 6c4009
being combined, it's impossible to know which one will handle a specific
Packit 6c4009
argument.  Each is called until one returns 0 or an error other than
Packit 6c4009
@code{ARGP_ERR_UNKNOWN}; if an argument is not handled,
Packit 6c4009
@code{argp_parse} immediately returns success, without parsing any more
Packit 6c4009
arguments.
Packit 6c4009
Packit 6c4009
Once a parser function returns success for this key, that fact is
Packit 6c4009
recorded, and the @code{ARGP_KEY_NO_ARGS} case won't be
Packit 6c4009
used.  @emph{However}, if while processing the argument a parser function
Packit 6c4009
decrements the @code{next} field of its @var{state} argument, the option
Packit 6c4009
won't be considered processed; this is to allow you to actually modify
Packit 6c4009
the argument, perhaps into an option, and have it processed again.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_ARGS
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
If a parser function returns @code{ARGP_ERR_UNKNOWN} for
Packit 6c4009
@code{ARGP_KEY_ARG}, it is immediately called again with the key
Packit 6c4009
@code{ARGP_KEY_ARGS}, which has a similar meaning, but is slightly more
Packit 6c4009
convenient for consuming all remaining arguments.  @var{arg} is 0, and
Packit 6c4009
the tail of the argument vector may be found at @code{@var{state}->argv
Packit 6c4009
+ @var{state}->next}.  If success is returned for this key, and
Packit 6c4009
@code{@var{state}->next} is unchanged, all remaining arguments are
Packit 6c4009
considered to have been consumed.  Otherwise, the amount by which
Packit 6c4009
@code{@var{state}->next} has been adjusted indicates how many were used.
Packit 6c4009
Here's an example that uses both, for different args:
Packit 6c4009
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@dots{}
Packit 6c4009
case ARGP_KEY_ARG:
Packit 6c4009
  if (@var{state}->arg_num == 0)
Packit 6c4009
    /* First argument */
Packit 6c4009
    first_arg = @var{arg};
Packit 6c4009
  else
Packit 6c4009
    /* Let the next case parse it.  */
Packit 6c4009
    return ARGP_KEY_UNKNOWN;
Packit 6c4009
  break;
Packit 6c4009
case ARGP_KEY_ARGS:
Packit 6c4009
  remaining_args = @var{state}->argv + @var{state}->next;
Packit 6c4009
  num_remaining_args = @var{state}->argc - @var{state}->next;
Packit 6c4009
  break;
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_END
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This indicates that there are no more command line arguments.  Parser
Packit 6c4009
functions are called in a different order, children first.  This allows
Packit 6c4009
each parser to clean up its state for the parent.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_NO_ARGS
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Because it's common to do some special processing if there aren't any
Packit 6c4009
non-option args, parser functions are called with this key if they
Packit 6c4009
didn't successfully process any non-option arguments.  This is called
Packit 6c4009
just before @code{ARGP_KEY_END}, where more general validity checks on
Packit 6c4009
previously parsed arguments take place.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_INIT
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This is passed in before any parsing is done.  Afterwards, the values of
Packit 6c4009
each element of the @code{child_input} field of @var{state}, if any, are
Packit 6c4009
copied to each child's state to be the initial value of the @code{input}
Packit 6c4009
when @emph{their} parsers are called.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_SUCCESS
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Passed in when parsing has successfully been completed, even if
Packit 6c4009
arguments remain.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_ERROR
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Passed in if an error has occurred and parsing is terminated.  In this
Packit 6c4009
case a call with a key of @code{ARGP_KEY_SUCCESS} is never made.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_FINI
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The final key ever seen by any parser, even after
Packit 6c4009
@code{ARGP_KEY_SUCCESS} and @code{ARGP_KEY_ERROR}.  Any resources
Packit 6c4009
allocated by @code{ARGP_KEY_INIT} may be freed here.  At times, certain
Packit 6c4009
resources allocated are to be returned to the caller after a successful
Packit 6c4009
parse.  In that case, those particular resources can be freed in the
Packit 6c4009
@code{ARGP_KEY_ERROR} case.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
In all cases, @code{ARGP_KEY_INIT} is the first key seen by parser
Packit 6c4009
functions, and @code{ARGP_KEY_FINI} the last, unless an error was
Packit 6c4009
returned by the parser for @code{ARGP_KEY_INIT}.  Other keys can occur
Packit 6c4009
in one the following orders.  @var{opt} refers to an arbitrary option
Packit 6c4009
key:
Packit 6c4009
Packit 6c4009
@table @asis
Packit 6c4009
@item @var{opt}@dots{} @code{ARGP_KEY_NO_ARGS} @code{ARGP_KEY_END} @code{ARGP_KEY_SUCCESS}
Packit 6c4009
The arguments being parsed did not contain any non-option arguments.
Packit 6c4009
Packit 6c4009
@item ( @var{opt} | @code{ARGP_KEY_ARG} )@dots{} @code{ARGP_KEY_END} @code{ARGP_KEY_SUCCESS}
Packit 6c4009
All non-option arguments were successfully handled by a parser
Packit 6c4009
function.  There may be multiple parser functions if multiple argp
Packit 6c4009
parsers were combined.
Packit 6c4009
Packit 6c4009
@item ( @var{opt} | @code{ARGP_KEY_ARG} )@dots{} @code{ARGP_KEY_SUCCESS}
Packit 6c4009
Some non-option argument went unrecognized.
Packit 6c4009
Packit 6c4009
This occurs when every parser function returns @code{ARGP_KEY_UNKNOWN}
Packit 6c4009
for an argument, in which case parsing stops at that argument if
Packit 6c4009
@var{arg_index} is a null pointer.  Otherwise an error occurs.
Packit 6c4009
@end table
Packit 6c4009
Packit 6c4009
In all cases, if a non-null value for @var{arg_index} gets passed to
Packit 6c4009
@code{argp_parse}, the index of the first unparsed command-line argument
Packit 6c4009
is passed back in that value.
Packit 6c4009
Packit 6c4009
If an error occurs and is either detected by argp or because a parser
Packit 6c4009
function returned an error value, each parser is called with
Packit 6c4009
@code{ARGP_KEY_ERROR}.  No further calls are made, except the final call
Packit 6c4009
with @code{ARGP_KEY_FINI}.
Packit 6c4009
Packit 6c4009
@node Argp Parsing State, Argp Helper Functions, Argp Special Keys, Argp Parser Functions
Packit 6c4009
@subsubsection Argp Parsing State
Packit 6c4009
Packit 6c4009
The third argument to argp parser functions (@pxref{Argp Parser
Packit 6c4009
Functions}) is a pointer to a @code{struct argp_state}, which contains
Packit 6c4009
information about the state of the option parsing.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct argp_state}
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This structure has the following fields, which may be modified as noted:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item const struct argp *const root_argp
Packit 6c4009
The top level argp parser being parsed.  Note that this is often
Packit 6c4009
@emph{not} the same @code{struct argp} passed into @code{argp_parse} by
Packit 6c4009
the invoking program.  @xref{Argp}.  It is an internal argp parser that
Packit 6c4009
contains options implemented by @code{argp_parse} itself, such as
Packit 6c4009
@samp{--help}.
Packit 6c4009
Packit 6c4009
@item int argc
Packit 6c4009
@itemx char **argv
Packit 6c4009
The argument vector being parsed.  This may be modified.
Packit 6c4009
Packit 6c4009
@item int next
Packit 6c4009
The index in @code{argv} of the next argument to be parsed.  This may be
Packit 6c4009
modified.
Packit 6c4009
Packit 6c4009
One way to consume all remaining arguments in the input is to set
Packit 6c4009
@code{@var{state}->next = @var{state}->argc}, perhaps after recording
Packit 6c4009
the value of the @code{next} field to find the consumed arguments.  The
Packit 6c4009
current option can be re-parsed immediately by decrementing this field,
Packit 6c4009
then modifying @code{@var{state}->argv[@var{state}->next]} to reflect
Packit 6c4009
the option that should be reexamined.
Packit 6c4009
Packit 6c4009
@item unsigned flags
Packit 6c4009
The flags supplied to @code{argp_parse}.  These may be modified, although
Packit 6c4009
some flags may only take effect when @code{argp_parse} is first
Packit 6c4009
invoked.  @xref{Argp Flags}.
Packit 6c4009
Packit 6c4009
@item unsigned arg_num
Packit 6c4009
While calling a parsing function with the @var{key} argument
Packit 6c4009
@code{ARGP_KEY_ARG}, this represents the number of the current arg,
Packit 6c4009
starting at 0.  It is incremented after each @code{ARGP_KEY_ARG} call
Packit 6c4009
returns.  At all other times, this is the number of @code{ARGP_KEY_ARG}
Packit 6c4009
arguments that have been processed.
Packit 6c4009
Packit 6c4009
@item int quoted
Packit 6c4009
If non-zero, the index in @code{argv} of the first argument following a
Packit 6c4009
special @samp{--} argument.  This prevents anything that follows from
Packit 6c4009
being interpreted as an option.  It is only set after argument parsing
Packit 6c4009
has proceeded past this point.
Packit 6c4009
Packit 6c4009
@item void *input
Packit 6c4009
An arbitrary pointer passed in from the caller of @code{argp_parse}, in
Packit 6c4009
the @var{input} argument.
Packit 6c4009
Packit 6c4009
@item void **child_inputs
Packit 6c4009
These are values that will be passed to child parsers.  This vector will
Packit 6c4009
be the same length as the number of children in the current parser.  Each
Packit 6c4009
child parser will be given the value of
Packit 6c4009
@code{@var{state}->child_inputs[@var{i}]} as @emph{its}
Packit 6c4009
@code{@var{state}->input} field, where @var{i} is the index of the child
Packit 6c4009
in the this parser's @code{children} field.  @xref{Argp Children}.
Packit 6c4009
Packit 6c4009
@item void *hook
Packit 6c4009
For the parser function's use.  Initialized to 0, but otherwise ignored
Packit 6c4009
by argp.
Packit 6c4009
Packit 6c4009
@item char *name
Packit 6c4009
The name used when printing messages.  This is initialized to
Packit 6c4009
@code{argv[0]}, or @code{program_invocation_name} if @code{argv[0]} is
Packit 6c4009
unavailable.
Packit 6c4009
Packit 6c4009
@item FILE *err_stream
Packit 6c4009
@itemx FILE *out_stream
Packit 6c4009
The stdio streams used when argp prints.  Error messages are printed to
Packit 6c4009
@code{err_stream}, all other output, such as @samp{--help} output) to
Packit 6c4009
@code{out_stream}.  These are initialized to @code{stderr} and
Packit 6c4009
@code{stdout} respectively.  @xref{Standard Streams}.
Packit 6c4009
Packit 6c4009
@item void *pstate
Packit 6c4009
Private, for use by the argp implementation.
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@node Argp Helper Functions, , Argp Parsing State, Argp Parser Functions
Packit 6c4009
@subsubsection Functions For Use in Argp Parsers
Packit 6c4009
Packit 6c4009
Argp provides a number of functions available to the user of argp
Packit 6c4009
(@pxref{Argp Parser Functions}), mostly for producing error messages.
Packit 6c4009
These take as their first argument the @var{state} argument to the
Packit 6c4009
parser function.  @xref{Argp Parsing State}.
Packit 6c4009
Packit 6c4009
Packit 6c4009
@cindex usage messages, in argp
Packit 6c4009
@deftypefun void argp_usage (const struct argp_state *@var{state})
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
Packit 6c4009
@c Just calls argp_state_help with stderr and ARGP_HELP_STD_USAGE.
Packit 6c4009
Outputs the standard usage message for the argp parser referred to by
Packit 6c4009
@var{state} to @code{@var{state}->err_stream} and terminates the program
Packit 6c4009
with @code{exit (argp_err_exit_status)}.  @xref{Argp Global Variables}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex syntax error messages, in argp
Packit 6c4009
@deftypefun void argp_error (const struct argp_state *@var{state}, const char *@var{fmt}, @dots{})
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
Packit 6c4009
@c Lock stream, vasprintf the formatted message into a buffer, print the
Packit 6c4009
@c buffer prefixed by the short program name (in libc,
Packit 6c4009
@c argp_short_program_name is a macro that expands to
Packit 6c4009
@c program_invocation_short_name), releases the buffer, then call
Packit 6c4009
@c argp_state_help with stream and ARGP_HELP_STD_ERR, unlocking the
Packit 6c4009
@c stream at the end.
Packit 6c4009
Prints the printf format string @var{fmt} and following args, preceded
Packit 6c4009
by the program name and @samp{:}, and followed by a @w{@samp{Try @dots{}
Packit 6c4009
--help}} message, and terminates the program with an exit status of
Packit 6c4009
@code{argp_err_exit_status}.  @xref{Argp Global Variables}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@cindex error messages, in argp
Packit 6c4009
@deftypefun void argp_failure (const struct argp_state *@var{state}, int @var{status}, int @var{errnum}, const char *@var{fmt}, @dots{})
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{}}}
Packit 6c4009
@c Lock stream, write out the short program name, vasprintf the optional
Packit 6c4009
@c formatted message to a buffer, print the buffer prefixed by colon and
Packit 6c4009
@c blank, release the buffer, call strerror_r with an automatic buffer,
Packit 6c4009
@c print it out after colon and blank, put[w]c a line break, unlock the
Packit 6c4009
@c stream, then exit unless ARGP_NO_EXIT.
Packit 6c4009
Similar to the standard GNU error-reporting function @code{error}, this
Packit 6c4009
prints the program name and @samp{:}, the printf format string
Packit 6c4009
@var{fmt}, and the appropriate following args.  If it is non-zero, the
Packit 6c4009
standard unix error text for @var{errnum} is printed.  If @var{status} is
Packit 6c4009
non-zero, it terminates the program with that value as its exit status.
Packit 6c4009
Packit 6c4009
The difference between @code{argp_failure} and @code{argp_error} is that
Packit 6c4009
@code{argp_error} is for @emph{parsing errors}, whereas
Packit 6c4009
@code{argp_failure} is for other problems that occur during parsing but
Packit 6c4009
don't reflect a syntactic problem with the input, such as illegal values
Packit 6c4009
for options, bad phase of the moon, etc.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@deftypefun void argp_state_help (const struct argp_state *@var{state}, FILE *@var{stream}, unsigned @var{flags})
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
Packit 6c4009
@c Just calls _help with the short program name and optionally exit.
Packit 6c4009
@c The main problems in _help, besides the usual issues with stream I/O
Packit 6c4009
@c and translation, are the use of a static buffer (uparams, thus
Packit 6c4009
@c @mtasurace:argpbuf) that makes the whole thing thread-unsafe, reading
Packit 6c4009
@c from the environment for ARGP_HELP_FMT, accessing the locale object
Packit 6c4009
@c multiple times.
Packit 6c4009
Packit 6c4009
@c _help @mtsenv @mtasurace:argpbuf @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c  dgettext @ascuintl
Packit 6c4009
@c  flockfile @aculock
Packit 6c4009
@c  funlockfile @aculock
Packit 6c4009
@c  fill_in_uparams @mtsenv @mtasurace:argpbuf @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
Packit 6c4009
@c   argp_failure dup (status = errnum = 0)
Packit 6c4009
@c   atoi dup @mtslocale
Packit 6c4009
@c  argp_hol @ascuheap @acsmem
Packit 6c4009
@c   make_hol @ascuheap @acsmem
Packit 6c4009
@c   hol_add_cluster @ascuheap @acsmem
Packit 6c4009
@c   hol_append @ascuheap @acsmem
Packit 6c4009
@c  hol_set_group ok
Packit 6c4009
@c   hol_find_entry ok
Packit 6c4009
@c  hol_sort @mtslocale @acucorrupt
Packit 6c4009
@c   qsort dup @acucorrupt
Packit 6c4009
@c    hol_entry_qcmp @mtslocale
Packit 6c4009
@c     hol_entry_cmp @mtslocale
Packit 6c4009
@c      group_cmp ok
Packit 6c4009
@c      hol_cluster_cmp ok
Packit 6c4009
@c       group_cmp ok
Packit 6c4009
@c      hol_entry_first_short @mtslocale
Packit 6c4009
@c       hol_entry_short_iterate [@mtslocale]
Packit 6c4009
@c        until_short ok
Packit 6c4009
@c         oshort ok
Packit 6c4009
@c          isprint ok
Packit 6c4009
@c      odoc ok
Packit 6c4009
@c      hol_entry_first_long ok
Packit 6c4009
@c      canon_doc_option @mtslocale
Packit 6c4009
@c      tolower dup
Packit 6c4009
@c  hol_usage @mtslocale @ascuintl @ascuheap @acsmem
Packit 6c4009
@c   hol_entry_short_iterate ok
Packit 6c4009
@c    add_argless_short_opt ok
Packit 6c4009
@c   argp_fmtstream_printf dup
Packit 6c4009
@c   hol_entry_short_iterate @mtslocale @ascuintl @ascuheap @acsmem
Packit 6c4009
@c    usage_argful_short_opt @mtslocale @ascuintl @ascuheap @acsmem
Packit 6c4009
@c     dgettext dup
Packit 6c4009
@c     argp_fmtstream_printf dup
Packit 6c4009
@c   hol_entry_long_iterate @mtslocale @ascuintl @ascuheap @acsmem
Packit 6c4009
@c    usage_long_opt @mtslocale @ascuintl @ascuheap @acsmem
Packit 6c4009
@c     dgettext dup
Packit 6c4009
@c     argp_fmtstream_printf dup
Packit 6c4009
@c  hol_help @mtslocale @mtasurace:argpbuf @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c   hol_entry_help @mtslocale @mtasurace:argpbuf @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c    argp_fmtstream_set_lmargin dup
Packit 6c4009
@c    argp_fmtstream_wmargin dup
Packit 6c4009
@c    argp_fmtstream_set_wmargin dup
Packit 6c4009
@c    comma @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c     argp_fmtstream_putc dup
Packit 6c4009
@c     hol_cluster_is_child ok
Packit 6c4009
@c     argp_fmtstream_wmargin dup
Packit 6c4009
@c     print_header dup
Packit 6c4009
@c     argp_fmtstream_set_wmargin dup
Packit 6c4009
@c     argp_fmtstream_puts dup
Packit 6c4009
@c     indent_to dup
Packit 6c4009
@c    argp_fmtstream_putc dup
Packit 6c4009
@c    arg @mtslocale @ascuheap @acsmem
Packit 6c4009
@c     argp_fmtstream_printf dup
Packit 6c4009
@c    odoc dup
Packit 6c4009
@c    argp_fmtstream_puts dup
Packit 6c4009
@c    argp_fmtstream_printf dup
Packit 6c4009
@c    print_header @mtslocale @mtasurace:argpbuf @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c     dgettext dup
Packit 6c4009
@c     filter_doc dup
Packit 6c4009
@c     argp_fmtstream_putc dup
Packit 6c4009
@c     indent_to dup
Packit 6c4009
@c     argp_fmtstream_set_lmargin dup
Packit 6c4009
@c     argp_fmtstream_set_wmargin dup
Packit 6c4009
@c     argp_fmtstream_puts dup
Packit 6c4009
@c     free dup
Packit 6c4009
@c    filter_doc dup
Packit 6c4009
@c    argp_fmtstream_point dup
Packit 6c4009
@c    indent_to @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c     argp_fmtstream_point dup
Packit 6c4009
@c     argp_fmtstream_putc dup
Packit 6c4009
@c   dgettext dup
Packit 6c4009
@c   filter_doc dup
Packit 6c4009
@c   argp_fmtstream_putc dup
Packit 6c4009
@c   argp_fmtstream_puts dup
Packit 6c4009
@c   free dup
Packit 6c4009
@c  hol_free @ascuheap @acsmem
Packit 6c4009
@c   free dup
Packit 6c4009
@c  argp_args_levels ok
Packit 6c4009
@c  argp_args_usage @mtslocale @ascuintl @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c   dgettext dup
Packit 6c4009
@c   filter_doc ok
Packit 6c4009
@c    argp_input ok
Packit 6c4009
@c    argp->help_filter
Packit 6c4009
@c   space @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c    argp_fmtstream_point dup
Packit 6c4009
@c    argp_fmtstream_rmargin @mtslocale @asucorrupt @acucorrupt @aculock
Packit 6c4009
@c     argp_fmtstream_update dup
Packit 6c4009
@c    argp_fmtstream_putc dup
Packit 6c4009
@c   argp_fmtstream_write dup
Packit 6c4009
@c   free dup
Packit 6c4009
@c  argp_doc @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c   dgettext @ascuintl
Packit 6c4009
@c   strndup @ascuheap @acsmem
Packit 6c4009
@c   argp_input dup
Packit 6c4009
@c   argp->help_filter
Packit 6c4009
@c   argp_fmtstream_putc @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c    argp_fmtstream_ensure dup
Packit 6c4009
@c   argp_fmtstream_write dup
Packit 6c4009
@c   argp_fmtstream_puts dup
Packit 6c4009
@c   argp_fmtstream_point @mtslocale @asucorrupt @acucorrupt @aculock
Packit 6c4009
@c    argp_fmtstream_update dup
Packit 6c4009
@c   argp_fmtstream_lmargin dup
Packit 6c4009
@c   free dup
Packit 6c4009
@c  argp_make_fmtstream @ascuheap @acsmem
Packit 6c4009
@c  argp_fmtstream_free @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c   argp_fmtstream_update @mtslocale @asucorrupt @acucorrupt @aculock
Packit 6c4009
@c    put[w]c_unlocked dup
Packit 6c4009
@c    isblank in loop @mtslocale
Packit 6c4009
@c    fxprintf @aculock
Packit 6c4009
@c   fxprintf @aculock
Packit 6c4009
@c   free dup
Packit 6c4009
@c  argp_fmtstream_set_wmargin @mtslocale @asucorrupt @acucorrupt @aculock
Packit 6c4009
@c   argp_fmtstream_update dup
Packit 6c4009
@c  argp_fmtstream_printf @mtslocale @ascuheap @acsmem
Packit 6c4009
@c   argp_fmtstream_ensure dup
Packit 6c4009
@c   vsnprintf dup
Packit 6c4009
@c  argp_fmtstream_set_lmargin @mtslocale @asucorrupt @acucorrupt @aculock
Packit 6c4009
@c   argp_fmtstream_update dup
Packit 6c4009
@c  argp_fmtstream_puts @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c   argp_fmtstream_write @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c    argp_fmtstream_ensure @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
Packit 6c4009
@c     argp_fmtstream_update dup
Packit 6c4009
@c     fxprintf @aculock
Packit 6c4009
@c     realloc @ascuheap @acsmem
Packit 6c4009
Outputs a help message for the argp parser referred to by @var{state},
Packit 6c4009
to @var{stream}.  The @var{flags} argument determines what sort of help
Packit 6c4009
message is produced.  @xref{Argp Help Flags}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
Error output is sent to @code{@var{state}->err_stream}, and the program
Packit 6c4009
name printed is @code{@var{state}->name}.
Packit 6c4009
Packit 6c4009
The output or program termination behavior of these functions may be
Packit 6c4009
suppressed if the @code{ARGP_NO_EXIT} or @code{ARGP_NO_ERRS} flags are
Packit 6c4009
passed to @code{argp_parse}.  @xref{Argp Flags}.
Packit 6c4009
Packit 6c4009
This behavior is useful if an argp parser is exported for use by other
Packit 6c4009
programs (e.g., by a library), and may be used in a context where it is
Packit 6c4009
not desirable to terminate the program in response to parsing errors.  In
Packit 6c4009
argp parsers intended for such general use, and for the case where the
Packit 6c4009
program @emph{doesn't} terminate, calls to any of these functions should
Packit 6c4009
be followed by code that returns the appropriate error code:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
if (@var{bad argument syntax})
Packit 6c4009
  @{
Packit 6c4009
     argp_usage (@var{state});
Packit 6c4009
     return EINVAL;
Packit 6c4009
  @}
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
If a parser function will @emph{only} be used when @code{ARGP_NO_EXIT}
Packit 6c4009
is not set, the return may be omitted.
Packit 6c4009
Packit 6c4009
@node Argp Children, Argp Help Filtering, Argp Parser Functions, Argp Parsers
Packit 6c4009
@subsection Combining Multiple Argp Parsers
Packit 6c4009
Packit 6c4009
The @code{children} field in a @code{struct argp} enables other argp
Packit 6c4009
parsers to be combined with the referencing one for the parsing of a
Packit 6c4009
single set of arguments.  This field should point to a vector of
Packit 6c4009
@code{struct argp_child}, which is terminated by an entry having a value
Packit 6c4009
of zero in the @code{argp} field.
Packit 6c4009
Packit 6c4009
Where conflicts between combined parsers arise, as when two specify an
Packit 6c4009
option with the same name, the parser conflicts are resolved in favor of
Packit 6c4009
the parent argp parser(s), or the earlier of the argp parsers in the
Packit 6c4009
list of children.
Packit 6c4009
Packit 6c4009
@deftp {Data Type} {struct argp_child}
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
An entry in the list of subsidiary argp parsers pointed to by the
Packit 6c4009
@code{children} field in a @code{struct argp}.  The fields are as
Packit 6c4009
follows:
Packit 6c4009
Packit 6c4009
@table @code
Packit 6c4009
@item const struct argp *argp
Packit 6c4009
The child argp parser, or zero to end of the list.
Packit 6c4009
Packit 6c4009
@item int flags
Packit 6c4009
Flags for this child.
Packit 6c4009
Packit 6c4009
@item const char *header
Packit 6c4009
If non-zero, this is an optional header to be printed within help output
Packit 6c4009
before the child options.  As a side-effect, a non-zero value forces the
Packit 6c4009
child options to be grouped together.  To achieve this effect without
Packit 6c4009
actually printing a header string, use a value of @code{""}.  As with
Packit 6c4009
header strings specified in an option entry, the conventional value of
Packit 6c4009
the last character is @samp{:}.  @xref{Argp Option Vectors}.
Packit 6c4009
Packit 6c4009
@item int group
Packit 6c4009
This is where the child options are grouped relative to the other
Packit 6c4009
`consolidated' options in the parent argp parser.  The values are the
Packit 6c4009
same as the @code{group} field in @code{struct argp_option}.  @xref{Argp
Packit 6c4009
Option Vectors}.  All child-groupings follow parent options at a
Packit 6c4009
particular group level.  If both this field and @code{header} are zero,
Packit 6c4009
then the child's options aren't grouped together, they are merged with
Packit 6c4009
parent options at the parent option group level.
Packit 6c4009
Packit 6c4009
@end table
Packit 6c4009
@end deftp
Packit 6c4009
Packit 6c4009
@node Argp Flags, Argp Help, Argp Parsers, Argp
Packit 6c4009
@subsection Flags for @code{argp_parse}
Packit 6c4009
Packit 6c4009
The default behavior of @code{argp_parse} is designed to be convenient
Packit 6c4009
for the most common case of parsing program command line argument.  To
Packit 6c4009
modify these defaults, the following flags may be or'd together in the
Packit 6c4009
@var{flags} argument to @code{argp_parse}:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item ARGP_PARSE_ARGV0
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Don't ignore the first element of the @var{argv} argument to
Packit 6c4009
@code{argp_parse}.  Unless @code{ARGP_NO_ERRS} is set, the first element
Packit 6c4009
of the argument vector is skipped for option parsing purposes, as it
Packit 6c4009
corresponds to the program name in a command line.
Packit 6c4009
Packit 6c4009
@item ARGP_NO_ERRS
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Don't print error messages for unknown options to @code{stderr}; unless
Packit 6c4009
this flag is set, @code{ARGP_PARSE_ARGV0} is ignored, as @code{argv[0]}
Packit 6c4009
is used as the program name in the error messages.  This flag implies
Packit 6c4009
@code{ARGP_NO_EXIT}.  This is based on the assumption that silent exiting
Packit 6c4009
upon errors is bad behavior.
Packit 6c4009
Packit 6c4009
@item ARGP_NO_ARGS
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Don't parse any non-option args.  Normally these are parsed by calling
Packit 6c4009
the parse functions with a key of @code{ARGP_KEY_ARG}, the actual
Packit 6c4009
argument being the value.  This flag needn't normally be set, as the
Packit 6c4009
default behavior is to stop parsing as soon as an argument fails to be
Packit 6c4009
parsed.  @xref{Argp Parser Functions}.
Packit 6c4009
Packit 6c4009
@item ARGP_IN_ORDER
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Parse options and arguments in the same order they occur on the command
Packit 6c4009
line.  Normally they're rearranged so that all options come first.
Packit 6c4009
Packit 6c4009
@item ARGP_NO_HELP
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Don't provide the standard long option @samp{--help}, which ordinarily
Packit 6c4009
causes usage and option help information to be output to @code{stdout}
Packit 6c4009
and @code{exit (0)}.
Packit 6c4009
Packit 6c4009
@item ARGP_NO_EXIT
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Don't exit on errors, although they may still result in error messages.
Packit 6c4009
Packit 6c4009
@item ARGP_LONG_ONLY
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Use the GNU getopt `long-only' rules for parsing arguments.  This allows
Packit 6c4009
long-options to be recognized with only a single @samp{-}
Packit 6c4009
(i.e., @samp{-help}).  This results in a less useful interface, and its
Packit 6c4009
use is discouraged as it conflicts with the way most GNU programs work
Packit 6c4009
as well as the GNU coding standards.
Packit 6c4009
Packit 6c4009
@item ARGP_SILENT
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Turns off any message-printing/exiting options, specifically
Packit 6c4009
@code{ARGP_NO_EXIT}, @code{ARGP_NO_ERRS}, and @code{ARGP_NO_HELP}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Argp Help Filtering, , Argp Children, Argp Parsers
Packit 6c4009
@need 2000
Packit 6c4009
@subsection Customizing Argp Help Output
Packit 6c4009
Packit 6c4009
The @code{help_filter} field in a @code{struct argp} is a pointer to a
Packit 6c4009
function that filters the text of help messages before displaying
Packit 6c4009
them.  They have a function signature like:
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
char *@var{help-filter} (int @var{key}, const char *@var{text}, void *@var{input})
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
Packit 6c4009
@noindent
Packit 6c4009
Where @var{key} is either a key from an option, in which case @var{text}
Packit 6c4009
is that option's help text.  @xref{Argp Option Vectors}.  Alternately, one
Packit 6c4009
of the special keys with names beginning with @samp{ARGP_KEY_HELP_}
Packit 6c4009
might be used, describing which other help text @var{text} will contain.
Packit 6c4009
@xref{Argp Help Filter Keys}.
Packit 6c4009
Packit 6c4009
The function should return either @var{text} if it remains as-is, or a
Packit 6c4009
replacement string allocated using @code{malloc}.  This will be either be
Packit 6c4009
freed by argp or zero, which prints nothing.  The value of @var{text} is
Packit 6c4009
supplied @emph{after} any translation has been done, so if any of the
Packit 6c4009
replacement text needs translation, it will be done by the filter
Packit 6c4009
function.  @var{input} is either the input supplied to @code{argp_parse}
Packit 6c4009
or it is zero, if @code{argp_help} was called directly by the user.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Keys: Argp Help Filter Keys.  Special @var{key} values for help filter functions.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argp Help Filter Keys, , , Argp Help Filtering
Packit 6c4009
@subsubsection Special Keys for Argp Help Filter Functions
Packit 6c4009
Packit 6c4009
The following special values may be passed to an argp help filter
Packit 6c4009
function as the first argument in addition to key values for user
Packit 6c4009
options.  They specify which help text the @var{text} argument contains:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item ARGP_KEY_HELP_PRE_DOC
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The help text preceding options.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_HELP_POST_DOC
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The help text following options.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_HELP_HEADER
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The option header string.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_HELP_EXTRA
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This is used after all other documentation; @var{text} is zero for this key.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_HELP_DUP_ARGS_NOTE
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The explanatory note printed when duplicate option arguments have been suppressed.
Packit 6c4009
Packit 6c4009
@item ARGP_KEY_HELP_ARGS_DOC
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The argument doc string; formally the @code{args_doc} field from the argp parser.  @xref{Argp Parsers}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Argp Help, Argp Examples, Argp Flags, Argp
Packit 6c4009
@subsection The @code{argp_help} Function
Packit 6c4009
Packit 6c4009
Normally programs using argp need not be written with particular
Packit 6c4009
printing argument-usage-type help messages in mind as the standard
Packit 6c4009
@samp{--help} option is handled automatically by argp.  Typical error
Packit 6c4009
cases can be handled using @code{argp_usage} and
Packit 6c4009
@code{argp_error}.  @xref{Argp Helper Functions}.  However, if it's
Packit 6c4009
desirable to print a help message in some context other than parsing the
Packit 6c4009
program options, argp offers the @code{argp_help} interface.
Packit 6c4009
Packit 6c4009
@deftypefun void argp_help (const struct argp *@var{argp}, FILE *@var{stream}, unsigned @var{flags}, char *@var{name})
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
@safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
Packit 6c4009
@c Just calls _help.
Packit 6c4009
This outputs a help message for the argp parser @var{argp} to
Packit 6c4009
@var{stream}.  The type of messages printed will be determined by
Packit 6c4009
@var{flags}.
Packit 6c4009
Packit 6c4009
Any options such as @samp{--help} that are implemented automatically by
Packit 6c4009
argp itself will @emph{not} be present in the help output; for this
Packit 6c4009
reason it is best to use @code{argp_state_help} if calling from within
Packit 6c4009
an argp parser function.  @xref{Argp Helper Functions}.
Packit 6c4009
@end deftypefun
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* Flags: Argp Help Flags.       Specifying what sort of help message to print.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argp Help Flags, , , Argp Help
Packit 6c4009
@subsection Flags for the @code{argp_help} Function
Packit 6c4009
Packit 6c4009
When calling @code{argp_help} (@pxref{Argp Help}) or
Packit 6c4009
@code{argp_state_help} (@pxref{Argp Helper Functions}) the exact output
Packit 6c4009
is determined by the @var{flags} argument.  This should consist of any of
Packit 6c4009
the following flags, or'd together:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item ARGP_HELP_USAGE
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
A unix @samp{Usage:} message that explicitly lists all options.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_SHORT_USAGE
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
A unix @samp{Usage:} message that displays an appropriate placeholder to
Packit 6c4009
indicate where the options go; useful for showing the non-option
Packit 6c4009
argument syntax.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_SEE
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
A @samp{Try @dots{} for more help} message; @samp{@dots{}} contains the
Packit 6c4009
program name and @samp{--help}.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_LONG
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
A verbose option help message that gives each option available along
Packit 6c4009
with its documentation string.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_PRE_DOC
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The part of the argp parser doc string preceding the verbose option help.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_POST_DOC
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
The part of the argp parser doc string that following the verbose option help.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_DOC
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
@code{(ARGP_HELP_PRE_DOC | ARGP_HELP_POST_DOC)}
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_BUG_ADDR
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
A message that prints where to report bugs for this program, if the
Packit 6c4009
@code{argp_program_bug_address} variable contains this information.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_LONG_ONLY
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This will modify any output to reflect the @code{ARGP_LONG_ONLY} mode.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
The following flags are only understood when used with
Packit 6c4009
@code{argp_state_help}.  They control whether the function returns after
Packit 6c4009
printing its output, or terminates the program:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item ARGP_HELP_EXIT_ERR
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This will terminate the program with @code{exit (argp_err_exit_status)}.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_EXIT_OK
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This will terminate the program with @code{exit (0)}.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
The following flags are combinations of the basic flags for printing
Packit 6c4009
standard messages:
Packit 6c4009
Packit 6c4009
@vtable @code
Packit 6c4009
@item ARGP_HELP_STD_ERR
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
Assuming that an error message for a parsing error has printed, this
Packit 6c4009
prints a message on how to get help, and terminates the program with an
Packit 6c4009
error.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_STD_USAGE
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This prints a standard usage message and terminates the program with an
Packit 6c4009
error.  This is used when no other specific error messages are
Packit 6c4009
appropriate or available.
Packit 6c4009
Packit 6c4009
@item ARGP_HELP_STD_HELP
Packit 6c4009
@standards{GNU, argp.h}
Packit 6c4009
This prints the standard response for a @samp{--help} option, and
Packit 6c4009
terminates the program successfully.
Packit 6c4009
@end vtable
Packit 6c4009
Packit 6c4009
@node Argp Examples, Argp User Customization, Argp Help, Argp
Packit 6c4009
@subsection Argp Examples
Packit 6c4009
Packit 6c4009
These example programs demonstrate the basic usage of argp.
Packit 6c4009
Packit 6c4009
@menu
Packit 6c4009
* 1: Argp Example 1.            A minimal program using argp.
Packit 6c4009
* 2: Argp Example 2.            A program using only default options.
Packit 6c4009
* 3: Argp Example 3.            A simple program with user options.
Packit 6c4009
* 4: Argp Example 4.            Combining multiple argp parsers.
Packit 6c4009
@end menu
Packit 6c4009
Packit 6c4009
@node Argp Example 1, Argp Example 2, , Argp Examples
Packit 6c4009
@subsubsection A Minimal Program Using Argp
Packit 6c4009
Packit 6c4009
This is perhaps the smallest program possible that uses argp.  It won't
Packit 6c4009
do much except give an error message and exit when there are any
Packit 6c4009
arguments, and prints a rather pointless message for @samp{--help}.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include argp-ex1.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Argp Example 2, Argp Example 3, Argp Example 1, Argp Examples
Packit 6c4009
@subsubsection A Program Using Argp with Only Default Options
Packit 6c4009
Packit 6c4009
This program doesn't use any options or arguments, it uses argp to be
Packit 6c4009
compliant with the GNU standard command line format.
Packit 6c4009
Packit 6c4009
In addition to giving no arguments and implementing a @samp{--help}
Packit 6c4009
option, this example has a @samp{--version} option, which will put the
Packit 6c4009
given documentation string and bug address in the @samp{--help} output,
Packit 6c4009
as per GNU standards.
Packit 6c4009
Packit 6c4009
The variable @code{argp} contains the argument parser
Packit 6c4009
specification.  Adding fields to this structure is the way most
Packit 6c4009
parameters are passed to @code{argp_parse}.  The first three fields are
Packit 6c4009
normally used, but they are not in this small program.  There are also
Packit 6c4009
two global variables that argp can use defined here,
Packit 6c4009
@code{argp_program_version} and @code{argp_program_bug_address}.  They
Packit 6c4009
are considered global variables because they will almost always be
Packit 6c4009
constant for a given program, even if they use different argument
Packit 6c4009
parsers for various tasks.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include argp-ex2.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Argp Example 3, Argp Example 4, Argp Example 2, Argp Examples
Packit 6c4009
@subsubsection A Program Using Argp with User Options
Packit 6c4009
Packit 6c4009
This program uses the same features as example 2, adding user options
Packit 6c4009
and arguments.
Packit 6c4009
Packit 6c4009
We now use the first four fields in @code{argp} (@pxref{Argp Parsers})
Packit 6c4009
and specify @code{parse_opt} as the parser function.  @xref{Argp Parser
Packit 6c4009
Functions}.
Packit 6c4009
Packit 6c4009
Note that in this example, @code{main} uses a structure to communicate
Packit 6c4009
with the @code{parse_opt} function, a pointer to which it passes in the
Packit 6c4009
@code{input} argument to @code{argp_parse}.  @xref{Argp}.  It is retrieved
Packit 6c4009
by @code{parse_opt} through the @code{input} field in its @code{state}
Packit 6c4009
argument.  @xref{Argp Parsing State}.  Of course, it's also possible to
Packit 6c4009
use global variables instead, but using a structure like this is
Packit 6c4009
somewhat more flexible and clean.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include argp-ex3.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Argp Example 4, , Argp Example 3, Argp Examples
Packit 6c4009
@subsubsection A Program Using Multiple Combined Argp Parsers
Packit 6c4009
Packit 6c4009
This program uses the same features as example 3, but has more options,
Packit 6c4009
and presents more structure in the @samp{--help} output.  It also
Packit 6c4009
illustrates how you can `steal' the remainder of the input arguments
Packit 6c4009
past a certain point for programs that accept a list of items.  It also
Packit 6c4009
illustrates the @var{key} value @code{ARGP_KEY_NO_ARGS}, which is only
Packit 6c4009
given if no non-option arguments were supplied to the
Packit 6c4009
program.  @xref{Argp Special Keys}.
Packit 6c4009
Packit 6c4009
For structuring help output, two features are used: @emph{headers} and a
Packit 6c4009
two part option string.  The @emph{headers} are entries in the options
Packit 6c4009
vector.  @xref{Argp Option Vectors}.  The first four fields are zero.  The
Packit 6c4009
two part documentation string are in the variable @code{doc}, which
Packit 6c4009
allows documentation both before and after the options.  @xref{Argp
Packit 6c4009
Parsers}, the two parts of @code{doc} are separated by a vertical-tab
Packit 6c4009
character (@code{'\v'}, or @code{'\013'}).  By convention, the
Packit 6c4009
documentation before the options is a short string stating what the
Packit 6c4009
program does, and after any options it is longer, describing the
Packit 6c4009
behavior in more detail.  All documentation strings are automatically
Packit 6c4009
filled for output, although newlines may be included to force a line
Packit 6c4009
break at a particular point.  In addition, documentation strings are
Packit 6c4009
passed to the @code{gettext} function, for possible translation into the
Packit 6c4009
current locale.
Packit 6c4009
Packit 6c4009
@smallexample
Packit 6c4009
@include argp-ex4.c.texi
Packit 6c4009
@end smallexample
Packit 6c4009
Packit 6c4009
@node Argp User Customization, , Argp Examples, Argp
Packit 6c4009
@subsection Argp User Customization
Packit 6c4009
Packit 6c4009
@cindex ARGP_HELP_FMT environment variable
Packit 6c4009
The formatting of argp @samp{--help} output may be controlled to some
Packit 6c4009
extent by a program's users, by setting the @code{ARGP_HELP_FMT}
Packit 6c4009
environment variable to a comma-separated list of tokens.  Whitespace is
Packit 6c4009
ignored:
Packit 6c4009
Packit 6c4009
@table @samp
Packit 6c4009
@item dup-args
Packit 6c4009
@itemx no-dup-args
Packit 6c4009
These turn @dfn{duplicate-argument-mode} on or off.  In duplicate
Packit 6c4009
argument mode, if an option that accepts an argument has multiple names,
Packit 6c4009
the argument is shown for each name.  Otherwise, it is only shown for the
Packit 6c4009
first long option.  A note is subsequently printed so the user knows that
Packit 6c4009
it applies to other names as well.  The default is @samp{no-dup-args},
Packit 6c4009
which is less consistent, but prettier.
Packit 6c4009
Packit 6c4009
@item dup-args-note
Packit 6c4009
@item no-dup-args-note
Packit 6c4009
These will enable or disable the note informing the user of suppressed
Packit 6c4009
option argument duplication.  The default is @samp{dup-args-note}.
Packit 6c4009
Packit 6c4009
@item short-opt-col=@var{n}
Packit 6c4009
This prints the first short option in column @var{n}.  The default is 2.
Packit 6c4009
Packit 6c4009
@item long-opt-col=@var{n}
Packit 6c4009
This prints the first long option in column @var{n}.  The default is 6.
Packit 6c4009
Packit 6c4009
@item doc-opt-col=@var{n}
Packit 6c4009
This prints `documentation options' (@pxref{Argp Option Flags}) in
Packit 6c4009
column @var{n}.  The default is 2.
Packit 6c4009
Packit 6c4009
@item opt-doc-col=@var{n}
Packit 6c4009
This prints the documentation for options starting in column
Packit 6c4009
@var{n}.  The default is 29.
Packit 6c4009
Packit 6c4009
@item header-col=@var{n}
Packit 6c4009
This will indent the group headers that document groups of options to
Packit 6c4009
column @var{n}.  The default is 1.
Packit 6c4009
Packit 6c4009
@item usage-indent=@var{n}
Packit 6c4009
This will indent continuation lines in @samp{Usage:} messages to column
Packit 6c4009
@var{n}.  The default is 12.
Packit 6c4009
Packit 6c4009
@item rmargin=@var{n}
Packit 6c4009
This will word wrap help output at or before column @var{n}.  The default
Packit 6c4009
is 79.
Packit 6c4009
@end table