diff options
author | Martin Grabmüller <mgrabmue@cs.tu-berlin.de> | 2001-05-31 15:54:25 +0000 |
---|---|---|
committer | Martin Grabmüller <mgrabmue@cs.tu-berlin.de> | 2001-05-31 15:54:25 +0000 |
commit | 2de7ddb7669c6a1f964fa9ad9a874523b26f4f34 (patch) | |
tree | d1aa9e7ee83c761f46be62f9698df1471c40e5f6 /examples/scripts | |
parent | 413a1367e205e594685f4686bc4d31d670d70511 (diff) | |
download | guile-2de7ddb7669c6a1f964fa9ad9a874523b26f4f34.tar.gz |
Added the new `examples' directory to the distribution.
Diffstat (limited to 'examples/scripts')
-rw-r--r-- | examples/scripts/Makefile.am | 22 | ||||
-rw-r--r-- | examples/scripts/README | 33 | ||||
-rwxr-xr-x | examples/scripts/fact | 70 | ||||
-rwxr-xr-x | examples/scripts/hello | 58 | ||||
-rw-r--r-- | examples/scripts/simple-hello.scm | 14 |
5 files changed, 197 insertions, 0 deletions
diff --git a/examples/scripts/Makefile.am b/examples/scripts/Makefile.am new file mode 100644 index 000000000..ff6173086 --- /dev/null +++ b/examples/scripts/Makefile.am @@ -0,0 +1,22 @@ +## Process this file with Automake to create Makefile.in +## +## Copyright (C) 2001 Free Software Foundation, Inc. +## +## This file is part of GUILE. +## +## GUILE is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as +## published by the Free Software Foundation; either version 2, or +## (at your option) any later version. +## +## GUILE is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public +## License along with GUILE; see the file COPYING. If not, write +## to the Free Software Foundation, Inc., 59 Temple Place, Suite +## 330, Boston, MA 02111-1307 USA + +EXTRA_DIST = README simple-hello.scm hello fact diff --git a/examples/scripts/README b/examples/scripts/README new file mode 100644 index 000000000..491007c27 --- /dev/null +++ b/examples/scripts/README @@ -0,0 +1,33 @@ + -*- text -*- + +This directory includes examples which show how to write scripts using +Guile. + +The descriptions below assume that you have a working copy of Guile +installed and available with the standard installation prefix +`/usr/local'. + +simple-hello.scm: + + The simplest "Hello World!" program for Guile. Run it like this: + + $ guile -s simple-hello.scm + +hello: + + An advanced version of the script above, with command line handling + for the important options --help and --version. Run it like this: + + ./hello + + or + + guile -s hello + +fact: + + Command-line factorial calculator. Run it like this: + + ./fact 5 + + to calculate the factorial of 5. diff --git a/examples/scripts/fact b/examples/scripts/fact new file mode 100755 index 000000000..90eecd7c2 --- /dev/null +++ b/examples/scripts/fact @@ -0,0 +1,70 @@ +#! /usr/local/bin/guile -s +!# +;;; Commentary: + +;;; This is a command-line factorial calculator. Run like this: +;;; +;;; ./fact 5 +;;; +;;; to calculate the factorial of 5 + +;;; Author: Martin Grabmueller +;;; Date: 2001-05-29 + +;;; Code: + +(use-modules (ice-9 getopt-long)) + +;; This is the grammar for the command line synopsis we expect. +;; +(define command-synopsis + '((version (single-char #\v) (value #f)) + (help (single-char #\h) (value #f)))) + +;; Display version information and exit. +;; +(define (display-version) + (display "fact 0.0.1\n")) + +;; Display the usage help message and exit. +;; +(define (display-help) + (display "Usage: fact [options...] number\n") + (display " --help, -h Show this usage information\n") + (display " --version, -v Show version information\n")) + +;; Interpret options, if --help or --version was given, print out the +;; requested information and exit. Otherwise, calculate the factorial +;; of the argument. +;; +(define (main options) + (let ((help-wanted (option-ref options 'help #f)) + (version-wanted (option-ref options 'version #f)) + (args (option-ref options '() '()))) + (cond + ((or version-wanted help-wanted) + (if version-wanted + (display-version)) + (if help-wanted + (display-help))) + ((not (= (length args) 1)) + (display-help)) + (else + (display (fact (string->number (car args)))) + (newline))))) + +;; Calculate the factorial of n. +;; +(define (fact n) + (if (< n 2) + 1 + (* n (fact (- n 1))))) + +;; Call the main program with parsed command line options. +;; +(main (getopt-long (command-line) command-synopsis)) + +;; Local variables: +;; mode: scheme +;; End: + diff --git a/examples/scripts/hello b/examples/scripts/hello new file mode 100755 index 000000000..4108db400 --- /dev/null +++ b/examples/scripts/hello @@ -0,0 +1,58 @@ +#! /usr/local/bin/guile -s +!# +;;; Commentary: + +;;; This is the famous Hello-World-program, written for Guile. It is a +;;; little bit enhanced in that it understands the command line options +;;; `--help' (-h) and `--version' (-v), which print a short usage +;;; decription or version information, respectively. + +;;; Author: Martin Grabmueller +;;; Date: 2001-05-29 + +;;; Code: + +(use-modules (ice-9 getopt-long)) + +;; This is the grammar for the command line synopsis we expect. +;; +(define command-synopsis + '((version (single-char #\v) (value #f)) + (help (single-char #\h) (value #f)))) + +;; Display version information and exit. +;; +(define (display-version) + (display "hello 0.0.1\n")) + +;; Display the usage help message and exit. +;; +(define (display-help) + (display "Usage: hello [options...]\n") + (display " --help, -h Show this usage information\n") + (display " --version, -v Show version information\n")) + +;; Interpret options, if --help or --version was given, print out the +;; requested information and exit. Otherwise, print the famous +;; message. +;; +(define (main options) + (let ((help-wanted (option-ref options 'help #f)) + (version-wanted (option-ref options 'version #f))) + (if (or version-wanted help-wanted) + (begin + (if version-wanted + (display-version)) + (if help-wanted + (display-help))) + (begin + (display "Hello, World!") (newline))))) + +;; Call the main program with parsed command line options. +;; +(main (getopt-long (command-line) command-synopsis)) + +;; Local variables: +;; mode: scheme +;; End: + diff --git a/examples/scripts/simple-hello.scm b/examples/scripts/simple-hello.scm new file mode 100644 index 000000000..713a1aee4 --- /dev/null +++ b/examples/scripts/simple-hello.scm @@ -0,0 +1,14 @@ +;;; Commentary: + +;;; This is the famous Hello-World-program, written for Guile. +;;; +;;; For an advanced version, see the script `hello' in the same +;;; directory. + +;;; Author: Martin Grabmueller +;;; Date: 2001-05-29 + +;;; Code: + +(display "Hello, World!") +(newline) |