X-Git-Url: https://git.jsancho.org/?p=guile-click.git;a=blobdiff_plain;f=click.scm;h=c4f16b01884687a0ac26f097530937a6caadcc06;hp=afeb2f914fa1145ad0573108a18f37f9ae84b0f8;hb=487cd0d4c6c0ee9681cc132e5316ded7fc5dd0f7;hpb=a2c12dede99c0d296194699e025a2e541c8a98a0 diff --git a/click.scm b/click.scm index afeb2f9..c4f16b0 100644 --- a/click.scm +++ b/click.scm @@ -19,27 +19,51 @@ (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)))))