]> git.jsancho.org Git - guile-click.git/blob - click/command.scm
Nested commands help message
[guile-click.git] / click / command.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 command)
21   #:export (command?
22             command-commands
23             command-help-text
24             command-name
25             command-option-spec
26             command-procedure
27             group?
28             make-command
29             set-command-click-manager!))
30
31
32 ;; Command VTable
33 (define <command-vtable> (make-struct/no-tail <applicable-struct-vtable> 'pwpwpwpwpwpw))
34
35 (define (command-printer struct port)
36   (let ((type (if (group? struct) "Group" "Command"))
37         (name (command-name struct)))
38     (if name
39         (format port "<~a ~a>" type name)
40         (format port "<~a>" type))))
41
42 (struct-set! <command-vtable> vtable-index-printer command-printer)
43
44
45 ;; Command API
46 (define (make-command name option-spec help-text procedure commands)
47   (make-struct/no-tail <command-vtable> #f name option-spec help-text procedure commands))
48
49 (define (set-command-click-manager! command click-manager)
50   (struct-set! command 0 click-manager))
51
52 (define (command-name command)
53   (struct-ref command 1))
54
55 (define (command-option-spec command)
56   (struct-ref command 2))
57
58 (define (command-help-text command)
59   (struct-ref command 3))
60
61 (define (command-procedure command)
62   (struct-ref command 4))
63
64 (define (command-commands command)
65   (struct-ref command 5))
66
67 (define (command? command)
68   (and (equal? (struct-vtable command) <command-vtable>)
69        (null? (command-commands command))))
70
71 (define (group? command)
72   (and (equal? (struct-vtable command) <command-vtable>)
73        (not (null? (command-commands command)))))