blob: 0faebf898573a888c3ad93ddcdfbd56cb5dcf210 (
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
|
(define-module (hnh util options)
:use-module (hnh util)
:use-module (ice-9 match)
:use-module (srfi srfi-1)
:use-module (text markup)
:export (getopt-opt
format-arg-help
print-arg-help
))
;; option-assoc → getopt-valid option-assoc
(define (getopt-opt options)
(define ice-9-names '(single-char required? value predicate))
(for (option-name flags ...) in options
(cons option-name
(map (match-lambda
(('value (_ ...)) `(value #t))
(('value (? symbol? _)) `(value optional))
((key v) `(,key ,v)))
(filter (match-lambda ((key _ ...) (memv key ice-9-names)))
flags)))))
;; (name (key value) ...) → sxml
(define (fmt-help option-line)
(match option-line
((name args ...)
(let ((valuefmt (match (assoc-ref args 'value)
[(#t) '(" " (i value))]
[(or #f (#f)) '()]
[(('options options ...))
`(" {" ,(string-join options "|") "}")]
[(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))))))))
(define (format-arg-help options)
(sxml->ansi-text (cons '*TOP* (map sxml->ansi-text (map fmt-help options)))))
(define* (print-arg-help options optional: (port (current-error-port)))
(display (format-arg-help options) port))
|