]> git.jsancho.org Git - guile-click.git/blobdiff - click/util.scm
Nested commands
[guile-click.git] / click / util.scm
index e83e15319a915d16cb43e2ab4cfc14efe207126b..827a4a9b2e12885fa179317b56dbdfe5d2334638 100644 (file)
 
 
 (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))
+            option-type
+           program-name))
 
 
-(define %program-name (make-fluid "guile"))
-(define (program-name)
-  (fluid-ref %program-name))
-
 (define (getopt-long-option-spec option-spec)
+  "Transform click option spec into getopt-long format"
   (map (lambda (option)
          (cons (car option)
                (cons (list 'value (not (option-property option 'flag)))
                              (cdr option)))))
        option-spec))
 
+
 (define* (option-property option property-name #:optional (default #f))
+  "Return the option property with a given name"
   (let ((property (assq property-name (cdr option))))
     (if property
         (cadr property)
         default)))
 
-(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 program-name (make-parameter #f))
 
 
-(define* (get-option-default-value option #:optional (no-prompt #f))
-  "Get value for option, asking user if prompt property is set"
+;; Types
+(define TYPE-TEXT
+  `((description . "TEXT")
+    (convert . ,identity)))
+(define TYPE-INTEGER
+  `((description . "INTEGER")
+    (convert . ,string->number)))
+(define TYPE-NUMBER
+  `((description . "NUMBER")
+    (convert . ,string->number)))
+
+(define (option-type option)
+  "Return allowed type for the value in the option"
   (let ((default (option-property option 'default)))
-    (if (not default)
-        (let ((prompt (option-property option 'prompt)))
-          (if prompt
-              (readline (format #f "~a: " prompt))
-              default))
-        default)))
+    (cond ((not default)
+           TYPE-TEXT)
+          ((integer? default)
+           TYPE-INTEGER)
+          ((number? default)
+           TYPE-NUMBER)
+          (else
+           TYPE-TEXT))))