Merge branch 'master' into wip-manual-2
[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 7@node Command Line Handling
3229f68b 8@section Handling Command Line Options and Arguments
a0e07ba4
NJ
9
10@c This chapter was written and contributed by Martin Grabmueller.
11
12The ability to accept and handle command line arguments is very
13important when writing Guile scripts to solve particular problems, such
14as extracting information from text files or interfacing with existing
15command line applications. This chapter describes how Guile makes
16command line arguments available to a Guile script, and the utilities
17that Guile provides to help with the processing of command line
18arguments.
19
a0e07ba4
NJ
20When a Guile script is invoked, Guile makes the command line arguments
21accessible via the procedure @code{command-line}, which returns the
22arguments as a list of strings.
23
24For example, if the script
25
26@example
27#! /usr/local/bin/guile -s
28!#
29(write (command-line))
30(newline)
31@end example
32
33@noindent
34is saved in a file @file{cmdline-test.scm} and invoked using the command
35line @code{./cmdline-test.scm bar.txt -o foo -frumple grob}, the output
36is
37
38@example
39("./cmdline-test.scm" "bar.txt" "-o" "foo" "-frumple" "grob")
40@end example
41
42If the script invocation includes a @code{-e} option, specifying a
43procedure to call after loading the script, Guile will call that
44procedure with @code{(command-line)} as its argument. So a script that
45uses @code{-e} doesn't need to refer explicitly to @code{command-line}
46in its code. For example, the script above would have identical
47behaviour if it was written instead like this:
48
49@example
50#! /usr/local/bin/guile \
51-e main -s
52!#
53(define (main args)
54 (write args)
55 (newline))
56@end example
57
58(Note the use of the meta switch @code{\} so that the script invocation
59can include more than one Guile option: @xref{The Meta Switch}.)
60
61These scripts use the @code{#!} POSIX convention so that they can be
62executed using their own file names directly, as in the example command
63line @code{./cmdline-test.scm bar.txt -o foo -frumple grob}. But they
64can also be executed by typing out the implied Guile command line in
65full, as in:
66
67@example
68$ guile -s ./cmdline-test.scm bar.txt -o foo -frumple grob
69@end example
70
71@noindent
72or
73
74@example
75$ guile -e main -s ./cmdline-test2.scm bar.txt -o foo -frumple grob
76@end example
77
78Even when a script is invoked using this longer form, the arguments that
79the script receives are the same as if it had been invoked using the
80short form. Guile ensures that the @code{(command-line)} or @code{-e}
81arguments are independent of how the script is invoked, by stripping off
82the arguments that Guile itself processes.
83
a0e07ba4
NJ
84A script is free to parse and handle its command line arguments in any
85way that it chooses. Where the set of possible options and arguments is
86complex, however, it can get tricky to extract all the options, check
87the validity of given arguments, and so on. This task can be greatly
88simplified by taking advantage of the module @code{(ice-9 getopt-long)},
3229f68b 89which is distributed with Guile, @xref{getopt-long}.
a0e07ba4
NJ
90
91@c Local Variables:
92@c TeX-master: "guile.texi"
93@c End: