]> git.jsancho.org Git - guile-click.git/blobdiff - click.scm
Nested commands
[guile-click.git] / click.scm
index afeb2f914fa1145ad0573108a18f37f9ae84b0f8..c4f16b01884687a0ac26f097530937a6caadcc06 100644 (file)
--- a/click.scm
+++ b/click.scm
 
 (define-module (click)
   #:use-module (ice-9 getopt-long)
+  #:use-module (srfi srfi-1)
+  #:use-module (click args)
+  #:use-module (click command)
   #:use-module (click constant)
-  #:use-module (click display)
+  #:use-module (click help)
   #:use-module (click util)
-  #:export (command))
-
-(define (get-options-value options option-spec)
-  (cond ((null? option-spec)
-         '())
-        (else
-         (let* ((option (car option-spec))
-                (option-name (car option))
-                (default (option-default-value option)))
-           (cons (cons option-name (option-ref options option-name default))
-                 (get-options-value options (cdr option-spec)))))))
-
-(define (command option-spec procedure)
-  (lambda (args)
-    (with-fluids ((%program-name (car args)))
-      (let* ((click-option-spec (append option-spec (list HELP_OPTION)))
-             (options (getopt-long args
-                                   (getopt-long-option-spec click-option-spec))))
-        (if (option-ref options 'help #f)
-            (display-help procedure click-option-spec)
-            (apply procedure (map cdr (get-options-value options option-spec))))))))
+  #:export (command
+            group))
+
+
+(define* (command #:key (name #f) (option-spec '()) (help "") (procedure #f))
+  "Define a new command for the procedure"
+  (group #:name name
+         #:option-spec option-spec
+         #:help help
+         #:procedure procedure))
+
+
+(define* (group #:key (name #f) (option-spec '()) (help "") (procedure #f) (commands '()))
+  "Define a new group with a list of commands associated"
+  (let* ((click-option-spec (append option-spec (list HELP_OPTION)))
+         (new-command (make-command name click-option-spec help procedure commands)))
+    (let ((click-manager
+           (lambda (args)
+            (when (not (program-name))
+              (program-name (car args)))
+             (let ((values (parse-args args click-option-spec)))
+               ;; Call current command
+               (cond ((option-ref values 'help #f)
+                     (display-help new-command))
+                     (else
+                      (when procedure
+                        (apply procedure (map cdr (get-values option-spec values))))
+                      ;; Call nested command (if exists)
+                      (call-nested-command commands values)))))))
+               
+      (set-command-click-manager! new-command click-manager)
+      new-command)))
+
+
+(define (call-nested-command commands values)
+  (let ((next-command-args (cdar values)))
+    (when (not (null? next-command-args))
+      (let* ((next-command-name (car next-command-args))
+             (next-command (find (lambda (command)
+                                  (equal? (command-name command) next-command-name))
+                                commands)))
+       (next-command next-command-args)))))