]> git.jsancho.org Git - guile-click.git/blob - click.scm
WIP: Nested commands
[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 (srfi srfi-1)
22   #:use-module (click args)
23   #:use-module (click command)
24   #:use-module (click constant)
25   #:use-module (click help)
26   #:use-module (click util)
27   #:export (command
28             group))
29
30
31 (define* (command #:key (name #f) (option-spec '()) (help "") (procedure #f))
32   "Define a new command for the procedure"
33   (group #:name name
34          #:option-spec option-spec
35          #:help help
36          #:procedure procedure))
37
38
39 (define* (group #:key (name #f) (option-spec '()) (help "") (procedure #f) (commands '()))
40   "Define a new group with a list of commands associated"
41   (let* ((click-option-spec (append option-spec (list HELP_OPTION)))
42          (new-command (make-command name click-option-spec help procedure commands)))
43     (let ((click-manager
44            (lambda (args)
45              (let ((values (parse-args args click-option-spec)))
46                ;; Call current command
47                (cond ((option-ref values 'help #f)
48                       (let ((program-name (car args)))
49                         (display-help program-name new-command)))
50                      (else
51                       (when procedure
52                         (apply procedure (map cdr (get-values option-spec values))))
53                       ;; Call nested command (if exists)
54                       (call-nested-command commands values)))))))
55                
56       (set-command-click-manager! new-command click-manager)
57       new-command)))
58
59
60 (define (call-nested-command commands values)
61   (let ((next-command-args (cdar values)))
62     (when (not (null? next-command-args))
63       (let* ((next-command-name (car next-command-args))
64              (next-command (find (lambda (command) (
65         (display next-command-args)(newline)
66         (display next-command-name) (newline)))))