]> git.jsancho.org Git - guile-click.git/blob - click/util.scm
Groups support
[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 (click constant)
22   #:export (getopt-long-option-spec
23             option-property
24             option-type))
25
26
27 (define (getopt-long-option-spec option-spec)
28   "Transform click option spec into getopt-long format"
29   (map (lambda (option)
30          (cons (car option)
31                (cons (list 'value (not (option-property option 'flag)))
32                      (filter (lambda (property)
33                                (not (memq (car property) CLICK_PROPERTIES)))
34                              (cdr option)))))
35        option-spec))
36
37
38 (define* (option-property option property-name #:optional (default #f))
39   "Return the option property with a given name"
40   (let ((property (assq property-name (cdr option))))
41     (if property
42         (cadr property)
43         default)))
44
45
46 ;; Types
47 (define TYPE-TEXT
48   `((description . "TEXT")
49     (convert . ,identity)))
50 (define TYPE-INTEGER
51   `((description . "INTEGER")
52     (convert . ,string->number)))
53 (define TYPE-NUMBER
54   `((description . "NUMBER")
55     (convert . ,string->number)))
56
57 (define (option-type option)
58   "Return allowed type for the value in the option"
59   (let ((default (option-property option 'default)))
60     (cond ((not default)
61            TYPE-TEXT)
62           ((integer? default)
63            TYPE-INTEGER)
64           ((number? default)
65            TYPE-NUMBER)
66           (else
67            TYPE-TEXT))))