1 #! /usr/local/bin/guile -s
5 ;;; This is a command-line factorial calculator. Run like this:
9 ;;; to calculate the factorial of 5
11 ;;; Author: Martin Grabmueller
16 (use-modules (ice-9 getopt-long))
18 ;; This is the grammar for the command line synopsis we expect.
20 (define command-synopsis
21 '((version (single-char #\v) (value #f))
22 (help (single-char #\h) (value #f))))
24 ;; Display version information and exit.
26 (define (display-version)
27 (display "fact 0.0.1\n"))
29 ;; Display the usage help message and exit.
31 (define (display-help)
32 (display "Usage: fact [options...] number\n")
33 (display " --help, -h Show this usage information\n")
34 (display " --version, -v Show version information\n"))
36 ;; Interpret options, if --help or --version was given, print out the
37 ;; requested information and exit. Otherwise, calculate the factorial
40 (define (main options)
41 (let ((help-wanted (option-ref options 'help #f))
42 (version-wanted (option-ref options 'version #f))
43 (args (option-ref options '() '())))
45 ((or version-wanted help-wanted)
50 ((not (= (length args) 1))
53 (display (fact (string->number (car args))))
56 ;; Calculate the factorial of n.
61 (* n (fact (- n 1)))))
63 ;; Call the main program with parsed command line options.
65 (main (getopt-long (command-line) command-synopsis))