From 432c741eb5dac653c1a65fae2b8f15c4b444897f Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Fri, 10 Sep 2021 18:27:24 +0200 Subject: [PATCH] Improve reading for default values and prompt --- click.scm | 17 ++++------------- click/util.scm | 21 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/click.scm b/click.scm index afeb2f9..aa80430 100644 --- a/click.scm +++ b/click.scm @@ -24,22 +24,13 @@ #: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) + (values (getopt-long args + (getopt-long-option-spec click-option-spec)))) + (if (option-ref values 'help #f) (display-help procedure click-option-spec) - (apply procedure (map cdr (get-options-value options option-spec)))))))) + (apply procedure (map cdr (get-values option-spec values)))))))) diff --git a/click/util.scm b/click/util.scm index 4fc80ba..e83e153 100644 --- a/click/util.scm +++ b/click/util.scm @@ -18,14 +18,18 @@ (define-module (click util) + #:use-module (ice-9 getopt-long) #:use-module (ice-9 readline) #:use-module (click constant) #:export (getopt-long-option-spec + get-option-default-value + get-values option-default-value option-property program-name %program-name)) + (define %program-name (make-fluid "guile")) (define (program-name) (fluid-ref %program-name)) @@ -45,8 +49,21 @@ (cadr property) default))) -(define* (option-default-value option #:optional (no-prompt #f)) - "Get default value for option, asking user if prompt property is set" +(define (get-values option-spec values) + "Return an associated list with values for all the options in option-spec" + (cond ((null? option-spec) + '()) + (else + (let* ((option (car option-spec)) + (option-name (car option)) + (value (or (option-ref values option-name #f) + (get-option-default-value option)))) + (cons (cons option-name value) + (get-values (cdr option-spec) values)))))) + + +(define* (get-option-default-value option #:optional (no-prompt #f)) + "Get value for option, asking user if prompt property is set" (let ((default (option-property option 'default))) (if (not default) (let ((prompt (option-property option 'prompt))) -- 2.39.2