mod-getopt-long.texi 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2011
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node getopt-long
  7. @section The (ice-9 getopt-long) Module
  8. The @code{(ice-9 getopt-long)} facility is designed to help parse
  9. arguments that are passed to Guile programs on the command line, and is
  10. modelled after the C library's facility of the same name
  11. (@pxref{Getopt,,,libc,The GNU C Library Reference Manual}). For a more
  12. low-level interface to command-line argument parsing, @xref{SRFI-37}.
  13. The @code{(ice-9 getopt-long)} module exports two procedures:
  14. @code{getopt-long} and @code{option-ref}.
  15. @itemize @bullet
  16. @item
  17. @code{getopt-long} takes a list of strings --- the command line
  18. arguments --- an @dfn{option specification}, and some optional keyword
  19. parameters. It parses the command line arguments according to the
  20. option specification and keyword parameters, and returns a data
  21. structure that encapsulates the results of the parsing.
  22. @item
  23. @code{option-ref} then takes the parsed data structure and a specific
  24. option's name, and returns information about that option in particular.
  25. @end itemize
  26. To make these procedures available to your Guile script, include the
  27. expression @code{(use-modules (ice-9 getopt-long))} somewhere near the
  28. top, before the first usage of @code{getopt-long} or @code{option-ref}.
  29. @menu
  30. * getopt-long Example:: A short getopt-long example.
  31. * Option Specification:: How to write an option specification.
  32. * Command Line Format:: The expected command line format.
  33. * getopt-long Reference:: Full documentation for @code{getopt-long}.
  34. * option-ref Reference:: Full documentation for @code{option-ref}.
  35. @end menu
  36. @node getopt-long Example
  37. @subsection A Short getopt-long Example
  38. This section illustrates how @code{getopt-long} is used by presenting
  39. and dissecting a simple example. The first thing that we need is an
  40. @dfn{option specification} that tells @code{getopt-long} how to parse
  41. the command line. This specification is an association list with the
  42. long option name as the key. Here is how such a specification might
  43. look:
  44. @lisp
  45. (define option-spec
  46. '((version (single-char #\v) (value #f))
  47. (help (single-char #\h) (value #f))))
  48. @end lisp
  49. This alist tells @code{getopt-long} that it should accept two long
  50. options, called @emph{version} and @emph{help}, and that these options
  51. can also be selected by the single-letter abbreviations @emph{v} and
  52. @emph{h}, respectively. The @code{(value #f)} clauses indicate that
  53. neither of the options accepts a value.
  54. With this specification we can use @code{getopt-long} to parse a given
  55. command line:
  56. @lisp
  57. (define options (getopt-long (command-line) option-spec))
  58. @end lisp
  59. After this call, @code{options} contains the parsed command line and is
  60. ready to be examined by @code{option-ref}. @code{option-ref} is called
  61. like this:
  62. @lisp
  63. (option-ref options 'help #f)
  64. @end lisp
  65. @noindent
  66. It expects the parsed command line, a symbol indicating the option to
  67. examine, and a default value. The default value is returned if the
  68. option was not present in the command line, or if the option was present
  69. but without a value; otherwise the value from the command line is
  70. returned. Usually @code{option-ref} is called once for each possible
  71. option that a script supports.
  72. The following example shows a main program which puts all this together
  73. to parse its command line and figure out what the user wanted.
  74. @lisp
  75. (define (main args)
  76. (let* ((option-spec '((version (single-char #\v) (value #f))
  77. (help (single-char #\h) (value #f))))
  78. (options (getopt-long args option-spec))
  79. (help-wanted (option-ref options 'help #f))
  80. (version-wanted (option-ref options 'version #f)))
  81. (if (or version-wanted help-wanted)
  82. (begin
  83. (if version-wanted
  84. (display "getopt-long-example version 0.3\n"))
  85. (if help-wanted
  86. (display "\
  87. getopt-long-example [options]
  88. -v, --version Display version
  89. -h, --help Display this help
  90. ")))
  91. (begin
  92. (display "Hello, World!") (newline)))))
  93. @end lisp
  94. @node Option Specification
  95. @subsection How to Write an Option Specification
  96. An option specification is an association list (@pxref{Association
  97. Lists}) with one list element for each supported option. The key of each
  98. list element is a symbol that names the option, while the value is a
  99. list of option properties:
  100. @lisp
  101. OPTION-SPEC ::= '( (OPT-NAME1 (PROP-NAME PROP-VALUE) @dots{})
  102. (OPT-NAME2 (PROP-NAME PROP-VALUE) @dots{})
  103. (OPT-NAME3 (PROP-NAME PROP-VALUE) @dots{})
  104. @dots{}
  105. )
  106. @end lisp
  107. Each @var{opt-name} specifies the long option name for that option. For
  108. example, a list element with @var{opt-name} @code{background} specifies
  109. an option that can be specified on the command line using the long
  110. option @code{--background}. Further information about the option ---
  111. whether it takes a value, whether it is required to be present in the
  112. command line, and so on --- is specified by the option properties.
  113. In the example of the preceding section, we already saw that a long
  114. option name can have a equivalent @dfn{short option} character. The
  115. equivalent short option character can be set for an option by specifying
  116. a @code{single-char} property in that option's property list. For
  117. example, a list element like @code{'(output (single-char #\o) @dots{})}
  118. specifies an option with long name @code{--output} that can also be
  119. specified by the equivalent short name @code{-o}.
  120. The @code{value} property specifies whether an option requires or
  121. accepts a value. If the @code{value} property is set to @code{#t}, the
  122. option requires a value: @code{getopt-long} will signal an error if the
  123. option name is present without a corresponding value. If set to
  124. @code{#f}, the option does not take a value; in this case, a non-option
  125. word that follows the option name in the command line will be treated as
  126. a non-option argument. If set to the symbol @code{optional}, the option
  127. accepts a value but does not require one: a non-option word that follows
  128. the option name in the command line will be interpreted as that option's
  129. value. If the option name for an option with @code{'(value optional)}
  130. is immediately followed in the command line by @emph{another} option
  131. name, the value for the first option is implicitly @code{#t}.
  132. The @code{required?} property indicates whether an option is required to
  133. be present in the command line. If the @code{required?} property is
  134. set to @code{#t}, @code{getopt-long} will signal an error if the option
  135. is not specified.
  136. Finally, the @code{predicate} property can be used to constrain the
  137. possible values of an option. If used, the @code{predicate} property
  138. should be set to a procedure that takes one argument --- the proposed
  139. option value as a string --- and returns either @code{#t} or @code{#f}
  140. according as the proposed value is or is not acceptable. If the
  141. predicate procedure returns @code{#f}, @code{getopt-long} will signal an
  142. error.
  143. By default, options do not have single-character equivalents, are not
  144. required, and do not take values. Where the list element for an option
  145. includes a @code{value} property but no @code{predicate} property, the
  146. option values are unconstrained.
  147. @node Command Line Format
  148. @subsection Expected Command Line Format
  149. In order for @code{getopt-long} to correctly parse a command line, that
  150. command line must conform to a standard set of rules for how command
  151. line options are specified. This section explains what those rules
  152. are.
  153. @code{getopt-long} splits a given command line into several pieces. All
  154. elements of the argument list are classified to be either options or
  155. normal arguments. Options consist of two dashes and an option name
  156. (so-called @dfn{long} options), or of one dash followed by a single
  157. letter (@dfn{short} options).
  158. Options can behave as switches, when they are given without a value, or
  159. they can be used to pass a value to the program. The value for an
  160. option may be specified using an equals sign, or else is simply the next
  161. word in the command line, so the following two invocations are
  162. equivalent:
  163. @example
  164. $ ./foo.scm --output=bar.txt
  165. $ ./foo.scm --output bar.txt
  166. @end example
  167. Short options can be used instead of their long equivalents and can be
  168. grouped together after a single dash. For example, the following
  169. commands are equivalent.
  170. @example
  171. $ ./foo.scm --version --help
  172. $ ./foo.scm -v --help
  173. $ ./foo.scm -vh
  174. @end example
  175. If an option requires a value, it can only be grouped together with other
  176. short options if it is the last option in the group; the value is the
  177. next argument. So, for example, with the following option
  178. specification ---
  179. @lisp
  180. ((apples (single-char #\a))
  181. (blimps (single-char #\b) (value #t))
  182. (catalexis (single-char #\c) (value #t)))
  183. @end lisp
  184. @noindent
  185. --- the following command lines would all be acceptable:
  186. @example
  187. $ ./foo.scm -a -b bang -c couth
  188. $ ./foo.scm -ab bang -c couth
  189. $ ./foo.scm -ac couth -b bang
  190. @end example
  191. But the next command line is an error, because @code{-b} is not the last
  192. option in its combination, and because a group of short options cannot
  193. include two options that both require values:
  194. @example
  195. $ ./foo.scm -abc couth bang
  196. @end example
  197. If an option's value is optional, @code{getopt-long} decides whether the
  198. option has a value by looking at what follows it in the argument list.
  199. If the next element is a string, and it does not appear to be an option
  200. itself, then that string is the option's value.
  201. If the option @code{--} appears in the argument list, argument parsing
  202. stops there and subsequent arguments are returned as ordinary arguments,
  203. even if they resemble options. So, with the command line
  204. @example
  205. $ ./foo.scm --apples "Granny Smith" -- --blimp Goodyear
  206. @end example
  207. @noindent
  208. @code{getopt-long} will recognize the @code{--apples} option as having
  209. the value "Granny Smith", but will not treat @code{--blimp} as an
  210. option. The strings @code{--blimp} and @code{Goodyear} will be returned
  211. as ordinary argument strings.
  212. @node getopt-long Reference
  213. @subsection Reference Documentation for @code{getopt-long}
  214. @deffn {Scheme Procedure} getopt-long args grammar [#:stop-at-first-non-option #t]
  215. Parse the command line given in @var{args} (which must be a list of
  216. strings) according to the option specification @var{grammar}.
  217. The @var{grammar} argument is expected to be a list of this form:
  218. @code{((@var{option} (@var{property} @var{value}) @dots{}) @dots{})}
  219. where each @var{option} is a symbol denoting the long option, but
  220. without the two leading dashes (e.g.@: @code{version} if the option is
  221. called @code{--version}).
  222. For each option, there may be list of arbitrarily many property/value
  223. pairs. The order of the pairs is not important, but every property may
  224. only appear once in the property list. The following table lists the
  225. possible properties:
  226. @table @asis
  227. @item @code{(single-char @var{char})}
  228. Accept @code{-@var{char}} as a single-character equivalent to
  229. @code{--@var{option}}. This is how to specify traditional Unix-style
  230. flags.
  231. @item @code{(required? @var{bool})}
  232. If @var{bool} is true, the option is required. @code{getopt-long} will
  233. raise an error if it is not found in @var{args}.
  234. @item @code{(value @var{bool})}
  235. If @var{bool} is @code{#t}, the option accepts a value; if it is
  236. @code{#f}, it does not; and if it is the symbol @code{optional}, the
  237. option may appear in @var{args} with or without a value.
  238. @item @code{(predicate @var{func})}
  239. If the option accepts a value (i.e.@: you specified @code{(value #t)} for
  240. this option), then @code{getopt-long} will apply @var{func} to the
  241. value, and throw an exception if it returns @code{#f}. @var{func}
  242. should be a procedure which accepts a string and returns a boolean
  243. value; you may need to use quasiquotes to get it into @var{grammar}.
  244. @end table
  245. The @code{#:stop-at-first-non-option} keyword, if specified with any
  246. true value, tells @code{getopt-long} to stop when it gets to the first
  247. non-option in the command line. That is, at the first word which is
  248. neither an option itself, nor the value of an option. Everything in the
  249. command line from that word onwards will be returned as non-option
  250. arguments.
  251. @end deffn
  252. @code{getopt-long}'s @var{args} parameter is expected to be a list of
  253. strings like the one returned by @code{command-line}, with the first
  254. element being the name of the command. Therefore @code{getopt-long}
  255. ignores the first element in @var{args} and starts argument
  256. interpretation with the second element.
  257. @code{getopt-long} signals an error if any of the following conditions
  258. hold.
  259. @itemize @bullet
  260. @item
  261. The option grammar has an invalid syntax.
  262. @item
  263. One of the options in the argument list was not specified by the
  264. grammar.
  265. @item
  266. A required option is omitted.
  267. @item
  268. An option which requires an argument did not get one.
  269. @item
  270. An option that doesn't accept an argument does get one (this can only
  271. happen using the long option @code{--opt=@var{value}} syntax).
  272. @item
  273. An option predicate fails.
  274. @end itemize
  275. @code{#:stop-at-first-non-option} is useful for command line invocations
  276. like @code{guild [--help | --version] [script [script-options]]}
  277. and @code{cvs [general-options] command [command-options]}, where there
  278. are options at two levels: some generic and understood by the outer
  279. command, and some that are specific to the particular script or command
  280. being invoked. To use @code{getopt-long} in such cases, you would call
  281. it twice: firstly with @code{#:stop-at-first-non-option #t}, so as to
  282. parse any generic options and identify the wanted script or sub-command;
  283. secondly, and after trimming off the initial generic command words, with
  284. a script- or sub-command-specific option grammar, so as to process those
  285. specific options.
  286. @node option-ref Reference
  287. @subsection Reference Documentation for @code{option-ref}
  288. @deffn {Scheme Procedure} option-ref options key default
  289. Search @var{options} for a command line option named @var{key} and
  290. return its value, if found. If the option has no value, but was given,
  291. return @code{#t}. If the option was not given, return @var{default}.
  292. @var{options} must be the result of a call to @code{getopt-long}.
  293. @end deffn
  294. @code{option-ref} always succeeds, either by returning the requested
  295. option value from the command line, or the default value.
  296. The special key @code{'()} can be used to get a list of all
  297. non-option arguments.