X-Git-Url: https://git.jsancho.org/?p=guile-click.git;a=blobdiff_plain;f=click.scm;h=c4f16b01884687a0ac26f097530937a6caadcc06;hp=c3769a448956d0ad78a2a52ba6b195e573da758a;hb=487cd0d4c6c0ee9681cc132e5316ded7fc5dd0f7;hpb=7bb66a7166059976c81aef6e76f205c14b45cc45 diff --git a/click.scm b/click.scm index c3769a4..c4f16b0 100644 --- a/click.scm +++ b/click.scm @@ -19,25 +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 (caar option-spec))) - (cons (cons option (option-ref options option #f)) - (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)))))