]> git.jsancho.org Git - guile-click.git/blob - click/value.scm
Type conversion
[guile-click.git] / click / value.scm
1 ;;; Click --- Command Line Interface Creation Kit for GNU Guile
2 ;;; Copyright © 2021 Javier Sancho <jsf@jsancho.org>
3 ;;;
4 ;;; This file is part of Click.
5 ;;;
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.
10 ;;;
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.
15 ;;;
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/>.
18
19
20 (define-module (click value)
21   #:use-module (ice-9 getopt-long)
22   #:use-module (ice-9 readline)
23   #:use-module (click util)
24   #:export (get-option-default-value
25             get-values))
26
27
28 (define (get-values option-spec values)
29   "Return an associated list with values for all the options in option-spec"
30   (cond ((null? option-spec)
31          '())
32         (else
33          (let* ((option (car option-spec))
34                 (option-name (car option))
35                 (value (get-normalized-value values option option-name)))
36            (cons (cons option-name value)
37                  (get-values (cdr option-spec) values))))))
38
39
40 (define (get-normalized-value values option option-name)
41   "Get value for option, converting from string to the appropriate value"
42   (let ((convert-proc (assoc-ref (option-type option) 'convert))
43         (value (option-ref values option-name #f)))
44     (cond ((and value (not (option-property option 'flag)))
45            (convert-proc value))
46           (value
47            value)
48           (else
49            (get-option-default-value option)))))
50
51
52 (define* (get-option-default-value option #:optional (no-prompt #f))
53   "Get default value for option, asking user if prompt property is set"
54   (let ((default (option-property option 'default)))
55     (if (not default)
56         (let ((prompt (option-property option 'prompt)))
57           (if prompt
58               (readline (format #f "~a: " prompt))
59               default))
60         default)))