Added Copyright notice.
[bpt/guile.git] / doc / ref / script-getopt.texi
CommitLineData
2da09c3f
MV
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
a0e07ba4
NJ
7@page
8@node Command Line Handling
9@chapter Handling Command Line Options and Arguments
10
11@c This chapter was written and contributed by Martin Grabmueller.
12
13The ability to accept and handle command line arguments is very
14important when writing Guile scripts to solve particular problems, such
15as extracting information from text files or interfacing with existing
16command line applications. This chapter describes how Guile makes
17command line arguments available to a Guile script, and the utilities
18that Guile provides to help with the processing of command line
19arguments.
20
21@menu
22* Command Line Args:: Using command line arguments.
23* getopt-long:: The (ice-9 getopt-long) module.
24@end menu
25
26
27@node Command Line Args
28@section Using Command Line Arguments
29
30When a Guile script is invoked, Guile makes the command line arguments
31accessible via the procedure @code{command-line}, which returns the
32arguments as a list of strings.
33
34For example, if the script
35
36@example
37#! /usr/local/bin/guile -s
38!#
39(write (command-line))
40(newline)
41@end example
42
43@noindent
44is saved in a file @file{cmdline-test.scm} and invoked using the command
45line @code{./cmdline-test.scm bar.txt -o foo -frumple grob}, the output
46is
47
48@example
49("./cmdline-test.scm" "bar.txt" "-o" "foo" "-frumple" "grob")
50@end example
51
52If the script invocation includes a @code{-e} option, specifying a
53procedure to call after loading the script, Guile will call that
54procedure with @code{(command-line)} as its argument. So a script that
55uses @code{-e} doesn't need to refer explicitly to @code{command-line}
56in its code. For example, the script above would have identical
57behaviour if it was written instead like this:
58
59@example
60#! /usr/local/bin/guile \
61-e main -s
62!#
63(define (main args)
64 (write args)
65 (newline))
66@end example
67
68(Note the use of the meta switch @code{\} so that the script invocation
69can include more than one Guile option: @xref{The Meta Switch}.)
70
71These scripts use the @code{#!} POSIX convention so that they can be
72executed using their own file names directly, as in the example command
73line @code{./cmdline-test.scm bar.txt -o foo -frumple grob}. But they
74can also be executed by typing out the implied Guile command line in
75full, as in:
76
77@example
78$ guile -s ./cmdline-test.scm bar.txt -o foo -frumple grob
79@end example
80
81@noindent
82or
83
84@example
85$ guile -e main -s ./cmdline-test2.scm bar.txt -o foo -frumple grob
86@end example
87
88Even when a script is invoked using this longer form, the arguments that
89the script receives are the same as if it had been invoked using the
90short form. Guile ensures that the @code{(command-line)} or @code{-e}
91arguments are independent of how the script is invoked, by stripping off
92the arguments that Guile itself processes.
93
94
95@node getopt-long
96@section The (ice-9 getopt-long) Module
97
98A script is free to parse and handle its command line arguments in any
99way that it chooses. Where the set of possible options and arguments is
100complex, however, it can get tricky to extract all the options, check
101the validity of given arguments, and so on. This task can be greatly
102simplified by taking advantage of the module @code{(ice-9 getopt-long)},
103which is distributed with Guile.
104
105The @code{(ice-9 getopt-long)} module exports two procedures:
106@code{getopt-long} and @code{option-ref}.
107
108@itemize @bullet
109@item
110@code{getopt-long} takes a list of strings --- the command line
111arguments --- and an @dfn{option specification}. It parses the command
112line arguments according to the option specification and returns a data
113structure that encapsulates the results of the parsing.
114
115@item
116@code{option-ref} then takes the parsed data structure and a specific
117option's name, and returns information about that option in particular.
118@end itemize
119
120To make these procedures available to your Guile script, include the
121expression @code{(use-modules (ice-9 getopt-long))} somewhere near the
122top, before the first usage of @code{getopt-long} or @code{option-ref}.
123
124@menu
125* getopt-long Example:: A short getopt-long example.
126* Option Specification:: How to write an option specification.
127* Command Line Format:: The expected command line format.
128* getopt-long Reference:: Full documentation for @code{getopt-long}.
129* option-ref Reference:: Full documentation for @code{option-ref}.
130@end menu
131
132
133@node getopt-long Example
134@subsection A Short getopt-long Example
135
136This subsection illustrates how @code{getopt-long} is used by presenting
137and dissecting a simple example. The first thing that we need is an
138@dfn{option specification} that tells @code{getopt-long} how to parse
139the command line. This specification is an association list with the
140long option name as the key. Here is how such a specification might
141look:
142
143@lisp
144(define option-spec
145 '((version (single-char #\v) (value #f))
146 (help (single-char #\h) (value #f))))
147@end lisp
148
149This alist tells @code{getopt-long} that it should accept two long
150options, called @emph{version} and @emph{help}, and that these options
151can also be selected by the single-letter abbreviations @emph{v} and
152@emph{h}, respectively. The @code{(value #f)} clauses indicate that
153neither of the options accepts a value.
154
155With this specification we can use @code{getopt-long} to parse a given
156command line:
157
158@lisp
159(define options (getopt-long (command-line) option-spec))
160@end lisp
161
162After this call, @code{options} contains the parsed command line and is
163ready to be examined by @code{option-ref}. @code{option-ref} is called
164like this:
165
166@lisp
167(option-ref options 'help #f)
168@end lisp
169
170@noindent
171It expects the parsed command line, a symbol indicating the option to
172examine, and a default value. The default value is returned if the
173option was not present in the command line, or if the option was present
174but without a value; otherwise the value from the command line is
175returned. Usually @code{option-ref} is called once for each possible
176option that a script supports.
177
178The following example shows a main program which puts all this together
179to parse its command line and figure out what the user wanted.
180
181@lisp
182(define (main args)
183 (let* ((option-spec '((version (single-char #\v) (value #f))
184 (help (single-char #\h) (value #f))))
185 (options (getopt-long args option-spec))
186 (help-wanted (option-ref options 'help #f))
187 (version-wanted (option-ref options 'version #f)))
188 (if (or version-wanted help-wanted)
189 (begin
190 (if version-wanted
191 (display "getopt-long-example version 0.3\n"))
192 (if help-wanted
193 (display "\
194getopt-long-example [options]
195 -v, --version Display version
196 -h, --help Display this help
197")))
198 (begin
199 (display "Hello, World!") (newline)))))
200@end lisp
201
202
203@node Option Specification
204@subsection How to Write an Option Specification
205
206An option specification is an association list (@pxref{Association
207Lists}) with one list element for each supported option. The key of each
208list element is a symbol that names the option, while the value is a
209list of option properties:
210
211@lisp
212OPTION-SPEC ::= '( (OPT-NAME1 (PROP-NAME PROP-VALUE) @dots{})
213 (OPT-NAME2 (PROP-NAME PROP-VALUE) @dots{})
214 (OPT-NAME3 (PROP-NAME PROP-VALUE) @dots{})
215 @dots{}
216 )
217@end lisp
218
219Each @var{opt-name} specifies the long option name for that option. For
220example, a list element with @var{opt-name} @code{background} specifies
221an option that can be specified on the command line using the long
222option @code{--background}. Further information about the option ---
223whether it takes a value, whether it is required to be present in the
224command line, and so on --- is specified by the option properties.
225
226In the example of the preceding subsection, we already saw that a long
227option name can have a equivalent @dfn{short option} character. The
228equivalent short option character can be set for an option by specifying
229a @code{single-char} property in that option's property list. For
230example, a list element like @code{'(output (single-char #\o) @dots{})}
231specifies an option with long name @code{--output} that can also be
232specified by the equivalent short name @code{-o}.
233
234The @code{value} property specifies whether an option requires or
235accepts a value. If the @code{value} property is set to @code{#t}, the
236option requires a value: @code{getopt-long} will signal an error if the
237option name is present without a corresponding value. If set to
238@code{#f}, the option does not take a value; in this case, a non-option
239word that follows the option name in the command line will be treated as
240a non-option argument. If set to the symbol @code{optional}, the option
241accepts a value but does not require one: a non-option word that follows
242the option name in the command line will be interpreted as that option's
243value. If the option name for an option with @code{'(value optional)}
244is immediately followed in the command line by @emph{another} option
245name, the value for the first option is implicitly @code{#t}.
246
247The @code{required?} property indicates whether an option is required to
248be present in the command line. If the @code{required?} property is
249set to @code{#t}, @code{getopt-long} will signal an error if the option
250is not specified.
251
252Finally, the @code{predicate} property can be used to constrain the
253possible values of an option. If used, the @code{predicate} property
254should be set to a procedure that takes one argument --- the proposed
255option value as a string --- and returns either @code{#t} or @code{#f}
256according as the proposed value is or is not acceptable. If the
257predicate procedure returns @code{#f}, @code{getopt-long} will signal an
258error.
259
260By default, options do not have single-character equivalents, are not
261required, and do not take values. Where the list element for an option
262includes a @code{value} property but no @code{predicate} property, the
263option values are unconstrained.
264
265
266@node Command Line Format
267@subsection Expected Command Line Format
268
269In order for @code{getopt-long} to correctly parse a command line, that
270command line must conform to a standard set of rules for how command
271line options are specified. This subsection explains what those rules
272are.
273
274@code{getopt-long} splits a given command line into several pieces. All
275elements of the argument list are classified to be either options or
276normal arguments. Options consist of two dashes and an option name
277(so-called @dfn{long} options), or of one dash followed by a single
278letter (@dfn{short} options).
279
280Options can behave as switches, when they are given without a value, or
281they can be used to pass a value to the program. The value for an
282option may be specified using an equals sign, or else is simply the next
283word in the command line, so the following two invocations are
284equivalent:
285
286@example
287$ ./foo.scm --output=bar.txt
288$ ./foo.scm --output bar.txt
289@end example
290
291Short options can be used instead of their long equivalents and can be
292grouped together after a single dash. For example, the following
293commands are equivalent.
294
295@example
296$ ./foo.scm --version --help
297$ ./foo.scm -v --help
298$ ./foo.scm -vh
299@end example
300
301If an option requires a value, it can only be grouped together with other
302short options if it is the last option in the group; the value is the
303next argument. So, for example, with the following option
304specification ---
305
306@lisp
307((apples (single-char #\a))
308 (blimps (single-char #\b) (value #t))
309 (catalexis (single-char #\c) (value #t)))
310@end lisp
311
312@noindent
313--- the following command lines would all be acceptable:
314
315@example
316$ ./foo.scm -a -b bang -c couth
317$ ./foo.scm -ab bang -c couth
318$ ./foo.scm -ac couth -b bang
319@end example
320
321But the next command line is an error, because @code{-b} is not the last
322option in its combination, and because a group of short options cannot
323include two options that both require values:
324
325@example
326$ ./foo.scm -abc couth bang
327@end example
328
329If an option's value is optional, @code{getopt-long} decides whether the
330option has a value by looking at what follows it in the argument list.
331If the next element is a string, and it does not appear to be an option
332itself, then that string is the option's value.
333
334If the option @code{--} appears in the argument list, argument parsing
335stops there and subsequent arguments are returned as ordinary arguments,
336even if they resemble options. So, with the command line
337
338@example
339$ ./foo.scm --apples "Granny Smith" -- --blimp Goodyear
340@end example
341
342@noindent
343@code{getopt-long} will recognize the @code{--apples} option as having
344the value "Granny Smith", but will not treat @code{--blimp} as an
345option. The strings @code{--blimp} and @code{Goodyear} will be returned
346as ordinary argument strings.
347
348
349@node getopt-long Reference
350@subsection Reference Documentation for @code{getopt-long}
351
8f85c0c6 352@deffn {Scheme Procedure} getopt-long args grammar
a0e07ba4
NJ
353Parse the command line given in @var{args} (which must be a list of
354strings) according to the option specification @var{grammar}.
355
356The @var{grammar} argument is expected to be a list of this form:
357
358@code{((@var{option} (@var{property} @var{value}) @dots{}) @dots{})}
359
360where each @var{option} is a symbol denoting the long option, but
361without the two leading dashes (e.g. @code{version} if the option is
362called @code{--version}).
363
364For each option, there may be list of arbitrarily many property/value
365pairs. The order of the pairs is not important, but every property may
366only appear once in the property list. The following table lists the
367possible properties:
368
369@table @asis
370@item @code{(single-char @var{char})}
371Accept @code{-@var{char}} as a single-character equivalent to
372@code{--@var{option}}. This is how to specify traditional Unix-style
373flags.
374@item @code{(required? @var{bool})}
375If @var{bool} is true, the option is required. @code{getopt-long} will
376raise an error if it is not found in @var{args}.
377@item @code{(value @var{bool})}
378If @var{bool} is @code{#t}, the option accepts a value; if it is
379@code{#f}, it does not; and if it is the symbol @code{optional}, the
380option may appear in @var{args} with or without a value.
381@item @code{(predicate @var{func})}
382If the option accepts a value (i.e. you specified @code{(value #t)} for
383this option), then @code{getopt-long} will apply @var{func} to the
384value, and throw an exception if it returns @code{#f}. @var{func}
385should be a procedure which accepts a string and returns a boolean
386value; you may need to use quasiquotes to get it into @var{grammar}.
387@end table
388@end deffn
389
390@code{getopt-long}'s @var{args} parameter is expected to be a list of
391strings like the one returned by @code{command-line}, with the first
392element being the name of the command. Therefore @code{getopt-long}
393ignores the first element in @var{args} and starts argument
394interpretation with the second element.
395
396@code{getopt-long} signals an error if any of the following conditions
397hold.
398
399@itemize @bullet
400@item
401The option grammar has an invalid syntax.
402
403@item
404One of the options in the argument list was not specified by the
405grammar.
406
407@item
408A required option is omitted.
409
410@item
411An option which requires an argument did not get one.
412
413@item
414An option that doesn't accept an argument does get one (this can only
415happen using the long option @code{--opt=@var{value}} syntax).
416
417@item
418An option predicate fails.
419@end itemize
420
421
422@node option-ref Reference
423@subsection Reference Documentation for @code{option-ref}
424
8f85c0c6 425@deffn {Scheme Procedure} option-ref options key default
a0e07ba4
NJ
426Search @var{options} for a command line option named @var{key} and
427return its value, if found. If the option has no value, but was given,
428return @code{#t}. If the option was not given, return @var{default}.
429@var{options} must be the result of a call to @code{getopt-long}.
430@end deffn
431
432@code{option-ref} always succeeds, either by returning the requested
433option value from the command line, or the default value.
434
435The special key @code{'()} can be used to get a list of all
436non-option arguments.
437
438
439@c Local Variables:
440@c TeX-master: "guile.texi"
441@c End: