Update.
[platform/upstream/glibc.git] / manual / argp.texi
1 @ignore
2    Documentation for the argp argument parser
3
4    Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6    Written by Miles Bader <miles@gnu.ai.mit.edu>.
7
8    The GNU C Library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Library General Public License as
10    published by the Free Software Foundation; either version 2 of the
11    License, or (at your option) any later version.
12
13    The GNU C Library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Library General Public License for more details.
17
18    You should have received a copy of the GNU Library General Public
19    License along with the GNU C Library; see the file COPYING.LIB.  If not,
20    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22 @end ignore
23
24 @node Argp, Suboptions, Getopt, Parsing Program Arguments
25 @need 5000
26 @section Parsing Program Options with Argp
27 @cindex argp (program argument parser)
28 @cindex argument parsing with argp
29 @cindex option parsing with argp
30
31 @dfn{Argp} is an interface for parsing unix-style argument vectors
32 (@pxref{Program Arguments}).
33
34 Unlike the more common @code{getopt} interface, it provides many related
35 convenience features in addition to parsing options, such as
36 automatically producing output in response to @samp{--help} and
37 @samp{--version} options (as defined by the GNU coding standards).
38 Doing these things in argp results in a more consistent look for
39 programs that use it, and makes less likely that implementors will
40 neglect to implement them or keep them up-to-date.
41
42 Argp also provides the ability to merge several independently defined
43 option parsers into one, mediating conflicts between them, and making
44 the result appear seamless.  A library can export an argp option parser,
45 which programs can easily use in conjunction with their own option
46 parser.  This results in less work for user programs (indeed, some may
47 use only argument parsers exported by libraries, and have no options of
48 their own), and more consistent option-parsing for the abstractions
49 implemented by the library.
50
51 @pindex argp.h
52 The header file @file{<argp.h>} should be included to use argp.
53
54 @subsection The @code{argp_parse} Function
55
56 The main interface to argp is the @code{argp_parse} function; often, a
57 call to @code{argp_parse} is the only argument-parsing code needed in
58 @code{main} (@pxref{Program Arguments}).
59
60 @comment argp.h
61 @comment GNU
62 @deftypefun {error_t} argp_parse (const struct argp *@var{argp}, @w{int @var{argc}, char **@var{argv}}, @w{unsigned @var{flags}}, @w{int *@var{arg_index}}, @w{void *@var{input}})
63 The @code{argp_parse} function parses the arguments in in @var{argv}, of
64 length @var{argc}, using the argp parser @var{argp} (@pxref{Argp
65 Parsers}); a value of zero is the same as a @code{struct argp}
66 containing all zeros.  @var{flags} is a set of flag bits that modify the
67 parsing behavior (@pxref{Argp Flags}).  @var{input} is passed through to
68 the argp parser @var{argp}, and has meaning defined by it; a typical
69 usage is to pass a pointer to a structure which can be used for
70 specifying parameters to the parser and passing back results from it.
71
72 Unless the @code{ARGP_NO_EXIT} or @code{ARGP_NO_HELP} flags are included
73 in @var{flags}, calling @code{argp_parse} may result in the program
74 exiting---for instance when an unknown option is encountered.
75 @xref{Program Termination}.
76
77 The return value is zero for successful parsing, or a unix error code
78 (@pxref{Error Codes}) if an error was detected.  Different argp parsers
79 may return arbitrary error codes, but standard ones are @code{ENOMEM} if
80 a memory allocation error occurred, or @code{EINVAL} if an unknown option
81 or option argument was encountered.
82 @end deftypefun
83
84 @menu
85 * Globals: Argp Global Variables.  Global argp parameters.
86 * Parsers: Argp Parsers.        Defining parsers for use with @code{argp_parse}.
87 * Flags: Argp Flags.            Flags that modify the behavior of @code{argp_parse}.
88 * Help: Argp Help.              Printing help messages when not parsing.
89 * Examples: Argp Examples.      Simple examples of programs using argp.
90 * Customization: Argp User Customization.
91                                 Users may control the @samp{--help} output format.
92 @end menu
93
94 @node Argp Global Variables, Argp Parsers, , Argp
95 @subsection Argp Global Variables
96
97 These variables make it very easy for every user program to implement
98 the @samp{--version} option and provide a bug-reporting address in the
99 @samp{--help} output (which is implemented by argp regardless).
100
101 @comment argp.h
102 @comment GNU
103 @deftypevar {const char *} argp_program_version
104 If defined or set by the user program to a non-zero value, then a
105 @samp{--version} option is added when parsing with @code{argp_parse}
106 (unless the @code{ARGP_NO_HELP} flag is used), which will print this
107 string followed by a newline and exit (unless the @code{ARGP_NO_EXIT}
108 flag is used).
109 @end deftypevar
110
111 @comment argp.h
112 @comment GNU
113 @deftypevar {const char *} argp_program_bug_address
114 If defined or set by the user program to a non-zero value,
115 @code{argp_program_bug_address} should point to string that is the
116 bug-reporting address for the program.  It will be printed at the end of
117 the standard output for the @samp{--help} option, embedded in a sentence
118 that says something like @samp{Report bugs to @var{address}.}.
119 @end deftypevar
120
121 @need 1500
122 @comment argp.h
123 @comment GNU
124 @defvar argp_program_version_hook
125 If defined or set by the user program to a non-zero value, then a
126 @samp{--version} option is added when parsing with @code{argp_parse}
127 (unless the @code{ARGP_NO_HELP} flag is used), which calls this function
128 to print the version, and then exits with a status of 0 (unless the
129 @code{ARGP_NO_EXIT} flag is used).  It should point to a function with
130 the following type signature:
131
132 @smallexample
133 void @var{print-version} (FILE *@var{stream}, struct argp_state *@var{state})
134 @end smallexample
135
136 @noindent
137 @xref{Argp Parsing State}, for an explanation of @var{state}.
138
139 This variable takes precedent over @code{argp_program_version}, and is
140 useful if a program has version information that cannot be easily
141 specified as a simple string.
142 @end defvar
143
144 @comment argp.h
145 @comment GNU
146 @deftypevar error_t argp_err_exit_status
147 The exit status that argp will use when exiting due to a parsing error.
148 If not defined or set by the user program, this defaults to
149 @code{EX_USAGE} from @file{<sysexits.h>}.
150 @end deftypevar
151
152 @node Argp Parsers, Argp Flags, Argp Global Variables, Argp
153 @subsection Specifying Argp Parsers
154
155 The first argument to the @code{argp_parse} function is a pointer to a
156 @code{struct argp}, which known as an @dfn{argp parser}:
157
158 @comment argp.h
159 @comment GNU
160 @deftp {Data Type} {struct argp}
161 This structure specifies how to parse a given set of options and
162 arguments, perhaps in conjunction with other argp parsers.  It has the
163 following fields:
164
165 @table @code
166 @item const struct argp_option *options
167 A pointer to a vector of @code{argp_option} structures specifying which
168 options this argp parser understands; it may be zero if there are no
169 options at all.  @xref{Argp Option Vectors}.
170
171 @item argp_parser_t parser
172 A pointer to a function that defines actions for this parser; it is
173 called for each option parsed, and at other well-defined points in the
174 parsing process.  A value of zero is the same as a pointer to a
175 function that always returns @code{ARGP_ERR_UNKNOWN}.
176 @xref{Argp Parser Functions}.
177
178 @item const char *args_doc
179 If non-zero, a string describing what non-option arguments are wanted by
180 this parser; it is only used to print the @samp{Usage:} message.  If it
181 contains newlines, the strings separated by them are considered
182 alternative usage patterns, and printed on separate lines (lines after
183 the first are prefix by @samp{ or: } instead of @samp{Usage:}).
184
185 @item const char *doc
186 If non-zero, a string containing extra text to be printed before and
187 after the options in a long help message, with the two sections
188 separated by a vertical tab (@code{'\v'}, @code{'\013'}) character.  By
189 convention, the documentation before the options is just a short string
190 saying what the program does, and that afterwards is longer, describing
191 the behavior in more detail.
192
193 @item const struct argp_child *children
194 A pointer to a vector of @code{argp_children} structures specifying
195 additional argp parsers that should be combined with this one.
196 @xref{Argp Children}.
197
198 @item char *(*help_filter)(int @var{key}, const char *@var{text}, void *@var{input})
199 If non-zero, a pointer to a function to filter the output of help
200 messages.  @xref{Argp Help Filtering}.
201 @end table
202 @end deftp
203
204 The @code{options}, @code{parser}, @code{args_doc}, and @code{doc}
205 fields are usually all that are needed.  If an argp parser is defined as
206 an initialized C variable, only the used fields need be specified in in
207 the initializer---the rest will default to zero due to the way C
208 structure initialization works (this fact is exploited for most argp
209 structures, grouping the most-used fields near the beginning, so that
210 unused fields can simply be left unspecified).
211
212 @menu
213 * Options: Argp Option Vectors.   Specifying options in an argp parser.
214 * Argp Parser Functions::         Defining actions for an argp parser.
215 * Children: Argp Children.        Combining multiple argp parsers.
216 * Help Filtering: Argp Help Filtering.  Customizing help output for an argp parser.
217 @end menu
218
219 @node Argp Option Vectors, Argp Parser Functions, Argp Parsers, Argp Parsers
220 @subsection Specifying Options in an Argp Parser
221
222 The @code{options} field in a @code{struct argp} points to a vector of
223 @code{struct argp_option} structures, each of which specifies an option
224 that argp parser supports (actually, sometimes multiple entries may used
225 for a single option if it has many names).  It should be terminated by
226 an entry with zero in all fields (note that when using an initialized C
227 array for options, writing @code{@{ 0 @}} is enough to achieve this).
228
229 @comment argp.h
230 @comment GNU
231 @deftp {Data Type} {struct argp_option}
232 This structure specifies a single option that an argp parser
233 understands, and how to parse and document it.  It has the following fields:
234
235 @table @code
236 @item const char *name
237 The long name for this option, corresponding to the long option
238 @samp{--@var{name}}; this field can be zero if this option only has a
239 short name.  To specify multiple names for an option, additional entries
240 may follow this one, with the @code{OPTION_ALIAS} flag set (@pxref{Argp
241 Option Flags}).
242
243 @item int key
244 The integer key that is provided to the argp parser's parsing function
245 when this option is being parsed.  Also, if @var{key} has a value that
246 is a printable @sc{ascii} character (i.e., @code{isascii (@var{key})} is
247 true), it @emph{also} specifies a short option @samp{-@var{char}}, where
248 @var{char} is the @sc{ascii} character with the code @var{key}.
249
250 @item const char *arg
251 If non-zero, this is the name of an argument associated with this
252 option, which must be provided (e.g., with the
253 @samp{--@var{name}=@var{value}} or @samp{-@var{char} @var{value}}
254 syntaxes) unless the @code{OPTION_ARG_OPTIONAL} flag (@pxref{Argp Option
255 Flags}) is set, in which case it @emph{may} be provided.
256
257 @item int flags
258 Flags associated with this option (some of which are referred to above).
259 @xref{Argp Option Flags}.
260
261 @item const char *doc
262 A documentation string for this option, for printing in help messages.
263
264 If both the @code{name} and @code{key} fields are zero, this string
265 will be printed out-dented from the normal option column, making it
266 useful as a group header (it will be the first thing printed in its
267 group); in this usage, it's conventional to end the string with a
268 @samp{:} character.
269
270 @item int group
271 The group this option is in.
272
273 In a long help message, options are sorted alphabetically within each
274 group, and the groups presented in the order @math{0, 1, 2,} @dots{},
275 @math{@var{n}, -@var{m},} @dots{}, @math{-2, -1}.  Every entry in an
276 options array with this
277 field 0 will inherit the group number of the previous entry, or zero if
278 it's the first one, unless its a group header (@code{name} and
279 @code{key} fields both zero), in which case, the previous entry
280 @math{@w{} + 1} is
281 the default.  Automagic options such as @samp{--help} are put into group
282 @math{-1}.
283
284 Note that because of C structure initialization rules, this field
285 often need not be specified, because 0 is the right value.
286 @end table
287 @end deftp
288
289 @menu
290 * Flags: Argp Option Flags.     Flags for options.
291 @end menu
292
293 @node Argp Option Flags, , , Argp Option Vectors
294 @subsubsection Flags for Argp Options
295
296 The following flags may be or'd together in the @code{flags} field of a
297 @code{struct argp_option}, and control various aspects of how that
298 option is parsed or displayed in help messages:
299
300 @vtable @code
301 @comment argp.h
302 @comment GNU
303 @item OPTION_ARG_OPTIONAL
304 The argument associated with this option is optional.
305
306 @comment argp.h
307 @comment GNU
308 @item OPTION_HIDDEN
309 This option isn't displayed in any help messages.
310
311 @comment argp.h
312 @comment GNU
313 @item OPTION_ALIAS
314 This option is an alias for the closest previous non-alias option.  This
315 means that it will be displayed in the same help entry, and will inherit
316 fields other than @code{name} and @code{key} from the aliased option.
317
318 @comment argp.h
319 @comment GNU
320 @item OPTION_DOC
321 This option isn't actually an option (and so should be ignored by the
322 actual option parser), but rather an arbitrary piece of documentation
323 that should be displayed in much the same manner as the options (known
324 as a @dfn{documentation option}).
325
326 If this flag is set, then the option @code{name} field is displayed
327 unmodified (e.g., no @samp{--} prefix is added) at the left-margin
328 (where a @emph{short} option would normally be displayed), and the
329 documentation string in the normal place.  For purposes of sorting, any
330 leading whitespace and punctuation is ignored, except that if the first
331 non-whitespace character is not @samp{-}, this entry is displayed after
332 all options (and @code{OPTION_DOC} entries with a leading @samp{-}) in
333 the same group.
334
335 @comment argp.h
336 @comment GNU
337 @item OPTION_NO_USAGE
338 This option shouldn't be included in `long' usage messages (but is still
339 included in help messages).  This is mainly intended for options that
340 are completely documented in an argp's @code{args_doc} field
341 (@pxref{Argp Parsers}), in which case including the option
342 in the generic usage list would be redundant.
343
344 For instance, if @code{args_doc} is @code{"FOO BAR\n-x BLAH"}, and the
345 @samp{-x} option's purpose is to distinguish these two cases, @samp{-x}
346 should probably be marked @code{OPTION_NO_USAGE}.
347 @end vtable
348
349 @node Argp Parser Functions, Argp Children, Argp Option Vectors, Argp Parsers
350 @subsection Argp Parser Functions
351
352 The function pointed to by the @code{parser} field in a @code{struct
353 argp} (@pxref{Argp Parsers}) defines what actions take place in response
354 to each option or argument that is parsed, and is also used as a hook,
355 to allow a parser to do something at certain other points during
356 parsing.
357
358 @need 2000
359 Argp parser functions have the following type signature:
360
361 @cindex argp parser functions
362 @smallexample
363 error_t @var{parser} (int @var{key}, char *@var{arg}, struct argp_state *@var{state})
364 @end smallexample
365
366 @noindent
367 where the arguments are as follows:
368
369 @table @var
370 @item key
371 For each option that is parsed, @var{parser} is called with a value of
372 @var{key} from that option's @code{key} field in the option vector
373 (@pxref{Argp Option Vectors}).  @var{parser} is also called at other
374 times with special reserved keys, such as @code{ARGP_KEY_ARG} for
375 non-option arguments.  @xref{Argp Special Keys}.
376
377 @item arg
378 If @var{key} is an option, @var{arg} is the value given for it, or zero
379 if no value was specified.  Only options that have a non-zero @code{arg}
380 field can ever have a value, and those must @emph{always} have a value,
381 unless the @code{OPTION_ARG_OPTIONAL} flag was specified (if the input
382 being parsed specifies a value for an option that doesn't allow one, an
383 error results before @var{parser} ever gets called).
384
385 If @var{key} is @code{ARGP_KEY_ARG}, @var{arg} is a non-option argument;
386 other special keys always have a zero @var{arg}.
387
388 @item state
389 @var{state} points to a @code{struct argp_state}, containing useful
390 information about the current parsing state for use by @var{parser}.
391 @xref{Argp Parsing State}.
392 @end table
393
394 When @var{parser} is called, it should perform whatever action is
395 appropriate for @var{key}, and return either @code{0} for success,
396 @code{ARGP_ERR_UNKNOWN}, if the value of @var{key} is not handled by
397 this parser function, or a unix error code if a real error occurred
398 (@pxref{Error Codes}).
399
400 @comment argp.h
401 @comment GNU
402 @deftypevr Macro int ARGP_ERR_UNKNOWN
403 Argp parser functions should return @code{ARGP_ERR_UNKNOWN} for any
404 @var{key} value they do not recognize, or for non-option arguments
405 (@code{@var{key} == ARGP_KEY_ARG}) that they do not which to handle.
406 @end deftypevr
407
408 @need 3000
409 A typical parser function uses a switch statement on @var{key}:
410
411 @smallexample
412 error_t
413 parse_opt (int key, char *arg, struct argp_state *state)
414 @{
415   switch (key)
416     @{
417     case @var{option_key}:
418       @var{action}
419       break;
420     @dots{}
421     default:
422       return ARGP_ERR_UNKNOWN;
423     @}
424   return 0;
425 @}
426 @end smallexample
427
428 @menu
429 * Keys: Argp Special Keys.           Special values for the @var{key} argument.
430 * State: Argp Parsing State.         What the @var{state} argument refers to.
431 * Functions: Argp Helper Functions.  Functions to help during argp parsing.
432 @end menu
433
434 @node Argp Special Keys, Argp Parsing State, , Argp Parser Functions
435 @subsubsection Special Keys for Argp Parser Functions
436
437 In addition to key values corresponding to user options, the @var{key}
438 argument to argp parser functions may have a number of other special
439 values (@var{arg} and @var{state} refer to parser function arguments;
440 @pxref{Argp Parser Functions}):
441
442 @vtable @code
443 @comment argp.h
444 @comment GNU
445 @item ARGP_KEY_ARG
446 This is not an option at all, but rather a command line argument, whose
447 value is pointed to by @var{arg}.
448
449 When there are multiple parser functions (due to argp parsers being
450 combined), it's impossible to know which one wants to handle an
451 argument, so each is called in turn, until one returns 0 or an error
452 other than @code{ARGP_ERR_UNKNOWN}; if an argument is handled by no one,
453 @code{argp_parse} immediately returns success, without parsing any more
454 arguments.
455
456 Once a parser function returns success for this key, that fact is
457 recorded, and the @code{ARGP_KEY_NO_ARGS} case won't be used.
458 @emph{However}, if while processing the argument, a parser function
459 decrements the @code{next} field of its @var{state} argument, the option
460 won't be considered processed; this is to allow you to actually modify
461 the argument (perhaps into an option), and have it processed again.
462
463 @comment argp.h
464 @comment GNU
465 @item ARGP_KEY_ARGS
466 If a parser function returns @code{ARGP_ERR_UNKNOWN} for
467 @code{ARGP_KEY_ARG}, it is immediately called again with the key
468 @code{ARGP_KEY_ARGS}, which has a similar meaning, but is slightly more
469 convenient for consuming all remaining arguments.  @var{arg} is 0, and
470 the tail of the argument vector may be found at @code{@var{state}->argv
471 + @var{state}->next}.  If success is returned for this key, and
472 @code{@var{state}->next} is unchanged, then all remaining arguments are
473 considered to have been consumed, otherwise, the amount by which
474 @code{@var{state}->next} has been adjust indicates how many were used.
475 For instance, here's an example that uses both, for different args:
476
477 @smallexample
478 ...
479 case ARGP_KEY_ARG:
480   if (@var{state}->arg_num == 0)
481     /* First argument */
482     first_arg = @var{arg};
483   else
484     return ARGP_KEY_UNKNOWN;    /* Let the next case parse it.  */
485   break;
486 case ARGP_KEY_ARGS:
487   remaining_args = @var{state}->argv + @var{state}->next;
488   num_remaining_args = @var{state}->argc - @var{state}->next;
489   break;
490 @end smallexample
491
492 @comment argp.h
493 @comment GNU
494 @item ARGP_KEY_END
495 There are no more command line arguments at all.
496
497 @comment argp.h
498 @comment GNU
499 @item ARGP_KEY_NO_ARGS
500 Because it's common to want to do some special processing if there
501 aren't any non-option args, parser functions are called with this key if
502 they didn't successfully process any non-option arguments.  Called just
503 before @code{ARGP_KEY_END} (where more general validity checks on
504 previously parsed arguments can take place).
505
506 @comment argp.h
507 @comment GNU
508 @item ARGP_KEY_INIT
509 Passed in before any parsing is done.  Afterwards, the values of each
510 element of the @code{child_input} field of @var{state}, if any, are
511 copied to each child's state to be the initial value of the @code{input}
512 when @emph{their} parsers are called.
513
514 @comment argp.h
515 @comment GNU
516 @item ARGP_KEY_SUCCESS
517 Passed in when parsing has successfully been completed (even if there are
518 still arguments remaining).
519
520 @comment argp.h
521 @comment GNU
522 @item ARGP_KEY_ERROR
523 Passed in if an error has occurred, and parsing terminated (in which case
524 a call with a key of @code{ARGP_KEY_SUCCESS} is never made).
525
526 @comment argp.h
527 @comment GNU
528 @item ARGP_KEY_FINI
529 The final key ever seen by any parser (even after
530 @code{ARGP_KEY_SUCCESS} and @code{ARGP_KEY_ERROR}).  Any resources
531 allocated by @code{ARGP_KEY_INIT} may be freed here (although sometimes
532 certain resources allocated there are to be returned to the caller after
533 a successful parse; in that case, those particular resources can be
534 freed in the @code{ARGP_KEY_ERROR} case).
535 @end vtable
536
537 In all cases, @code{ARGP_KEY_INIT} is the first key seen by parser
538 functions, and @code{ARGP_KEY_FINI} the last (unless an error was
539 returned by the parser for @code{ARGP_KEY_INIT}).  Other keys can occur
540 in one the following orders (@var{opt} refers to an arbitrary option
541 key):
542
543 @table @asis
544 @item @var{opt}@dots{} @code{ARGP_KEY_NO_ARGS} @code{ARGP_KEY_END} @code{ARGP_KEY_SUCCESS}
545 The arguments being parsed contained no non-option arguments at all.
546
547 @item ( @var{opt} | @code{ARGP_KEY_ARG} )@dots{} @code{ARGP_KEY_END} @code{ARGP_KEY_SUCCESS}
548 All non-option arguments were successfully handled by a parser function
549 (there may be multiple parser functions if multiple argp parsers were
550 combined).
551
552 @item ( @var{opt} | @code{ARGP_KEY_ARG} )@dots{} @code{ARGP_KEY_SUCCESS}
553 Some non-option argument was unrecognized.
554
555 This occurs when every parser function returns @code{ARGP_KEY_UNKNOWN}
556 for an argument, in which case parsing stops at that argument.  If a
557 non-zero value for @var{arg_index} was passed to @code{argp_parse}, the
558 index of this argument is returned in it, otherwise an error occurs.
559 @end table
560
561 If an error occurs (either detected by argp, or because a parser
562 function returned an error value), then each parser is called with
563 @code{ARGP_KEY_ERROR}, and no further calls are made except the final
564 call with @code{ARGP_KEY_FINI}.
565
566 @node Argp Helper Functions, , Argp Parsing State, Argp Parser Functions
567 @subsubsection Functions For Use in Argp Parsers
568
569 Argp provides a number of functions for the user of argp parser
570 functions (@pxref{Argp Parser Functions}), mostly for producing error
571 messages.  These take as their first argument the @var{state} argument
572 to the parser function (@pxref{Argp Parsing State}).
573
574 @cindex usage messages, in argp
575 @comment argp.h
576 @comment GNU
577 @deftypefun void argp_usage (const struct argp_state *@var{state})
578 Output the standard usage message for the argp parser referred to by
579 @var{state} to @code{@var{state}->err_stream} and terminate the program
580 with @code{exit (argp_err_exit_status)} (@pxref{Argp Global Variables}).
581 @end deftypefun
582
583 @cindex syntax error messages, in argp
584 @comment argp.h
585 @comment GNU
586 @deftypefun void argp_error (const struct argp_state *@var{state}, @w{const char *@var{fmt}, @dots{}})
587 Print the printf format string @var{fmt} and following args, preceded by
588 the program name and @samp{:}, and followed by a @w{@samp{Try @dots{}
589 --help}} message, and terminate the program with an exit status of
590 @code{argp_err_exit_status} (@pxref{Argp Global Variables}).
591 @end deftypefun
592
593 @cindex error messages, in argp
594 @comment argp.h
595 @comment GNU
596 @deftypefun void argp_failure (const struct argp_state *@var{state}, @w{int @var{status}, int @var{errnum},} @w{const char *@var{fmt}, @dots{}})
597 Similarly to the standard gnu error-reporting function @code{error},
598 print the printf format string @var{fmt} and following args, preceded by
599 the program name and @samp{:}, and followed by the standard unix error
600 text for @var{errnum} if it is non-zero; then if @var{status} is
601 non-zero, terminate the program with that as its exit status.
602
603 The difference between this function and @code{argp_error} is that
604 @code{argp_error} is for @emph{parsing errors}, whereas
605 @code{argp_failure} is for other problems that occur during parsing but
606 don't reflect a syntactic problem with the input---such as illegal
607 values for options, bad phase of the moon, etc.
608 @end deftypefun
609
610 @comment argp.h
611 @comment GNU
612 @deftypefun void argp_state_help (const struct argp_state *@var{state}, @w{FILE *@var{stream}}, @w{unsigned @var{flags}})
613 Output a help message for the argp parser referred to by @var{state} to
614 @var{stream}.  The @var{flags} argument determines what sort of help
615 message is produced.  @xref{Argp Help Flags}.
616 @end deftypefun
617
618 Error output is sent to @code{@var{state}->err_stream}, and the program
619 name printed is @code{@var{state}->name}.
620
621 The output or program termination behavior of these functions may be
622 suppressed if the @code{ARGP_NO_EXIT} or @code{ARGP_NO_ERRS} flags,
623 respectively, were passed to @code{argp_parse}.  @xref{Argp Flags}.
624
625 This behavior is useful if an argp parser is exported for use by other
626 programs (e.g., by a library), and may be used in a context where it is
627 not desirable to terminate the program in response to parsing errors.
628 In argp parsers intended for such general use, calls to any of these
629 functions should be followed by code return of an appropriate error code
630 for the case where the program @emph{doesn't} terminate; for example:
631
632 @smallexample
633 if (@var{bad argument syntax})
634   @{
635      argp_usage (@var{state});
636      return EINVAL;
637   @}
638 @end smallexample
639
640 @noindent
641 If it's known that a parser function will only be used when
642 @code{ARGP_NO_EXIT} is not set, the return may be omitted.
643
644 @node Argp Parsing State, Argp Helper Functions, Argp Special Keys, Argp Parser Functions
645 @subsubsection Argp Parsing State
646
647 The third argument to argp parser functions (@pxref{Argp Parser
648 Functions}) is a pointer to a @code{struct argp_state}, which contains
649 information about the state of the option parsing.
650
651 @comment argp.h
652 @comment GNU
653 @deftp {Data Type} {struct argp_state}
654 This structure has the following fields, which may be modified as noted:
655
656 @table @code
657 @item const struct argp *const root_argp
658 The top level argp parser being parsed.  Note that this is often
659 @emph{not} the same @code{struct argp} passed into @code{argp_parse} by
660 the invoking program (@pxref{Argp}), but instead an internal argp parser
661 that contains options implemented by @code{argp_parse} itself (such as
662 @samp{--help}).
663
664 @item int argc
665 @itemx char **argv
666 The argument vector being parsed.  May be modified.
667
668 @item int next
669 The index in @code{argv} of the next argument to be parsed.  May be modified.
670
671 One way to consume all remaining arguments in the input is to set
672 @code{@var{state}->next = @var{state}->argc} (perhaps after recording
673 the value of the @code{next} field to find the consumed arguments).
674 Also, you can cause the current option to be re-parsed by decrementing
675 this field, and then modifying
676 @code{@var{state}->argv[@var{state}->next]} to be the option that should
677 be reexamined.
678
679 @item unsigned flags
680 The flags supplied to @code{argp_parse}.  May be modified, although some
681 flags may only take effect when @code{argp_parse} is first invoked.
682 @xref{Argp Flags}.
683
684 @item unsigned arg_num
685 While calling a parsing function with the @var{key} argument
686 @code{ARGP_KEY_ARG}, this is the number of the current arg, starting at
687 0, and incremented after each such call returns.  At all other times,
688 this is the number of such arguments that have been processed.
689
690 @item int quoted
691 If non-zero, the index in @code{argv} of the first argument following a
692 special @samp{--} argument (which prevents anything following being
693 interpreted as an option).  Only set once argument parsing has proceeded
694 past this point.
695
696 @item void *input
697 An arbitrary pointer passed in from the caller of @code{argp_parse}, in
698 the @var{input} argument.
699
700 @item void **child_inputs
701 Values to pass to child parsers.  This vector will be the same length as
702 the number of children in the current parser, and each child parser will
703 be given the value of @code{@var{state}->child_inputs[@var{i}]} as
704 @emph{its} @code{@var{state}->input} field, where @var{i} is the index
705 of the child in the this parser's @code{children} field.  @xref{Argp
706 Children}.
707
708 @item void *hook
709 For the parser function's use.  Initialized to 0, but otherwise ignored
710 by argp.
711
712 @item char *name
713 The name used when printing messages.  This is initialized to
714 @code{argv[0]}, or @code{program_invocation_name} if that is
715 unavailable.
716
717 @item FILE *err_stream
718 @itemx FILE *out_stream
719 Stdio streams used when argp prints something; error messages are
720 printed to @code{err_stream}, and all other output (such as
721 @samp{--help} output) to @code{out_stream}.  These are initialized to
722 @code{stderr} and @code{stdout} respectively (@pxref{Standard Streams}).
723
724 @item void *pstate
725 Private, for use by the argp implementation.
726 @end table
727 @end deftp
728
729 @node Argp Children, Argp Help Filtering, Argp Parser Functions, Argp Parsers
730 @subsection Combining Multiple Argp Parsers
731
732 The @code{children} field in a @code{struct argp} allows other argp
733 parsers to be combined with the referencing one to parse a single set of
734 arguments.  It should point to a vector of @code{struct argp_child},
735 terminated by an entry having a value of zero in the @code{argp} field.
736
737 Where conflicts between combined parsers arise (for instance, if two
738 specify an option with the same name), they are resolved in favor of
739 the parent argp parsers, or earlier argp parsers in the list of children.
740
741 @comment argp.h
742 @comment GNU
743 @deftp {Data Type} {struct argp_child}
744 An entry in the list of subsidiary argp parsers pointed to by the
745 @code{children} field in a @code{struct argp}.  The fields are as follows:
746
747 @table @code
748 @item const struct argp *argp
749 The child argp parser, or zero to end the list.
750
751 @item int flags
752 Flags for this child.
753
754 @item const char *header
755 If non-zero, an optional header to be printed in help output before the
756 child options.  As a side-effect, a non-zero value forces the child
757 options to be grouped together; to achieve this effect without actually
758 printing a header string, use a value of @code{""}.  As with header
759 strings specified in an option entry, the value conventionally has
760 @samp{:} as the last character.  @xref{Argp Option Vectors}.
761
762 @item int group
763 Where to group the child options relative to the other (`consolidated')
764 options in the parent argp parser.  The values are the same as the
765 @code{group} field in @code{struct argp_option} (@pxref{Argp Option
766 Vectors}), but all child-groupings follow parent options at a particular
767 group level.  If both this field and @code{header} are zero, then the
768 child's options aren't grouped together at all, but rather merged with
769 the parent options (merging the child's grouping levels with the
770 parents).
771 @end table
772 @end deftp
773
774 @node Argp Flags, Argp Help, Argp Parsers, Argp
775 @subsection Flags for @code{argp_parse}
776
777 The default behavior of @code{argp_parse} is designed to be convenient
778 for the most common case of parsing program command line argument.  To
779 modify these defaults, the following flags may be or'd together in the
780 @var{flags} argument to @code{argp_parse}:
781
782 @vtable @code
783 @comment argp.h
784 @comment GNU
785 @item ARGP_PARSE_ARGV0
786 Don't ignore the first element of the @var{argv} argument to
787 @code{argp_parse}.  Normally (and always unless @code{ARGP_NO_ERRS} is
788 set) the first element of the argument vector is skipped for option
789 parsing purposes, as it corresponds to the program name in a command
790 line.
791
792 @comment argp.h
793 @comment GNU
794 @item ARGP_NO_ERRS
795 Don't print error messages for unknown options to @code{stderr}; unless
796 this flag is set, @code{ARGP_PARSE_ARGV0} is ignored, as @code{argv[0]}
797 is used as the program name in the error messages.  This flag implies
798 @code{ARGP_NO_EXIT} (on the assumption that silent exiting upon errors
799 is bad behaviour).
800
801 @comment argp.h
802 @comment GNU
803 @item ARGP_NO_ARGS
804 Don't parse any non-option args.  Normally non-option args are parsed by
805 calling the parse functions with a key of @code{ARGP_KEY_ARG}, and the
806 actual arg as the value.  This flag needn't normally be set, as the
807 normal behavior is to stop parsing as soon as some argument isn't
808 accepted by a parsing function.  @xref{Argp Parser Functions}.
809
810 @comment argp.h
811 @comment GNU
812 @item ARGP_IN_ORDER
813 Parse options and arguments in the same order they occur on the command
814 line---normally they're rearranged so that all options come first
815
816 @comment argp.h
817 @comment GNU
818 @item ARGP_NO_HELP
819 Don't provide the standard long option @samp{--help}, which ordinarily
820 causes usage and option help information to be output to @code{stdout},
821 and @code{exit (0)} called.
822
823 @comment argp.h
824 @comment GNU
825 @item ARGP_NO_EXIT
826 Don't exit on errors (they may still result in error messages).
827
828 @comment argp.h
829 @comment GNU
830 @item ARGP_LONG_ONLY
831 Use the gnu getopt `long-only' rules for parsing arguments.  This
832 allows long-options to be recognized with only a single @samp{-} (for
833 instances, @samp{-help}), but results in a generally somewhat less
834 useful interface, that conflicts with the way most GNU programs work.
835 For this reason, its use is discouraged.
836
837 @comment argp.h
838 @comment GNU
839 @item ARGP_SILENT
840 Turns off any message-printing/exiting options, specifically
841 @code{ARGP_NO_EXIT}, @code{ARGP_NO_ERRS}, and @code{ARGP_NO_HELP}.
842 @end vtable
843
844 @node Argp Help Filtering, , Argp Children, Argp Parsers
845 @need 2000
846 @subsection Customizing Argp Help Output
847
848 The @code{help_filter} field in a a @code{struct argp} is a pointer to a
849 function to filter the text of help messages before displaying them.
850 They have a function signature like:
851
852 @smallexample
853 char *@var{help-filter} (int @var{key}, const char *@var{text}, void *@var{input})
854 @end smallexample
855
856 @noindent
857 where @var{key} is either a key from an option, in which case @var{text}
858 is that option's help text (@pxref{Argp Option Vectors}), or one of the
859 special keys with names beginning with @samp{ARGP_KEY_HELP_}, describing
860 which other help text @var{text} is (@pxref{Argp Help Filter Keys}).
861
862 The function should return either @var{text}, if it should be used
863 as-is, a replacement string, which should be allocated using
864 @code{malloc}, and will be freed by argp, or zero, meaning `print
865 nothing'.  The value of @var{text} supplied is @emph{after} any
866 translation has been done, so if any of the replacement text also needs
867 translation, that should be done by the filter function.  @var{input} is
868 either the input supplied to @code{argp_parse}, or zero, if
869 @code{argp_help} was called directly by the user.
870
871 @menu
872 * Keys: Argp Help Filter Keys.  Special @var{key} values for help filter functions.
873 @end menu
874
875 @node Argp Help Filter Keys, , , Argp Help Filtering
876 @subsubsection Special Keys for Argp Help Filter Functions
877
878 The following special values may be passed to an argp help filter
879 function as the first argument, in addition to key values for user
880 options, and specify which help text the @var{text} argument contains:
881
882 @vtable @code
883 @comment argp.h
884 @comment GNU
885 @item ARGP_KEY_HELP_PRE_DOC
886 Help text preceding options.
887
888 @comment argp.h
889 @comment GNU
890 @item ARGP_KEY_HELP_POST_DOC
891 Help text following options.
892
893 @comment argp.h
894 @comment GNU
895 @item ARGP_KEY_HELP_HEADER
896 Option header string.
897
898 @comment argp.h
899 @comment GNU
900 @item ARGP_KEY_HELP_EXTRA
901 After all other documentation; @var{text} is zero for this key.
902
903 @comment argp.h
904 @comment GNU
905 @item ARGP_KEY_HELP_DUP_ARGS_NOTE
906 The explanatory note emitted when duplicate option arguments have been
907 suppressed.
908
909 @comment argp.h
910 @comment GNU
911 @item ARGP_KEY_HELP_ARGS_DOC
912 The argument doc string (the @code{args_doc} field from the argp parser;
913 @pxref{Argp Parsers}).
914 @end vtable
915
916 @node Argp Help, Argp Examples, Argp Flags, Argp
917 @subsection The @code{argp_help} Function
918
919 Normally programs using argp need not worry too much about printing
920 argument-usage-type help messages, because the standard @samp{--help}
921 option is handled automatically by argp, and the typical error cases can
922 be handled using @code{argp_usage} and @code{argp_error} (@pxref{Argp
923 Helper Functions}).
924
925 However, if it's desirable to print a standard help message in some
926 context other than parsing the program options, argp offers the
927 @code{argp_help} interface.
928
929 @comment argp.h
930 @comment GNU
931 @deftypefun void argp_help (const struct argp *@var{argp}, @w{FILE *@var{stream}}, @w{unsigned @var{flags}}, @w{char *@var{name}})
932 Output a help message for the argp parser @var{argp} to @var{stream}.
933 What sort of messages is printed is determined by @var{flags}.
934
935 Any options such as @samp{--help} that are implemented automatically by
936 argp itself will @emph{not} be present in the help output; for this
937 reason, it is better to use @code{argp_state_help} if calling from
938 within an argp parser function.  @xref{Argp Helper Functions}.
939 @end deftypefun
940
941 @menu
942 * Flags: Argp Help Flags.       Specifying what sort of help message to print.
943 @end menu
944
945 @node Argp Help Flags, , , Argp Help
946 @subsection Flags for the @code{argp_help} Function
947
948 When calling @code{argp_help} (@pxref{Argp Help}), or
949 @code{argp_state_help} (@pxref{Argp Helper Functions}), exactly what is
950 output is determined by the @var{flags} argument, which should consist
951 of any of the following flags, or'd together:
952
953 @vtable @code
954 @item ARGP_HELP_USAGE
955 A unix @samp{Usage:} message that explicitly lists all options.
956
957 @item ARGP_HELP_SHORT_USAGE
958 A unix @samp{Usage:} message that displays only an appropriate
959 placeholder to indicate where the options go; useful for showing
960 the non-option argument syntax.
961
962 @item ARGP_HELP_SEE
963 A @samp{Try @dots{} for more help} message; @samp{@dots{}} contains the
964 program name and @samp{--help}.
965
966 @item ARGP_HELP_LONG
967 A verbose option help message that gives each option understood along
968 with its documentation string.
969
970 @item ARGP_HELP_PRE_DOC
971 The part of the argp parser doc string that precedes the verbose option help.
972
973 @item ARGP_HELP_POST_DOC
974 The part of the argp parser doc string that follows the verbose option help.
975
976 @item ARGP_HELP_DOC
977 @code{(ARGP_HELP_PRE_DOC | ARGP_HELP_POST_DOC)}
978
979 @item ARGP_HELP_BUG_ADDR
980 A message saying where to report bugs for this program, if the
981 @code{argp_program_bug_address} variable contains one.
982
983 @item ARGP_HELP_LONG_ONLY
984 Modify any output appropriately to reflect @code{ARGP_LONG_ONLY} mode.
985 @end vtable
986
987 The following flags are only understood when used with
988 @code{argp_state_help}, and control whether the function returns after
989 printing its output, or terminates the program:
990
991 @vtable @code
992 @item ARGP_HELP_EXIT_ERR
993 Terminate the program with @code{exit (argp_err_exit_status)}.
994
995 @item ARGP_HELP_EXIT_OK
996 Terminate the program with @code{exit (0)}.
997 @end vtable
998
999 The following flags are combinations of the basic ones for printing
1000 standard messages:
1001
1002 @vtable @code
1003 @item ARGP_HELP_STD_ERR
1004 Assuming an error message for a parsing error has already printed,
1005 prints a note on how to get help, and terminates the program with an
1006 error.
1007
1008 @item ARGP_HELP_STD_USAGE
1009 Prints a standard usage message and terminates the program with an
1010 error.  This is used when no more specific error message is appropriate.
1011
1012 @item ARGP_HELP_STD_HELP
1013 Prints the standard response for a @samp{--help} option, and terminates
1014 the program successfully.
1015 @end vtable
1016
1017 @node Argp Examples, Argp User Customization, Argp Help, Argp
1018 @subsection Argp Examples
1019
1020 These example programs demonstrate the basic usage of argp.
1021
1022 @menu
1023 * 1: Argp Example 1.            A minimal program using argp.
1024 * 2: Argp Example 2.            A program using only default options.
1025 * 3: Argp Example 3.            A simple program with user options.
1026 * 4: Argp Example 4.            Combining multiple argp parsers.
1027 @end menu
1028
1029 @node Argp Example 1, Argp Example 2, , Argp Examples
1030 @subsubsection A Minimal Program Using Argp
1031
1032 This is (probably) the smallest possible program that uses argp.
1033 It won't do much except give an error messages and exit when there are any
1034 arguments, and print a (rather pointless) message for @samp{--help}.
1035
1036 @smallexample
1037 @include argp-ex1.c.texi
1038 @end smallexample
1039
1040 @node Argp Example 2, Argp Example 3, Argp Example 1, Argp Examples
1041 @subsubsection A Program Using Argp with Only Default Options
1042
1043 This program doesn't use any options or arguments, but uses argp to be
1044 compliant with the GNU standard command line format.
1045
1046 In addition to making sure no arguments are given, and implementing a
1047 @samp{--help} option, this example will have a @samp{--version} option,
1048 and will put the given documentation string and bug address in the
1049 @samp{--help} output, as per GNU standards.
1050
1051 The variable @code{argp} contains the argument parser specification;
1052 adding fields to this structure is the way most parameters are passed to
1053 @code{argp_parse} (the first three fields are usually used, but not in
1054 this small program).  There are also two global variables that argp
1055 knows about defined here, @code{argp_program_version} and
1056 @code{argp_program_bug_address} (they are global variables because they
1057 will almost always be constant for a given program, even if it uses
1058 different argument parsers for various tasks).
1059
1060 @smallexample
1061 @include argp-ex2.c.texi
1062 @end smallexample
1063
1064 @node Argp Example 3, Argp Example 4, Argp Example 2, Argp Examples
1065 @subsubsection A Program Using Argp with User Options
1066
1067 This program uses the same features as example 2, and adds user options
1068 and arguments.
1069
1070 We now use the first four fields in @code{argp} (@pxref{Argp Parsers}),
1071 and specifies @code{parse_opt} as the parser function (@pxref{Argp
1072 Parser Functions}).
1073
1074 Note that in this example, @code{main} uses a structure to communicate
1075 with the @code{parse_opt} function, a pointer to which it passes in the
1076 @code{input} argument to @code{argp_parse} (@pxref{Argp}), and is
1077 retrieved by @code{parse_opt} through the @code{input} field in its
1078 @code{state} argument (@pxref{Argp Parsing State}).  Of course, it's
1079 also possible to use global variables instead, but using a structure
1080 like this is somewhat more flexible and clean.
1081
1082 @smallexample
1083 @include argp-ex3.c.texi
1084 @end smallexample
1085
1086 @node Argp Example 4, , Argp Example 3, Argp Examples
1087 @subsubsection A Program Using Multiple Combined Argp Parsers
1088
1089 This program uses the same features as example 3, but has more options,
1090 and somewhat more structure in the @samp{--help} output.  It also shows
1091 how you can `steal' the remainder of the input arguments past a certain
1092 point, for programs that accept a list of items, and the special
1093 @var{key} value @code{ARGP_KEY_NO_ARGS}, which is only given if no
1094 non-option arguments were supplied to the program (@pxref{Argp Special
1095 Keys}).
1096
1097 For structuring the help output, two features are used: @emph{headers},
1098 which are entries in the options vector (@pxref{Argp Option Vectors})
1099 with the first four fields being zero, and a two part documentation
1100 string (in the variable @code{doc}), which allows documentation both
1101 before and after the options (@pxref{Argp Parsers}); the
1102 two parts of @code{doc} are separated by a vertical-tab character
1103 (@code{'\v'}, or @code{'\013'}).  By convention, the documentation
1104 before the options is just a short string saying what the program does,
1105 and that afterwards is longer, describing the behavior in more detail.
1106 All documentation strings are automatically filled for output, although
1107 newlines may be included to force a line break at a particular point.
1108 All documentation strings are also passed to the @code{gettext}
1109 function, for possible translation into the current locale.
1110
1111 @smallexample
1112 @include argp-ex4.c.texi
1113 @end smallexample
1114
1115 @node Argp User Customization, , Argp Examples, Argp
1116 @subsection Argp User Customization
1117
1118 @cindex ARGP_HELP_FMT environment variable
1119 The way formatting of argp @samp{--help} output may be controlled to
1120 some extent by a program's users, by setting the @code{ARGP_HELP_FMT}
1121 environment variable to a comma-separated list (whitespace is ignored)
1122 of the following tokens:
1123
1124 @table @samp
1125 @item dup-args
1126 @itemx no-dup-args
1127 Turn @dfn{duplicate-argument-mode} on or off.  In duplicate argument
1128 mode, if an option which accepts an argument has multiple names, the
1129 argument is shown for each name; otherwise, it is only shown for the
1130 first long option, and a note is emitted later so the user knows that it
1131 applies to the other names as well.  The default is @samp{no-dup-args},
1132 which is less consistent, but prettier.
1133
1134 @item dup-args-note
1135 @item no-dup-args-note
1136 Enable or disable the note informing the user of suppressed option
1137 argument duplication.  The default is @samp{dup-args-note}.
1138
1139 @item short-opt-col=@var{n}
1140 Show the first short option in column @var{n} (default 2).
1141
1142 @item long-opt-col=@var{n}
1143 Show the first long option in column @var{n} (default 6).
1144
1145 @item doc-opt-col=@var{n}
1146 Show `documentation options' (@pxref{Argp Option Flags}) in column
1147 @var{n} (default 2).
1148
1149 @item opt-doc-col=@var{n}
1150 Show the documentation for options starting in column @var{n} (default 29).
1151
1152 @item header-col=@var{n}
1153 Indent group headers (which document groups of options) to column
1154 @var{n} (default 1).
1155
1156 @item usage-indent=@var{n}
1157 Indent continuation lines in @samp{Usage:} messages to column @var{n}
1158 (default 12).
1159
1160 @item rmargin=@var{n}
1161 Word wrap help output at or before column @var{n} (default 79).
1162 @end table