argp.texi 53 KB

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