mod-getopt-long.texi 13 KB

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