]> git.jsancho.org Git - guile-click.git/blob - click.scm
Nested commands help message
[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 (srfi srfi-9)
23   #:use-module (srfi srfi-9 gnu)
24   #:use-module (click command)
25   #:use-module (click constant)
26   #:use-module (click help)
27   #:use-module (click util)
28   #:use-module (click value)
29   #:export (command
30             group))
31
32
33 (define* (command #:key (name #f) (option-spec '()) (help "") (procedure #f))
34   "Define a new command for the procedure"
35   (group #:name name
36          #:option-spec option-spec
37          #:help help
38          #:procedure procedure))
39
40
41 (define* (group #:key (name #f) (option-spec '()) (help "") (procedure #f) (commands '()))
42   "Define a new group with a list of commands associated"
43   (let* ((click-option-spec (append option-spec (list HELP_OPTION)))
44          (new-command (make-command name click-option-spec help procedure commands)))
45     (let ((click-manager
46            (lambda (args)
47              (let ((values
48                     (getopt-long args (getopt-long-option-spec click-option-spec))))
49                (if (option-ref values 'help #f)
50                    (let ((program-name (car args)))
51                      (display-help program-name new-command))
52                    (apply procedure (map cdr (get-values option-spec values))))))))
53       (set-command-click-manager! new-command click-manager)
54       new-command)))