1 ;;; Click --- Command Line Interface Creation Kit for GNU Guile
2 ;;; Copyright © 2021 Javier Sancho <jsf@jsancho.org>
4 ;;; This file is part of Click.
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.
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.
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/>.
20 (define-module (click)
21 #:use-module (ice-9 getopt-long)
22 #:use-module (srfi srfi-1)
23 #:use-module (click args)
24 #:use-module (click command)
25 #:use-module (click constant)
26 #:use-module (click help)
27 #:use-module (click util)
32 (define* (command #:key (name #f) (option-spec '()) (help "") (procedure #f))
33 "Define a new command for the procedure"
35 #:option-spec option-spec
37 #:procedure procedure))
40 (define* (group #:key (name #f) (option-spec '()) (help "") (procedure #f) (commands '()))
41 "Define a new group with a list of commands associated"
42 (let* ((click-option-spec (append option-spec (list HELP_OPTION)))
43 (new-command (make-command name click-option-spec help procedure commands)))
46 (when (not (program-name))
47 (program-name (car args)))
48 (let ((values (parse-args args click-option-spec)))
49 ;; Call current command
50 (cond ((option-ref values 'help #f)
51 (display-help new-command))
54 (apply procedure (map cdr (get-values option-spec values))))
55 ;; Call nested command (if exists)
56 (call-nested-command commands values)))))))
58 (set-command-click-manager! new-command click-manager)
62 (define (call-nested-command commands values)
63 (let ((next-command-args (cdar values)))
64 (when (not (null? next-command-args))
65 (let* ((next-command-name (car next-command-args))
66 (next-command (find (lambda (command)
67 (equal? (command-name command) next-command-name))
69 (next-command next-command-args)))))