]> git.jsancho.org Git - guile-click.git/blob - click.scm
Initial commit
[guile-click.git] / click.scm
1 ;;; Click --- Command Line Interface Creation Kit for GNU Guile
2 ;;; Copyright © 2021 Javier Sancho <jsf@jsancho.org>
3 ;;;
4 ;;; This file is part of Click.
5 ;;;
6 ;;; Click is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; Click is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;;; General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with Click.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 (define-module (click)
21   #:use-module (ice-9 getopt-long)
22   #:use-module (click constant)
23   #:use-module (click display)
24   #:use-module (click util)
25   #:export (command))
26
27 (define (get-options-value options option-spec)
28   (cond ((null? option-spec)
29          '())
30         (else
31          (let ((option (caar option-spec)))
32            (cons (cons option (option-ref options option #f))
33                  (get-options-value options (cdr option-spec)))))))
34
35 (define (command option-spec procedure)
36   (lambda (args)
37     (with-fluids ((%program-name (car args)))
38       (let* ((click-option-spec (append option-spec (list HELP_OPTION)))
39              (options (getopt-long args
40                                    (getopt-long-option-spec click-option-spec))))
41         (if (option-ref options 'help #f)
42             (display-help procedure click-option-spec)
43             (apply procedure (map cdr (get-options-value options option-spec))))))))