]> git.jsancho.org Git - guile-click.git/blob - click/util.scm
Prompt for values
[guile-click.git] / click / util.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 util)
21   #:use-module (ice-9 readline)
22   #:use-module (click constant)
23   #:export (getopt-long-option-spec
24             option-default-value
25             option-property
26             program-name
27             %program-name))
28
29 (define %program-name (make-fluid "guile"))
30 (define (program-name)
31   (fluid-ref %program-name))
32
33 (define (getopt-long-option-spec option-spec)
34   (map (lambda (option)
35          (cons (car option)
36                (cons (list 'value (not (option-property option 'flag)))
37                      (filter (lambda (property)
38                                (not (memq (car property) CLICK_PROPERTIES)))
39                              (cdr option)))))
40        option-spec))
41
42 (define* (option-property option property-name #:optional (default #f))
43   (let ((property (assq property-name (cdr option))))
44     (if property
45         (cadr property)
46         default)))
47
48 (define* (option-default-value option #:optional (no-prompt #f))
49   "Get default value for option, asking user if prompt property is set"
50   (let ((default (option-property option 'default)))
51     (if (not default)
52         (let ((prompt (option-property option 'prompt)))
53           (if prompt
54               (readline (format #f "~a: " prompt))
55               default))
56         default)))