script-getopt.texi 3.2 KB

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