mod-getopt-long.texi 14 KB

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