aboutsummaryrefslogtreecommitdiff
path: root/module/calp/util/options.scm
blob: e715821051c9588dd773068e0b28a3620565d5a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(define-module (calp util options)
  :use-module (calp util)
  :use-module (srfi srfi-1)
)

;; option-assoc → getopt-valid option-assoc
(define-public (getopt-opt options)
  (map (lambda (optline)
         (cons (car optline)
               (map (lambda (opt-field)
                      (cons (car opt-field)
                            (cond [(eq? 'value (car opt-field))
                                   (cond [(cadr opt-field)
                                          list? => (lambda (opts)
                                                     (case (car opts)
                                                       ;; TODO this should also generate a validator
                                                       ((options) '(#t))
                                                       (else '(#t))))]
                                         [(symbol? (cadr opt-field)) '(optional)]
                                         [else (cdr opt-field)])]
                                  [else (cdr opt-field)])))
                    (lset-intersection (lambda (a b) (eqv? b (car a)))
                                       (cdr optline)
                                       '(single-char required? value predicate)))))
       options))




;; (name (key value) ...) → sxml
(define (fmt-help option-line)
  (let ((name (car option-line))
        (args (cdr option-line)))
    (let ((valuefmt (case (and=> (assoc-ref args 'value) car)
                      [(#t) '(" " (i value))]
                      [(#f) '()]
                      [else => (lambda (s)
                                 (if (list? s)
                                     (case (car s)
                                       [(options)
                                        `(" {" ,(string-join (cdr s) "|") "}")])
                                     `(" [" (i ,s) "]")))])))
      `(*TOP* (b "--" ,name) ,@valuefmt
              ,@(awhen (assoc-ref args 'single-char)
                       `("," (ws)
                         (b "-" ,(car it)) 
                         ,@valuefmt))
              (br)
              ,@(awhen (assoc-ref args 'description)
                       `((blockquote ,@it)
                         (br)))))))

(use-modules (text markup))

(define-public (format-arg-help options)
  (sxml->ansi-text (cons '*TOP* (map sxml->ansi-text (map fmt-help options)))))

(define*-public (print-arg-help options optional: (port (current-error-port)))
  (display (format-arg-help options) port))