| 1 |
#! /usr/local/bin/guile -s |
| 2 |
!# |
| 3 |
;;; Commentary: |
| 4 |
|
| 5 |
;;; This is the famous Hello-World-program, written for Guile. It is a |
| 6 |
;;; little bit enhanced in that it understands the command line options |
| 7 |
;;; `--help' (-h) and `--version' (-v), which print a short usage |
| 8 |
;;; decription or version information, respectively. |
| 9 |
|
| 10 |
;;; Author: Martin Grabmueller |
| 11 |
;;; Date: 2001-05-29 |
| 12 |
|
| 13 |
;;; Code: |
| 14 |
|
| 15 |
(use-modules (ice-9 getopt-long)) |
| 16 |
|
| 17 |
;; This is the grammar for the command line synopsis we expect. |
| 18 |
;; |
| 19 |
(define command-synopsis |
| 20 |
'((version (single-char #\v) (value #f)) |
| 21 |
(help (single-char #\h) (value #f)))) |
| 22 |
|
| 23 |
;; Display version information. |
| 24 |
;; |
| 25 |
(define (display-version) |
| 26 |
(display "hello 0.0.1\n")) |
| 27 |
|
| 28 |
;; Display the usage help message. |
| 29 |
;; |
| 30 |
(define (display-help) |
| 31 |
(display "Usage: hello [options...]\n") |
| 32 |
(display " --help, -h Show this usage information\n") |
| 33 |
(display " --version, -v Show version information\n")) |
| 34 |
|
| 35 |
;; Interpret options, if --help or --version was given, print out the |
| 36 |
;; requested information and exit. Otherwise, print the famous |
| 37 |
;; message. |
| 38 |
;; |
| 39 |
(define (main options) |
| 40 |
(let ((help-wanted (option-ref options 'help #f)) |
| 41 |
(version-wanted (option-ref options 'version #f))) |
| 42 |
(if (or version-wanted help-wanted) |
| 43 |
(begin |
| 44 |
(if version-wanted |
| 45 |
(display-version)) |
| 46 |
(if help-wanted |
| 47 |
(display-help))) |
| 48 |
(begin |
| 49 |
(display "Hello, World!") (newline))))) |
| 50 |
|
| 51 |
;; Call the main program with parsed command line options. |
| 52 |
;; |
| 53 |
(main (getopt-long (command-line) command-synopsis)) |
| 54 |
|
| 55 |
;; Local variables: |
| 56 |
;; mode: scheme |
| 57 |
;; End: |