S390: Ifunc resolver macro for vector instructions.
[glibc.git] / manual / argp.texi
blobbe1d158f2269f97b686dc76f6d06ffd435b3b160
1 @node Argp, Suboptions, Getopt, Parsing Program Arguments
2 @need 5000
3 @section Parsing Program Options with Argp
4 @cindex argp (program argument parser)
5 @cindex argument parsing with argp
6 @cindex option parsing with argp
8 @dfn{Argp} is an interface for parsing unix-style argument vectors.
9 @xref{Program Arguments}.
11 Argp provides features unavailable in the more commonly used
12 @code{getopt} interface.  These features include automatically producing
13 output in response to the @samp{--help} and @samp{--version} options, as
14 described in the GNU coding standards.  Using argp makes it less likely
15 that programmers will neglect to implement these additional options or
16 keep them up to date.
18 Argp also provides the ability to merge several independently defined
19 option parsers into one, mediating conflicts between them and making the
20 result appear seamless.  A library can export an argp option parser that
21 user programs might employ in conjunction with their own option parsers,
22 resulting in less work for the user programs.  Some programs may use only
23 argument parsers exported by libraries, thereby achieving consistent and
24 efficient option-parsing for abstractions implemented by the libraries.
26 @pindex argp.h
27 The header file @file{<argp.h>} should be included to use argp.
29 @subsection The @code{argp_parse} Function
31 The main interface to argp is the @code{argp_parse} function.  In many
32 cases, calling @code{argp_parse} is the only argument-parsing code
33 needed in @code{main}.
34 @xref{Program Arguments}.
36 @comment argp.h
37 @comment GNU
38 @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})
39 @safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtslocale{} @mtsenv{}}@asunsafe{@ascuheap{} @ascuintl{} @asulock{} @asucorrupt{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
40 @c Optionally alloca()tes standard help options, initializes the parser,
41 @c then parses individual args in a loop, and then finalizes.
42 @c  parser_init
43 @c   calc_sizes ok
44 @c    option_is_end ok
45 @c   malloc @ascuheap @acsmem
46 @c   parser_convert @mtslocale
47 @c    convert_options @mtslocale
48 @c     option_is_end ok
49 @c     option_is_short ok
50 @c      isprint, but locale may change within the loop
51 @c     find_long_option ok
52 @c   group_parse
53 @c    group->parser (from argp->parser)
54 @c  parser_parse_next
55 @c   getopt_long(_only)_r many issues, same as non_r minus @mtasurace
56 @c   parser_parse_arg
57 @c    group_parse dup
58 @c   parser_parse_opt
59 @c    group_parse dup
60 @c    argp_error dup @mtasurace:argpbuf @mtsenv @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
61 @c    dgettext (bad key error) dup @mtsenv @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsfd @acsmem
62 @c  parser_finalize
63 @c   group_parse
64 @c   fprintf dup @mtslocale @asucorrupt @aculock @acucorrupt [no @ascuheap @acsmem]
65 @c   dgettext dup @mtsenv @asucorrupt @ascuheap @asulock @ascudlopen @acucorrupt @aculock @acsfd @acsmem
66 @c   arg_state_help
67 @c   free dup @ascuhelp @acsmem
68 The @code{argp_parse} function parses the arguments in @var{argv}, of
69 length @var{argc}, using the argp parser @var{argp}.  @xref{Argp
70 Parsers}.  Passing a null pointer for @var{argp} is the same as using
71 a @code{struct argp} containing all zeros.
73 @var{flags} is a set of flag bits that modify the parsing behavior.
74 @xref{Argp Flags}.  @var{input} is passed through to the argp parser
75 @var{argp}, and has meaning defined by @var{argp}.  A typical usage is
76 to pass a pointer to a structure which is used for specifying
77 parameters to the parser and passing back the results.
79 Unless the @code{ARGP_NO_EXIT} or @code{ARGP_NO_HELP} flags are included
80 in @var{flags}, calling @code{argp_parse} may result in the program
81 exiting.  This behavior is true if an error is detected, or when an
82 unknown option is encountered.  @xref{Program Termination}.
84 If @var{arg_index} is non-null, the index of the first unparsed option
85 in @var{argv} is returned as a value.
87 The return value is zero for successful parsing, or an error code
88 (@pxref{Error Codes}) if an error is detected.  Different argp parsers
89 may return arbitrary error codes, but the standard error codes are:
90 @code{ENOMEM} if a memory allocation error occurred, or @code{EINVAL} if
91 an unknown option or option argument is encountered.
92 @end deftypefun
94 @menu
95 * Globals: Argp Global Variables.  Global argp parameters.
96 * Parsers: Argp Parsers.        Defining parsers for use with @code{argp_parse}.
97 * Flags: Argp Flags.            Flags that modify the behavior of @code{argp_parse}.
98 * Help: Argp Help.              Printing help messages when not parsing.
99 * Examples: Argp Examples.      Simple examples of programs using argp.
100 * Customization: Argp User Customization.
101                                 Users may control the @samp{--help} output format.
102 @end menu
104 @node Argp Global Variables, Argp Parsers, , Argp
105 @subsection Argp Global Variables
107 These variables make it easy for user programs to implement the
108 @samp{--version} option and provide a bug-reporting address in the
109 @samp{--help} output.  These are implemented in argp by default.
111 @comment argp.h
112 @comment GNU
113 @deftypevar {const char *} argp_program_version
114 If defined or set by the user program to a non-zero value, then a
115 @samp{--version} option is added when parsing with @code{argp_parse},
116 which will print the @samp{--version} string followed by a newline and
117 exit.  The exception to this is if the @code{ARGP_NO_EXIT} flag is used.
118 @end deftypevar
120 @comment argp.h
121 @comment GNU
122 @deftypevar {const char *} argp_program_bug_address
123 If defined or set by the user program to a non-zero value,
124 @code{argp_program_bug_address} should point to a string that will be
125 printed at the end of the standard output for the @samp{--help} option,
126 embedded in a sentence that says @samp{Report bugs to @var{address}.}.
127 @end deftypevar
129 @need 1500
130 @comment argp.h
131 @comment GNU
132 @defvar argp_program_version_hook
133 If defined or set by the user program to a non-zero value, a
134 @samp{--version} option is added when parsing with @code{arg_parse},
135 which prints the program version and exits with a status of zero.  This
136 is not the case if the @code{ARGP_NO_HELP} flag is used.  If the
137 @code{ARGP_NO_EXIT} flag is set, the exit behavior of the program is
138 suppressed or modified, as when the argp parser is going to be used by
139 other programs.
141 It should point to a function with this type of signature:
143 @smallexample
144 void @var{print-version} (FILE *@var{stream}, struct argp_state *@var{state})
145 @end smallexample
147 @noindent
148 @xref{Argp Parsing State}, for an explanation of @var{state}.
150 This variable takes precedence over @code{argp_program_version}, and is
151 useful if a program has version information not easily expressed in a
152 simple string.
153 @end defvar
155 @comment argp.h
156 @comment GNU
157 @deftypevar error_t argp_err_exit_status
158 This is the exit status used when argp exits due to a parsing error.  If
159 not defined or set by the user program, this defaults to:
160 @code{EX_USAGE} from @file{<sysexits.h>}.
161 @end deftypevar
163 @node Argp Parsers, Argp Flags, Argp Global Variables, Argp
164 @subsection Specifying Argp Parsers
166 The first argument to the @code{argp_parse} function is a pointer to a
167 @code{struct argp}, which is known as an @dfn{argp parser}:
169 @comment argp.h
170 @comment GNU
171 @deftp {Data Type} {struct argp}
172 This structure specifies how to parse a given set of options and
173 arguments, perhaps in conjunction with other argp parsers.  It has the
174 following fields:
176 @table @code
177 @item const struct argp_option *options
178 A pointer to a vector of @code{argp_option} structures specifying which
179 options this argp parser understands; it may be zero if there are no
180 options at all.  @xref{Argp Option Vectors}.
182 @item argp_parser_t parser
183 A pointer to a function that defines actions for this parser; it is
184 called for each option parsed, and at other well-defined points in the
185 parsing process.  A value of zero is the same as a pointer to a function
186 that always returns @code{ARGP_ERR_UNKNOWN}.  @xref{Argp Parser
187 Functions}.
189 @item const char *args_doc
190 If non-zero, a string describing what non-option arguments are called by
191 this parser.  This is only used to print the @samp{Usage:} message.  If
192 it contains newlines, the strings separated by them are considered
193 alternative usage patterns and printed on separate lines.  Lines after
194 the first are prefixed by @samp{ or: } instead of @samp{Usage:}.
196 @item const char *doc
197 If non-zero, a string containing extra text to be printed before and
198 after the options in a long help message, with the two sections
199 separated by a vertical tab (@code{'\v'}, @code{'\013'}) character.  By
200 convention, the documentation before the options is just a short string
201 explaining what the program does.  Documentation printed after the
202 options describe behavior in more detail.
204 @item const struct argp_child *children
205 A pointer to a vector of @code{argp_children} structures.  This pointer
206 specifies which additional argp parsers should be combined with this
207 one.  @xref{Argp Children}.
209 @item char *(*help_filter)(int @var{key}, const char *@var{text}, void *@var{input})
210 If non-zero, a pointer to a function that filters the output of help
211 messages.  @xref{Argp Help Filtering}.
213 @item const char *argp_domain
214 If non-zero, the strings used in the argp library are translated using
215 the domain described by this string.  If zero, the current default domain
216 is used.
218 @end table
219 @end deftp
221 Of the above group, @code{options}, @code{parser}, @code{args_doc}, and
222 the @code{doc} fields are usually all that are needed.  If an argp
223 parser is defined as an initialized C variable, only the fields used
224 need be specified in the initializer.  The rest will default to zero due
225 to the way C structure initialization works.  This design is exploited in
226 most argp structures; the most-used fields are grouped near the
227 beginning, the unused fields left unspecified.
229 @menu
230 * Options: Argp Option Vectors.   Specifying options in an argp parser.
231 * Argp Parser Functions::         Defining actions for an argp parser.
232 * Children: Argp Children.        Combining multiple argp parsers.
233 * Help Filtering: Argp Help Filtering.  Customizing help output for an argp parser.
234 @end menu
236 @node Argp Option Vectors, Argp Parser Functions, Argp Parsers, Argp Parsers
237 @subsection Specifying Options in an Argp Parser
239 The @code{options} field in a @code{struct argp} points to a vector of
240 @code{struct argp_option} structures, each of which specifies an option
241 that the argp parser supports.  Multiple entries may be used for a single
242 option provided it has multiple names.  This should be terminated by an
243 entry with zero in all fields.  Note that when using an initialized C
244 array for options, writing @code{@{ 0 @}} is enough to achieve this.
246 @comment argp.h
247 @comment GNU
248 @deftp {Data Type} {struct argp_option}
249 This structure specifies a single option that an argp parser
250 understands, as well as how to parse and document that option.  It has
251 the following fields:
253 @table @code
254 @item const char *name
255 The long name for this option, corresponding to the long option
256 @samp{--@var{name}}; this field may be zero if this option @emph{only}
257 has a short name.  To specify multiple names for an option, additional
258 entries may follow this one, with the @code{OPTION_ALIAS} flag
259 set.  @xref{Argp Option Flags}.
261 @item int key
262 The integer key provided by the current option to the option parser.  If
263 @var{key} has a value that is a printable @sc{ascii} character (i.e.,
264 @code{isascii (@var{key})} is true), it @emph{also} specifies a short
265 option @samp{-@var{char}}, where @var{char} is the @sc{ascii} character
266 with the code @var{key}.
268 @item const char *arg
269 If non-zero, this is the name of an argument associated with this
270 option, which must be provided (e.g., with the
271 @samp{--@var{name}=@var{value}} or @samp{-@var{char} @var{value}}
272 syntaxes), unless the @code{OPTION_ARG_OPTIONAL} flag (@pxref{Argp
273 Option Flags}) is set, in which case it @emph{may} be provided.
275 @item int flags
276 Flags associated with this option, some of which are referred to above.
277 @xref{Argp Option Flags}.
279 @item const char *doc
280 A documentation string for this option, for printing in help messages.
282 If both the @code{name} and @code{key} fields are zero, this string
283 will be printed tabbed left from the normal option column, making it
284 useful as a group header.  This will be the first thing printed in its
285 group.  In this usage, it's conventional to end the string with a
286 @samp{:} character.
288 @item int group
289 Group identity for this option.
291 In a long help message, options are sorted alphabetically within each
292 group, and the groups presented in the order 0, 1, 2, @dots{}, @var{n},
293 @minus{}@var{m}, @dots{}, @minus{}2, @minus{}1.
295 Every entry in an options array with this field 0 will inherit the group
296 number of the previous entry, or zero if it's the first one.  If it's a
297 group header with @code{name} and @code{key} fields both zero, the
298 previous entry + 1 is the default.  Automagic options such as
299 @samp{--help} are put into group @minus{}1.
301 Note that because of C structure initialization rules, this field often
302 need not be specified, because 0 is the correct value.
303 @end table
304 @end deftp
307 @menu
308 * Flags: Argp Option Flags.     Flags for options.
309 @end menu
311 @node Argp Option Flags, , , Argp Option Vectors
312 @subsubsection Flags for Argp Options
314 The following flags may be or'd together in the @code{flags} field of a
315 @code{struct argp_option}.  These flags control various aspects of how
316 that option is parsed or displayed in help messages:
319 @vtable @code
320 @comment argp.h
321 @comment GNU
322 @item OPTION_ARG_OPTIONAL
323 The argument associated with this option is optional.
325 @comment argp.h
326 @comment GNU
327 @item OPTION_HIDDEN
328 This option isn't displayed in any help messages.
330 @comment argp.h
331 @comment GNU
332 @item OPTION_ALIAS
333 This option is an alias for the closest previous non-alias option.  This
334 means that it will be displayed in the same help entry, and will inherit
335 fields other than @code{name} and @code{key} from the option being
336 aliased.
339 @comment argp.h
340 @comment GNU
341 @item OPTION_DOC
342 This option isn't actually an option and should be ignored by the actual
343 option parser.  It is an arbitrary section of documentation that should
344 be displayed in much the same manner as the options.  This is known as a
345 @dfn{documentation option}.
347 If this flag is set, then the option @code{name} field is displayed
348 unmodified (e.g., no @samp{--} prefix is added) at the left-margin where
349 a @emph{short} option would normally be displayed, and this
350 documentation string is left in it's usual place.  For purposes of
351 sorting, any leading whitespace and punctuation is ignored, unless the
352 first non-whitespace character is @samp{-}.  This entry is displayed
353 after all options, after @code{OPTION_DOC} entries with a leading
354 @samp{-}, in the same group.
356 @comment argp.h
357 @comment GNU
358 @item OPTION_NO_USAGE
359 This option shouldn't be included in `long' usage messages, but should
360 still be included in other help messages.  This is intended for options
361 that are completely documented in an argp's @code{args_doc}
362 field.  @xref{Argp Parsers}.  Including this option in the generic usage
363 list would be redundant, and should be avoided.
365 For instance, if @code{args_doc} is @code{"FOO BAR\n-x BLAH"}, and the
366 @samp{-x} option's purpose is to distinguish these two cases, @samp{-x}
367 should probably be marked @code{OPTION_NO_USAGE}.
368 @end vtable
370 @node Argp Parser Functions, Argp Children, Argp Option Vectors, Argp Parsers
371 @subsection Argp Parser Functions
373 The function pointed to by the @code{parser} field in a @code{struct
374 argp} (@pxref{Argp Parsers}) defines what actions take place in response
375 to each option or argument parsed.  It is also used as a hook, allowing a
376 parser to perform tasks at certain other points during parsing.
378 @need 2000
379 Argp parser functions have the following type signature:
381 @cindex argp parser functions
382 @smallexample
383 error_t @var{parser} (int @var{key}, char *@var{arg}, struct argp_state *@var{state})
384 @end smallexample
386 @noindent
387 where the arguments are as follows:
389 @table @var
390 @item key
391 For each option that is parsed, @var{parser} is called with a value of
392 @var{key} from that option's @code{key} field in the option
393 vector.  @xref{Argp Option Vectors}.  @var{parser} is also called at
394 other times with special reserved keys, such as @code{ARGP_KEY_ARG} for
395 non-option arguments.  @xref{Argp Special Keys}.
397 @item arg
398 If @var{key} is an option, @var{arg} is its given value.  This defaults
399 to zero if no value is specified.  Only options that have a non-zero
400 @code{arg} field can ever have a value.  These must @emph{always} have a
401 value unless the @code{OPTION_ARG_OPTIONAL} flag is specified.  If the
402 input being parsed specifies a value for an option that doesn't allow
403 one, an error results before @var{parser} ever gets called.
405 If @var{key} is @code{ARGP_KEY_ARG}, @var{arg} is a non-option
406 argument.  Other special keys always have a zero @var{arg}.
408 @item state
409 @var{state} points to a @code{struct argp_state}, containing useful
410 information about the current parsing state for use by
411 @var{parser}.  @xref{Argp Parsing State}.
412 @end table
414 When @var{parser} is called, it should perform whatever action is
415 appropriate for @var{key}, and return @code{0} for success,
416 @code{ARGP_ERR_UNKNOWN} if the value of @var{key} is not handled by this
417 parser function, or a unix error code if a real error
418 occurred.  @xref{Error Codes}.
420 @comment argp.h
421 @comment GNU
422 @deftypevr Macro int ARGP_ERR_UNKNOWN
423 Argp parser functions should return @code{ARGP_ERR_UNKNOWN} for any
424 @var{key} value they do not recognize, or for non-option arguments
425 (@code{@var{key} == ARGP_KEY_ARG}) that they are not equipped to handle.
426 @end deftypevr
428 @need 3000
429 A typical parser function uses a switch statement on @var{key}:
431 @smallexample
432 error_t
433 parse_opt (int key, char *arg, struct argp_state *state)
435   switch (key)
436     @{
437     case @var{option_key}:
438       @var{action}
439       break;
440     @dots{}
441     default:
442       return ARGP_ERR_UNKNOWN;
443     @}
444   return 0;
446 @end smallexample
448 @menu
449 * Keys: Argp Special Keys.           Special values for the @var{key} argument.
450 * State: Argp Parsing State.         What the @var{state} argument refers to.
451 * Functions: Argp Helper Functions.  Functions to help during argp parsing.
452 @end menu
454 @node Argp Special Keys, Argp Parsing State, , Argp Parser Functions
455 @subsubsection Special Keys for Argp Parser Functions
457 In addition to key values corresponding to user options, the @var{key}
458 argument to argp parser functions may have a number of other special
459 values.  In the following example @var{arg} and @var{state} refer to
460 parser function arguments.  @xref{Argp Parser Functions}.
462 @vtable @code
463 @comment argp.h
464 @comment GNU
465 @item ARGP_KEY_ARG
466 This is not an option at all, but rather a command line argument, whose
467 value is pointed to by @var{arg}.
469 When there are multiple parser functions in play due to argp parsers
470 being combined, it's impossible to know which one will handle a specific
471 argument.  Each is called until one returns 0 or an error other than
472 @code{ARGP_ERR_UNKNOWN}; if an argument is not handled,
473 @code{argp_parse} immediately returns success, without parsing any more
474 arguments.
476 Once a parser function returns success for this key, that fact is
477 recorded, and the @code{ARGP_KEY_NO_ARGS} case won't be
478 used.  @emph{However}, if while processing the argument a parser function
479 decrements the @code{next} field of its @var{state} argument, the option
480 won't be considered processed; this is to allow you to actually modify
481 the argument, perhaps into an option, and have it processed again.
483 @comment argp.h
484 @comment GNU
485 @item ARGP_KEY_ARGS
486 If a parser function returns @code{ARGP_ERR_UNKNOWN} for
487 @code{ARGP_KEY_ARG}, it is immediately called again with the key
488 @code{ARGP_KEY_ARGS}, which has a similar meaning, but is slightly more
489 convenient for consuming all remaining arguments.  @var{arg} is 0, and
490 the tail of the argument vector may be found at @code{@var{state}->argv
491 + @var{state}->next}.  If success is returned for this key, and
492 @code{@var{state}->next} is unchanged, all remaining arguments are
493 considered to have been consumed.  Otherwise, the amount by which
494 @code{@var{state}->next} has been adjusted indicates how many were used.
495 Here's an example that uses both, for different args:
498 @smallexample
499 @dots{}
500 case ARGP_KEY_ARG:
501   if (@var{state}->arg_num == 0)
502     /* First argument */
503     first_arg = @var{arg};
504   else
505     /* Let the next case parse it.  */
506     return ARGP_KEY_UNKNOWN;
507   break;
508 case ARGP_KEY_ARGS:
509   remaining_args = @var{state}->argv + @var{state}->next;
510   num_remaining_args = @var{state}->argc - @var{state}->next;
511   break;
512 @end smallexample
514 @comment argp.h
515 @comment GNU
516 @item ARGP_KEY_END
517 This indicates that there are no more command line arguments.  Parser
518 functions are called in a different order, children first.  This allows
519 each parser to clean up its state for the parent.
521 @comment argp.h
522 @comment GNU
523 @item ARGP_KEY_NO_ARGS
524 Because it's common to do some special processing if there aren't any
525 non-option args, parser functions are called with this key if they
526 didn't successfully process any non-option arguments.  This is called
527 just before @code{ARGP_KEY_END}, where more general validity checks on
528 previously parsed arguments take place.
530 @comment argp.h
531 @comment GNU
532 @item ARGP_KEY_INIT
533 This is passed in before any parsing is done.  Afterwards, the values of
534 each element of the @code{child_input} field of @var{state}, if any, are
535 copied to each child's state to be the initial value of the @code{input}
536 when @emph{their} parsers are called.
538 @comment argp.h
539 @comment GNU
540 @item ARGP_KEY_SUCCESS
541 Passed in when parsing has successfully been completed, even if
542 arguments remain.
544 @comment argp.h
545 @comment GNU
546 @item ARGP_KEY_ERROR
547 Passed in if an error has occurred and parsing is terminated.  In this
548 case a call with a key of @code{ARGP_KEY_SUCCESS} is never made.
550 @comment argp.h
551 @comment GNU
552 @item ARGP_KEY_FINI
553 The final key ever seen by any parser, even after
554 @code{ARGP_KEY_SUCCESS} and @code{ARGP_KEY_ERROR}.  Any resources
555 allocated by @code{ARGP_KEY_INIT} may be freed here.  At times, certain
556 resources allocated are to be returned to the caller after a successful
557 parse.  In that case, those particular resources can be freed in the
558 @code{ARGP_KEY_ERROR} case.
559 @end vtable
561 In all cases, @code{ARGP_KEY_INIT} is the first key seen by parser
562 functions, and @code{ARGP_KEY_FINI} the last, unless an error was
563 returned by the parser for @code{ARGP_KEY_INIT}.  Other keys can occur
564 in one the following orders.  @var{opt} refers to an arbitrary option
565 key:
567 @table @asis
568 @item @var{opt}@dots{} @code{ARGP_KEY_NO_ARGS} @code{ARGP_KEY_END} @code{ARGP_KEY_SUCCESS}
569 The arguments being parsed did not contain any non-option arguments.
571 @item ( @var{opt} | @code{ARGP_KEY_ARG} )@dots{} @code{ARGP_KEY_END} @code{ARGP_KEY_SUCCESS}
572 All non-option arguments were successfully handled by a parser
573 function.  There may be multiple parser functions if multiple argp
574 parsers were combined.
576 @item ( @var{opt} | @code{ARGP_KEY_ARG} )@dots{} @code{ARGP_KEY_SUCCESS}
577 Some non-option argument went unrecognized.
579 This occurs when every parser function returns @code{ARGP_KEY_UNKNOWN}
580 for an argument, in which case parsing stops at that argument if
581 @var{arg_index} is a null pointer.  Otherwise an error occurs.
582 @end table
584 In all cases, if a non-null value for @var{arg_index} gets passed to
585 @code{argp_parse}, the index of the first unparsed command-line argument
586 is passed back in that value.
588 If an error occurs and is either detected by argp or because a parser
589 function returned an error value, each parser is called with
590 @code{ARGP_KEY_ERROR}.  No further calls are made, except the final call
591 with @code{ARGP_KEY_FINI}.
593 @node Argp Parsing State, Argp Helper Functions, Argp Special Keys, Argp Parser Functions
594 @subsubsection Argp Parsing State
596 The third argument to argp parser functions (@pxref{Argp Parser
597 Functions}) is a pointer to a @code{struct argp_state}, which contains
598 information about the state of the option parsing.
600 @comment argp.h
601 @comment GNU
602 @deftp {Data Type} {struct argp_state}
603 This structure has the following fields, which may be modified as noted:
605 @table @code
606 @item const struct argp *const root_argp
607 The top level argp parser being parsed.  Note that this is often
608 @emph{not} the same @code{struct argp} passed into @code{argp_parse} by
609 the invoking program.  @xref{Argp}.  It is an internal argp parser that
610 contains options implemented by @code{argp_parse} itself, such as
611 @samp{--help}.
613 @item int argc
614 @itemx char **argv
615 The argument vector being parsed.  This may be modified.
617 @item int next
618 The index in @code{argv} of the next argument to be parsed.  This may be
619 modified.
621 One way to consume all remaining arguments in the input is to set
622 @code{@var{state}->next = @var{state}->argc}, perhaps after recording
623 the value of the @code{next} field to find the consumed arguments.  The
624 current option can be re-parsed immediately by decrementing this field,
625 then modifying @code{@var{state}->argv[@var{state}->next]} to reflect
626 the option that should be reexamined.
628 @item unsigned flags
629 The flags supplied to @code{argp_parse}.  These may be modified, although
630 some flags may only take effect when @code{argp_parse} is first
631 invoked.  @xref{Argp Flags}.
633 @item unsigned arg_num
634 While calling a parsing function with the @var{key} argument
635 @code{ARGP_KEY_ARG}, this represents the number of the current arg,
636 starting at 0.  It is incremented after each @code{ARGP_KEY_ARG} call
637 returns.  At all other times, this is the number of @code{ARGP_KEY_ARG}
638 arguments that have been processed.
640 @item int quoted
641 If non-zero, the index in @code{argv} of the first argument following a
642 special @samp{--} argument.  This prevents anything that follows from
643 being interpreted as an option.  It is only set after argument parsing
644 has proceeded past this point.
646 @item void *input
647 An arbitrary pointer passed in from the caller of @code{argp_parse}, in
648 the @var{input} argument.
650 @item void **child_inputs
651 These are values that will be passed to child parsers.  This vector will
652 be the same length as the number of children in the current parser.  Each
653 child parser will be given the value of
654 @code{@var{state}->child_inputs[@var{i}]} as @emph{its}
655 @code{@var{state}->input} field, where @var{i} is the index of the child
656 in the this parser's @code{children} field.  @xref{Argp Children}.
658 @item void *hook
659 For the parser function's use.  Initialized to 0, but otherwise ignored
660 by argp.
662 @item char *name
663 The name used when printing messages.  This is initialized to
664 @code{argv[0]}, or @code{program_invocation_name} if @code{argv[0]} is
665 unavailable.
667 @item FILE *err_stream
668 @itemx FILE *out_stream
669 The stdio streams used when argp prints.  Error messages are printed to
670 @code{err_stream}, all other output, such as @samp{--help} output) to
671 @code{out_stream}.  These are initialized to @code{stderr} and
672 @code{stdout} respectively.  @xref{Standard Streams}.
674 @item void *pstate
675 Private, for use by the argp implementation.
676 @end table
677 @end deftp
679 @node Argp Helper Functions, , Argp Parsing State, Argp Parser Functions
680 @subsubsection Functions For Use in Argp Parsers
682 Argp provides a number of functions available to the user of argp
683 (@pxref{Argp Parser Functions}), mostly for producing error messages.
684 These take as their first argument the @var{state} argument to the
685 parser function.  @xref{Argp Parsing State}.
688 @cindex usage messages, in argp
689 @comment argp.h
690 @comment GNU
691 @deftypefun void argp_usage (const struct argp_state *@var{state})
692 @safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
693 @c Just calls argp_state_help with stderr and ARGP_HELP_STD_USAGE.
694 Outputs the standard usage message for the argp parser referred to by
695 @var{state} to @code{@var{state}->err_stream} and terminate the program
696 with @code{exit (argp_err_exit_status)}.  @xref{Argp Global Variables}.
697 @end deftypefun
699 @cindex syntax error messages, in argp
700 @comment argp.h
701 @comment GNU
702 @deftypefun void argp_error (const struct argp_state *@var{state}, const char *@var{fmt}, @dots{})
703 @safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
704 @c Lock stream, vasprintf the formatted message into a buffer, print the
705 @c buffer prefixed by the short program name (in libc,
706 @c argp_short_program_name is a macro that expands to
707 @c program_invocation_short_name), releases the buffer, then call
708 @c argp_state_help with stream and ARGP_HELP_STD_ERR, unlocking the
709 @c stream at the end.
710 Prints the printf format string @var{fmt} and following args, preceded
711 by the program name and @samp{:}, and followed by a @w{@samp{Try @dots{}
712 --help}} message, and terminates the program with an exit status of
713 @code{argp_err_exit_status}.  @xref{Argp Global Variables}.
714 @end deftypefun
716 @cindex error messages, in argp
717 @comment argp.h
718 @comment GNU
719 @deftypefun void argp_failure (const struct argp_state *@var{state}, int @var{status}, int @var{errnum}, const char *@var{fmt}, @dots{})
720 @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{}}}
721 @c Lock stream, write out the short program name, vasprintf the optional
722 @c formatted message to a buffer, print the buffer prefixed by colon and
723 @c blank, release the buffer, call strerror_r with an automatic buffer,
724 @c print it out after colon and blank, put[w]c a line break, unlock the
725 @c stream, then exit unless ARGP_NO_EXIT.
726 Similar to the standard gnu error-reporting function @code{error}, this
727 prints the program name and @samp{:}, the printf format string
728 @var{fmt}, and the appropriate following args.  If it is non-zero, the
729 standard unix error text for @var{errnum} is printed.  If @var{status} is
730 non-zero, it terminates the program with that value as its exit status.
732 The difference between @code{argp_failure} and @code{argp_error} is that
733 @code{argp_error} is for @emph{parsing errors}, whereas
734 @code{argp_failure} is for other problems that occur during parsing but
735 don't reflect a syntactic problem with the input, such as illegal values
736 for options, bad phase of the moon, etc.
737 @end deftypefun
739 @comment argp.h
740 @comment GNU
741 @deftypefun void argp_state_help (const struct argp_state *@var{state}, FILE *@var{stream}, unsigned @var{flags})
742 @safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
743 @c Just calls _help with the short program name and optionally exit.
744 @c The main problems in _help, besides the usual issues with stream I/O
745 @c and translation, are the use of a static buffer (uparams, thus
746 @c @mtasurace:argpbuf) that makes the whole thing thread-unsafe, reading
747 @c from the environment for ARGP_HELP_FMT, accessing the locale object
748 @c multiple times.
750 @c _help @mtsenv @mtasurace:argpbuf @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
751 @c  dgettext @ascuintl
752 @c  flockfile @aculock
753 @c  funlockfile @aculock
754 @c  fill_in_uparams @mtsenv @mtasurace:argpbuf @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
755 @c   argp_failure dup (status = errnum = 0)
756 @c   atoi dup @mtslocale
757 @c  argp_hol @ascuheap @acsmem
758 @c   make_hol @ascuheap @acsmem
759 @c   hol_add_cluster @ascuheap @acsmem
760 @c   hol_append @ascuheap @acsmem
761 @c  hol_set_group ok
762 @c   hol_find_entry ok
763 @c  hol_sort @mtslocale @acucorrupt
764 @c   qsort dup @acucorrupt
765 @c    hol_entry_qcmp @mtslocale
766 @c     hol_entry_cmp @mtslocale
767 @c      group_cmp ok
768 @c      hol_cluster_cmp ok
769 @c       group_cmp ok
770 @c      hol_entry_first_short @mtslocale
771 @c       hol_entry_short_iterate [@mtslocale]
772 @c        until_short ok
773 @c         oshort ok
774 @c          isprint ok
775 @c      odoc ok
776 @c      hol_entry_first_long ok
777 @c      canon_doc_option @mtslocale
778 @c      tolower dup
779 @c  hol_usage @mtslocale @ascuintl @ascuheap @acsmem
780 @c   hol_entry_short_iterate ok
781 @c    add_argless_short_opt ok
782 @c   argp_fmtstream_printf dup
783 @c   hol_entry_short_iterate @mtslocale @ascuintl @ascuheap @acsmem
784 @c    usage_argful_short_opt @mtslocale @ascuintl @ascuheap @acsmem
785 @c     dgettext dup
786 @c     argp_fmtstream_printf dup
787 @c   hol_entry_long_iterate @mtslocale @ascuintl @ascuheap @acsmem
788 @c    usage_long_opt @mtslocale @ascuintl @ascuheap @acsmem
789 @c     dgettext dup
790 @c     argp_fmtstream_printf dup
791 @c  hol_help @mtslocale @mtasurace:argpbuf @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
792 @c   hol_entry_help @mtslocale @mtasurace:argpbuf @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
793 @c    argp_fmtstream_set_lmargin dup
794 @c    argp_fmtstream_wmargin dup
795 @c    argp_fmtstream_set_wmargin dup
796 @c    comma @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
797 @c     argp_fmtstream_putc dup
798 @c     hol_cluster_is_child ok
799 @c     argp_fmtstream_wmargin dup
800 @c     print_header dup
801 @c     argp_fmtstream_set_wmargin dup
802 @c     argp_fmtstream_puts dup
803 @c     indent_to dup
804 @c    argp_fmtstream_putc dup
805 @c    arg @mtslocale @ascuheap @acsmem
806 @c     argp_fmtstream_printf dup
807 @c    odoc dup
808 @c    argp_fmtstream_puts dup
809 @c    argp_fmtstream_printf dup
810 @c    print_header @mtslocale @mtasurace:argpbuf @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
811 @c     dgettext dup
812 @c     filter_doc dup
813 @c     argp_fmtstream_putc dup
814 @c     indent_to dup
815 @c     argp_fmtstream_set_lmargin dup
816 @c     argp_fmtstream_set_wmargin dup
817 @c     argp_fmtstream_puts dup
818 @c     free dup
819 @c    filter_doc dup
820 @c    argp_fmtstream_point dup
821 @c    indent_to @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
822 @c     argp_fmtstream_point dup
823 @c     argp_fmtstream_putc dup
824 @c   dgettext dup
825 @c   filter_doc dup
826 @c   argp_fmtstream_putc dup
827 @c   argp_fmtstream_puts dup
828 @c   free dup
829 @c  hol_free @ascuheap @acsmem
830 @c   free dup
831 @c  argp_args_levels ok
832 @c  argp_args_usage @mtslocale @ascuintl @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
833 @c   dgettext dup
834 @c   filter_doc ok
835 @c    argp_input ok
836 @c    argp->help_filter
837 @c   space @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
838 @c    argp_fmtstream_point dup
839 @c    argp_fmtstream_rmargin @mtslocale @asucorrupt @acucorrupt @aculock
840 @c     argp_fmtstream_update dup
841 @c    argp_fmtstream_putc dup
842 @c   argp_fmtstream_write dup
843 @c   free dup
844 @c  argp_doc @mtslocale @ascuheap @ascuintl @asucorrupt @acsmem @acucorrupt @aculock
845 @c   dgettext @ascuintl
846 @c   strndup @ascuheap @acsmem
847 @c   argp_input dup
848 @c   argp->help_filter
849 @c   argp_fmtstream_putc @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
850 @c    argp_fmtstream_ensure dup
851 @c   argp_fmtstream_write dup
852 @c   argp_fmtstream_puts dup
853 @c   argp_fmtstream_point @mtslocale @asucorrupt @acucorrupt @aculock
854 @c    argp_fmtstream_update dup
855 @c   argp_fmtstream_lmargin dup
856 @c   free dup
857 @c  argp_make_fmtstream @ascuheap @acsmem
858 @c  argp_fmtstream_free @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
859 @c   argp_fmtstream_update @mtslocale @asucorrupt @acucorrupt @aculock
860 @c    put[w]c_unlocked dup
861 @c    isblank in loop @mtslocale
862 @c    fxprintf @aculock
863 @c   fxprintf @aculock
864 @c   free dup
865 @c  argp_fmtstream_set_wmargin @mtslocale @asucorrupt @acucorrupt @aculock
866 @c   argp_fmtstream_update dup
867 @c  argp_fmtstream_printf @mtslocale @ascuheap @acsmem
868 @c   argp_fmtstream_ensure dup
869 @c   vsnprintf dup
870 @c  argp_fmtstream_set_lmargin @mtslocale @asucorrupt @acucorrupt @aculock
871 @c   argp_fmtstream_update dup
872 @c  argp_fmtstream_puts @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
873 @c   argp_fmtstream_write @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
874 @c    argp_fmtstream_ensure @mtslocale @ascuheap @asucorrupt @acsmem @acucorrupt @aculock
875 @c     argp_fmtstream_update dup
876 @c     fxprintf @aculock
877 @c     realloc @ascuheap @acsmem
878 Outputs a help message for the argp parser referred to by @var{state},
879 to @var{stream}.  The @var{flags} argument determines what sort of help
880 message is produced.  @xref{Argp Help Flags}.
881 @end deftypefun
883 Error output is sent to @code{@var{state}->err_stream}, and the program
884 name printed is @code{@var{state}->name}.
886 The output or program termination behavior of these functions may be
887 suppressed if the @code{ARGP_NO_EXIT} or @code{ARGP_NO_ERRS} flags are
888 passed to @code{argp_parse}.  @xref{Argp Flags}.
890 This behavior is useful if an argp parser is exported for use by other
891 programs (e.g., by a library), and may be used in a context where it is
892 not desirable to terminate the program in response to parsing errors.  In
893 argp parsers intended for such general use, and for the case where the
894 program @emph{doesn't} terminate, calls to any of these functions should
895 be followed by code that returns the appropriate error code:
897 @smallexample
898 if (@var{bad argument syntax})
899   @{
900      argp_usage (@var{state});
901      return EINVAL;
902   @}
903 @end smallexample
905 @noindent
906 If a parser function will @emph{only} be used when @code{ARGP_NO_EXIT}
907 is not set, the return may be omitted.
909 @node Argp Children, Argp Help Filtering, Argp Parser Functions, Argp Parsers
910 @subsection Combining Multiple Argp Parsers
912 The @code{children} field in a @code{struct argp} enables other argp
913 parsers to be combined with the referencing one for the parsing of a
914 single set of arguments.  This field should point to a vector of
915 @code{struct argp_child}, which is terminated by an entry having a value
916 of zero in the @code{argp} field.
918 Where conflicts between combined parsers arise, as when two specify an
919 option with the same name, the parser conflicts are resolved in favor of
920 the parent argp parser(s), or the earlier of the argp parsers in the
921 list of children.
923 @comment argp.h
924 @comment GNU
925 @deftp {Data Type} {struct argp_child}
926 An entry in the list of subsidiary argp parsers pointed to by the
927 @code{children} field in a @code{struct argp}.  The fields are as
928 follows:
930 @table @code
931 @item const struct argp *argp
932 The child argp parser, or zero to end of the list.
934 @item int flags
935 Flags for this child.
937 @item const char *header
938 If non-zero, this is an optional header to be printed within help output
939 before the child options.  As a side-effect, a non-zero value forces the
940 child options to be grouped together.  To achieve this effect without
941 actually printing a header string, use a value of @code{""}.  As with
942 header strings specified in an option entry, the conventional value of
943 the last character is @samp{:}.  @xref{Argp Option Vectors}.
945 @item int group
946 This is where the child options are grouped relative to the other
947 `consolidated' options in the parent argp parser.  The values are the
948 same as the @code{group} field in @code{struct argp_option}.  @xref{Argp
949 Option Vectors}.  All child-groupings follow parent options at a
950 particular group level.  If both this field and @code{header} are zero,
951 then the child's options aren't grouped together, they are merged with
952 parent options at the parent option group level.
954 @end table
955 @end deftp
957 @node Argp Flags, Argp Help, Argp Parsers, Argp
958 @subsection Flags for @code{argp_parse}
960 The default behavior of @code{argp_parse} is designed to be convenient
961 for the most common case of parsing program command line argument.  To
962 modify these defaults, the following flags may be or'd together in the
963 @var{flags} argument to @code{argp_parse}:
965 @vtable @code
966 @comment argp.h
967 @comment GNU
968 @item ARGP_PARSE_ARGV0
969 Don't ignore the first element of the @var{argv} argument to
970 @code{argp_parse}.  Unless @code{ARGP_NO_ERRS} is set, the first element
971 of the argument vector is skipped for option parsing purposes, as it
972 corresponds to the program name in a command line.
974 @comment argp.h
975 @comment GNU
976 @item ARGP_NO_ERRS
977 Don't print error messages for unknown options to @code{stderr}; unless
978 this flag is set, @code{ARGP_PARSE_ARGV0} is ignored, as @code{argv[0]}
979 is used as the program name in the error messages.  This flag implies
980 @code{ARGP_NO_EXIT}.  This is based on the assumption that silent exiting
981 upon errors is bad behavior.
983 @comment argp.h
984 @comment GNU
985 @item ARGP_NO_ARGS
986 Don't parse any non-option args.  Normally these are parsed by calling
987 the parse functions with a key of @code{ARGP_KEY_ARG}, the actual
988 argument being the value.  This flag needn't normally be set, as the
989 default behavior is to stop parsing as soon as an argument fails to be
990 parsed.  @xref{Argp Parser Functions}.
992 @comment argp.h
993 @comment GNU
994 @item ARGP_IN_ORDER
995 Parse options and arguments in the same order they occur on the command
996 line.  Normally they're rearranged so that all options come first.
998 @comment argp.h
999 @comment GNU
1000 @item ARGP_NO_HELP
1001 Don't provide the standard long option @samp{--help}, which ordinarily
1002 causes usage and option help information to be output to @code{stdout}
1003 and @code{exit (0)}.
1005 @comment argp.h
1006 @comment GNU
1007 @item ARGP_NO_EXIT
1008 Don't exit on errors, although they may still result in error messages.
1010 @comment argp.h
1011 @comment GNU
1012 @item ARGP_LONG_ONLY
1013 Use the gnu getopt `long-only' rules for parsing arguments.  This allows
1014 long-options to be recognized with only a single @samp{-}
1015 (i.e., @samp{-help}).  This results in a less useful interface, and its
1016 use is discouraged as it conflicts with the way most GNU programs work
1017 as well as the GNU coding standards.
1019 @comment argp.h
1020 @comment GNU
1021 @item ARGP_SILENT
1022 Turns off any message-printing/exiting options, specifically
1023 @code{ARGP_NO_EXIT}, @code{ARGP_NO_ERRS}, and @code{ARGP_NO_HELP}.
1024 @end vtable
1026 @node Argp Help Filtering, , Argp Children, Argp Parsers
1027 @need 2000
1028 @subsection Customizing Argp Help Output
1030 The @code{help_filter} field in a @code{struct argp} is a pointer to a
1031 function that filters the text of help messages before displaying
1032 them.  They have a function signature like:
1034 @smallexample
1035 char *@var{help-filter} (int @var{key}, const char *@var{text}, void *@var{input})
1036 @end smallexample
1039 @noindent
1040 Where @var{key} is either a key from an option, in which case @var{text}
1041 is that option's help text.  @xref{Argp Option Vectors}.  Alternately, one
1042 of the special keys with names beginning with @samp{ARGP_KEY_HELP_}
1043 might be used, describing which other help text @var{text} will contain.
1044 @xref{Argp Help Filter Keys}.
1046 The function should return either @var{text} if it remains as-is, or a
1047 replacement string allocated using @code{malloc}.  This will be either be
1048 freed by argp or zero, which prints nothing.  The value of @var{text} is
1049 supplied @emph{after} any translation has been done, so if any of the
1050 replacement text needs translation, it will be done by the filter
1051 function.  @var{input} is either the input supplied to @code{argp_parse}
1052 or it is zero, if @code{argp_help} was called directly by the user.
1054 @menu
1055 * Keys: Argp Help Filter Keys.  Special @var{key} values for help filter functions.
1056 @end menu
1058 @node Argp Help Filter Keys, , , Argp Help Filtering
1059 @subsubsection Special Keys for Argp Help Filter Functions
1061 The following special values may be passed to an argp help filter
1062 function as the first argument in addition to key values for user
1063 options.  They specify which help text the @var{text} argument contains:
1065 @vtable @code
1066 @comment argp.h
1067 @comment GNU
1068 @item ARGP_KEY_HELP_PRE_DOC
1069 The help text preceding options.
1071 @comment argp.h
1072 @comment GNU
1073 @item ARGP_KEY_HELP_POST_DOC
1074 The help text following options.
1076 @comment argp.h
1077 @comment GNU
1078 @item ARGP_KEY_HELP_HEADER
1079 The option header string.
1081 @comment argp.h
1082 @comment GNU
1083 @item ARGP_KEY_HELP_EXTRA
1084 This is used after all other documentation; @var{text} is zero for this key.
1086 @comment argp.h
1087 @comment GNU
1088 @item ARGP_KEY_HELP_DUP_ARGS_NOTE
1089 The explanatory note printed when duplicate option arguments have been suppressed.
1091 @comment argp.h
1092 @comment GNU
1093 @item ARGP_KEY_HELP_ARGS_DOC
1094 The argument doc string; formally the @code{args_doc} field from the argp parser.  @xref{Argp Parsers}.
1095 @end vtable
1097 @node Argp Help, Argp Examples, Argp Flags, Argp
1098 @subsection The @code{argp_help} Function
1100 Normally programs using argp need not be written with particular
1101 printing argument-usage-type help messages in mind as the standard
1102 @samp{--help} option is handled automatically by argp.  Typical error
1103 cases can be handled using @code{argp_usage} and
1104 @code{argp_error}.  @xref{Argp Helper Functions}.  However, if it's
1105 desirable to print a help message in some context other than parsing the
1106 program options, argp offers the @code{argp_help} interface.
1108 @comment argp.h
1109 @comment GNU
1110 @deftypefun void argp_help (const struct argp *@var{argp}, FILE *@var{stream}, unsigned @var{flags}, char *@var{name})
1111 @safety{@prelim{}@mtunsafe{@mtasurace{:argpbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascuheap{} @ascuintl{} @asucorrupt{}}@acunsafe{@acsmem{} @acucorrupt{} @aculock{}}}
1112 @c Just calls _help.
1113 This outputs a help message for the argp parser @var{argp} to
1114 @var{stream}.  The type of messages printed will be determined by
1115 @var{flags}.
1117 Any options such as @samp{--help} that are implemented automatically by
1118 argp itself will @emph{not} be present in the help output; for this
1119 reason it is best to use @code{argp_state_help} if calling from within
1120 an argp parser function.  @xref{Argp Helper Functions}.
1121 @end deftypefun
1123 @menu
1124 * Flags: Argp Help Flags.       Specifying what sort of help message to print.
1125 @end menu
1127 @node Argp Help Flags, , , Argp Help
1128 @subsection Flags for the @code{argp_help} Function
1130 When calling @code{argp_help} (@pxref{Argp Help}) or
1131 @code{argp_state_help} (@pxref{Argp Helper Functions}) the exact output
1132 is determined by the @var{flags} argument.  This should consist of any of
1133 the following flags, or'd together:
1135 @vtable @code
1136 @item ARGP_HELP_USAGE
1137 A unix @samp{Usage:} message that explicitly lists all options.
1139 @item ARGP_HELP_SHORT_USAGE
1140 A unix @samp{Usage:} message that displays an appropriate placeholder to
1141 indicate where the options go; useful for showing the non-option
1142 argument syntax.
1144 @item ARGP_HELP_SEE
1145 A @samp{Try @dots{} for more help} message; @samp{@dots{}} contains the
1146 program name and @samp{--help}.
1148 @item ARGP_HELP_LONG
1149 A verbose option help message that gives each option available along
1150 with its documentation string.
1152 @item ARGP_HELP_PRE_DOC
1153 The part of the argp parser doc string preceding the verbose option help.
1155 @item ARGP_HELP_POST_DOC
1156 The part of the argp parser doc string that following the verbose option help.
1158 @item ARGP_HELP_DOC
1159 @code{(ARGP_HELP_PRE_DOC | ARGP_HELP_POST_DOC)}
1161 @item ARGP_HELP_BUG_ADDR
1162 A message that prints where to report bugs for this program, if the
1163 @code{argp_program_bug_address} variable contains this information.
1165 @item ARGP_HELP_LONG_ONLY
1166 This will modify any output to reflect the @code{ARGP_LONG_ONLY} mode.
1167 @end vtable
1169 The following flags are only understood when used with
1170 @code{argp_state_help}.  They control whether the function returns after
1171 printing its output, or terminates the program:
1173 @vtable @code
1174 @item ARGP_HELP_EXIT_ERR
1175 This will terminate the program with @code{exit (argp_err_exit_status)}.
1177 @item ARGP_HELP_EXIT_OK
1178 This will terminate the program with @code{exit (0)}.
1179 @end vtable
1181 The following flags are combinations of the basic flags for printing
1182 standard messages:
1184 @vtable @code
1185 @item ARGP_HELP_STD_ERR
1186 Assuming that an error message for a parsing error has printed, this
1187 prints a message on how to get help, and terminates the program with an
1188 error.
1190 @item ARGP_HELP_STD_USAGE
1191 This prints a standard usage message and terminates the program with an
1192 error.  This is used when no other specific error messages are
1193 appropriate or available.
1195 @item ARGP_HELP_STD_HELP
1196 This prints the standard response for a @samp{--help} option, and
1197 terminates the program successfully.
1198 @end vtable
1200 @node Argp Examples, Argp User Customization, Argp Help, Argp
1201 @subsection Argp Examples
1203 These example programs demonstrate the basic usage of argp.
1205 @menu
1206 * 1: Argp Example 1.            A minimal program using argp.
1207 * 2: Argp Example 2.            A program using only default options.
1208 * 3: Argp Example 3.            A simple program with user options.
1209 * 4: Argp Example 4.            Combining multiple argp parsers.
1210 @end menu
1212 @node Argp Example 1, Argp Example 2, , Argp Examples
1213 @subsubsection A Minimal Program Using Argp
1215 This is perhaps the smallest program possible that uses argp.  It won't
1216 do much except give an error messages and exit when there are any
1217 arguments, and prints a rather pointless message for @samp{--help}.
1219 @smallexample
1220 @include argp-ex1.c.texi
1221 @end smallexample
1223 @node Argp Example 2, Argp Example 3, Argp Example 1, Argp Examples
1224 @subsubsection A Program Using Argp with Only Default Options
1226 This program doesn't use any options or arguments, it uses argp to be
1227 compliant with the GNU standard command line format.
1229 In addition to giving no arguments and implementing a @samp{--help}
1230 option, this example has a @samp{--version} option, which will put the
1231 given documentation string and bug address in the @samp{--help} output,
1232 as per GNU standards.
1234 The variable @code{argp} contains the argument parser
1235 specification.  Adding fields to this structure is the way most
1236 parameters are passed to @code{argp_parse}.  The first three fields are
1237 normally used, but they are not in this small program.  There are also
1238 two global variables that argp can use defined here,
1239 @code{argp_program_version} and @code{argp_program_bug_address}.  They
1240 are considered global variables because they will almost always be
1241 constant for a given program, even if they use different argument
1242 parsers for various tasks.
1244 @smallexample
1245 @include argp-ex2.c.texi
1246 @end smallexample
1248 @node Argp Example 3, Argp Example 4, Argp Example 2, Argp Examples
1249 @subsubsection A Program Using Argp with User Options
1251 This program uses the same features as example 2, adding user options
1252 and arguments.
1254 We now use the first four fields in @code{argp} (@pxref{Argp Parsers})
1255 and specify @code{parse_opt} as the parser function.  @xref{Argp Parser
1256 Functions}.
1258 Note that in this example, @code{main} uses a structure to communicate
1259 with the @code{parse_opt} function, a pointer to which it passes in the
1260 @code{input} argument to @code{argp_parse}.  @xref{Argp}.  It is retrieved
1261 by @code{parse_opt} through the @code{input} field in its @code{state}
1262 argument.  @xref{Argp Parsing State}.  Of course, it's also possible to
1263 use global variables instead, but using a structure like this is
1264 somewhat more flexible and clean.
1266 @smallexample
1267 @include argp-ex3.c.texi
1268 @end smallexample
1270 @node Argp Example 4, , Argp Example 3, Argp Examples
1271 @subsubsection A Program Using Multiple Combined Argp Parsers
1273 This program uses the same features as example 3, but has more options,
1274 and presents more structure in the @samp{--help} output.  It also
1275 illustrates how you can `steal' the remainder of the input arguments
1276 past a certain point for programs that accept a list of items.  It also
1277 illustrates the @var{key} value @code{ARGP_KEY_NO_ARGS}, which is only
1278 given if no non-option arguments were supplied to the
1279 program.  @xref{Argp Special Keys}.
1281 For structuring help output, two features are used: @emph{headers} and a
1282 two part option string.  The @emph{headers} are entries in the options
1283 vector.  @xref{Argp Option Vectors}.  The first four fields are zero.  The
1284 two part documentation string are in the variable @code{doc}, which
1285 allows documentation both before and after the options.  @xref{Argp
1286 Parsers}, the two parts of @code{doc} are separated by a vertical-tab
1287 character (@code{'\v'}, or @code{'\013'}).  By convention, the
1288 documentation before the options is a short string stating what the
1289 program does, and after any options it is longer, describing the
1290 behavior in more detail.  All documentation strings are automatically
1291 filled for output, although newlines may be included to force a line
1292 break at a particular point.  In addition, documentation strings are
1293 passed to the @code{gettext} function, for possible translation into the
1294 current locale.
1296 @smallexample
1297 @include argp-ex4.c.texi
1298 @end smallexample
1300 @node Argp User Customization, , Argp Examples, Argp
1301 @subsection Argp User Customization
1303 @cindex ARGP_HELP_FMT environment variable
1304 The formatting of argp @samp{--help} output may be controlled to some
1305 extent by a program's users, by setting the @code{ARGP_HELP_FMT}
1306 environment variable to a comma-separated list of tokens.  Whitespace is
1307 ignored:
1309 @table @samp
1310 @item dup-args
1311 @itemx no-dup-args
1312 These turn @dfn{duplicate-argument-mode} on or off.  In duplicate
1313 argument mode, if an option that accepts an argument has multiple names,
1314 the argument is shown for each name.  Otherwise, it is only shown for the
1315 first long option.  A note is subsequently printed so the user knows that
1316 it applies to other names as well.  The default is @samp{no-dup-args},
1317 which is less consistent, but prettier.
1319 @item dup-args-note
1320 @item no-dup-args-note
1321 These will enable or disable the note informing the user of suppressed
1322 option argument duplication.  The default is @samp{dup-args-note}.
1324 @item short-opt-col=@var{n}
1325 This prints the first short option in column @var{n}.  The default is 2.
1327 @item long-opt-col=@var{n}
1328 This prints the first long option in column @var{n}.  The default is 6.
1330 @item doc-opt-col=@var{n}
1331 This prints `documentation options' (@pxref{Argp Option Flags}) in
1332 column @var{n}.  The default is 2.
1334 @item opt-doc-col=@var{n}
1335 This prints the documentation for options starting in column
1336 @var{n}.  The default is 29.
1338 @item header-col=@var{n}
1339 This will indent the group headers that document groups of options to
1340 column @var{n}.  The default is 1.
1342 @item usage-indent=@var{n}
1343 This will indent continuation lines in @samp{Usage:} messages to column
1344 @var{n}.  The default is 12.
1346 @item rmargin=@var{n}
1347 This will word wrap help output at or before column @var{n}.  The default
1348 is 79.
1349 @end table