| 1 |
|
| 2 |
|
| 3 |
;;; WORK IN PROGRESS, NOT FOR CONSUMPTION |
| 4 |
;;; TODO: long options |
| 5 |
;;; multiple options in one go, e.g., -XYZ |
| 6 |
;;; concat value with option, e.g., -Xxvalue |
| 7 |
;;; usage error message [ok?] |
| 8 |
;;; -h --help should not be user-defined |
| 9 |
;;; check duplicate options |
| 10 |
|
| 11 |
(library (command-line) |
| 12 |
(export command-line-interface) |
| 13 |
(import (rnrs)) |
| 14 |
|
| 15 |
(define (dispatch-opts arguments data* proc*) |
| 16 |
(define (print p . args) |
| 17 |
(for-each (lambda (x) (display x p)) args)) |
| 18 |
(define (print-usage detailed?) |
| 19 |
(define-record-type f (fields id char type def)) |
| 20 |
(define (mkf x id) |
| 21 |
(make-f id (car x) (cadr x) (cddr x))) |
| 22 |
(define (get type ls) |
| 23 |
(filter (lambda (x) (eq? (f-type x) type)) ls)) |
| 24 |
(define (fmt-req p) |
| 25 |
(lambda (x) |
| 26 |
(print p " -" (f-char x) " <" (f-id x) ">"))) |
| 27 |
(define (fmt-req-no-value p) |
| 28 |
(lambda (x) |
| 29 |
(display " -" p) |
| 30 |
(display (f-char x) p))) |
| 31 |
(define (fmt-z p c) |
| 32 |
(lambda (x) |
| 33 |
(print p " [-" (f-char x) " <" (f-id x) ">]" c))) |
| 34 |
(define (fmt-<> p) |
| 35 |
(lambda (x) |
| 36 |
(display " <" p) |
| 37 |
(display x p) |
| 38 |
(display ">" p))) |
| 39 |
(define (synopsis f* args args-rest) |
| 40 |
(let ([opt* (get 'optional f*)] |
| 41 |
[flag* (get 'flag f*)] |
| 42 |
[req0* (get 'required0 f*)] |
| 43 |
[req1* (get 'required1 f*)] |
| 44 |
[z0* (get 'zero-plus f*)] |
| 45 |
[z1* (get 'one-plus f*)]) |
| 46 |
(let-values ([(p e) (open-string-output-port)]) |
| 47 |
(display (car arguments) p) |
| 48 |
(for-each (fmt-req-no-value p) req0*) |
| 49 |
(unless (null? flag*) |
| 50 |
(display " [-" p) |
| 51 |
(apply print p (map f-char flag*)) |
| 52 |
(display "]" p)) |
| 53 |
(for-each (fmt-z p "") opt*) |
| 54 |
(for-each (fmt-z p "*") z0*) |
| 55 |
(for-each (fmt-z p "+") z1*) |
| 56 |
(for-each (fmt-req p) req1*) |
| 57 |
(for-each (fmt-<> p) args) |
| 58 |
(when args-rest |
| 59 |
((fmt-<> p) args-rest) |
| 60 |
(display " ..." p)) |
| 61 |
(e)))) |
| 62 |
(define (print-usage-line help fields field-ids args args-rest dash-rest) |
| 63 |
(let ([f* (map mkf fields field-ids)]) |
| 64 |
(display " ") |
| 65 |
(display (synopsis f* args args-rest)) |
| 66 |
(newline) |
| 67 |
(unless (string=? help "") |
| 68 |
(display " ") |
| 69 |
(display help) |
| 70 |
(newline)) |
| 71 |
(when detailed? |
| 72 |
(let ([def* (filter f-def (get 'optional f*))]) |
| 73 |
(unless (null? def*) |
| 74 |
(for-each |
| 75 |
(lambda (x) |
| 76 |
(print (current-output-port) |
| 77 |
" -" (f-char x) |
| 78 |
" defaults to " (f-def x) |
| 79 |
"\n")) |
| 80 |
def*))) |
| 81 |
(newline)))) |
| 82 |
(display "\nUsage:\n") |
| 83 |
(for-each (lambda (x) (apply print-usage-line x)) data*) |
| 84 |
(print-usage-line "Display this help message" |
| 85 |
'([#\h required0 . #f]) |
| 86 |
'(dummy) |
| 87 |
'() |
| 88 |
#f |
| 89 |
#f) |
| 90 |
#f) |
| 91 |
(define (arguments-match) |
| 92 |
(define-condition-type &help &condition |
| 93 |
make-help-condition help-condition? |
| 94 |
(extended? help-extended?)) |
| 95 |
(define-condition-type &unmatched &condition |
| 96 |
make-unmatched-condition unmatched-condition?) |
| 97 |
(define (help x) |
| 98 |
(raise (make-help-condition x))) |
| 99 |
(define (unmatched) |
| 100 |
(raise (make-unmatched-condition))) |
| 101 |
(define (option? x) |
| 102 |
(or (equal? x "--help") ;;; hack |
| 103 |
(and (string? x) |
| 104 |
(>= (string-length x) 2) |
| 105 |
(char=? (string-ref x 0) #\-) |
| 106 |
(not (char=? (string-ref x 1) #\-))))) |
| 107 |
;;; |
| 108 |
(define (fill-char-opt c ls fields) |
| 109 |
;;; field = [c required0 . _] ; requires 0 args |
| 110 |
;;; | [c required1 . _] ; requires 1 arg |
| 111 |
;;; | [c flag . default] ; toggles default to #t |
| 112 |
;;; | [c zero-plus . reversed-list] |
| 113 |
;;; | [c one-plus . reversed-list] |
| 114 |
;;; | [c optional . default] ; overridden by value |
| 115 |
;;; | [c ok . value] ; already used, not on input |
| 116 |
(let f ([fields fields]) |
| 117 |
(when (null? fields) (unmatched)) |
| 118 |
(let ([field (car fields)]) |
| 119 |
(if (char=? c (car field)) |
| 120 |
(let ([t (cadr field)]) |
| 121 |
(case t |
| 122 |
[(required1 optional) |
| 123 |
(when (null? ls) (unmatched)) |
| 124 |
(let ([val (car ls)] [ls (cdr ls)]) |
| 125 |
(values (cons (cons* c 'ok val) (cdr fields)) ls))] |
| 126 |
[(flag) |
| 127 |
(values (cons (cons* c 'ok #t) (cdr fields)) ls)] |
| 128 |
[(zero-plus one-plus) |
| 129 |
(when (null? ls) (unmatched)) |
| 130 |
(let ([val (car ls)]) |
| 131 |
(values |
| 132 |
(cons (cons* c 'zero-plus (cons val (cddr field))) |
| 133 |
(cdr fields)) |
| 134 |
(cdr ls)))] |
| 135 |
[else (unmatched)])) |
| 136 |
(let-values ([(fields ls) (f (cdr fields))]) |
| 137 |
(values (cons field fields) ls)))))) |
| 138 |
;;; |
| 139 |
(define (fill-option a ls fields) |
| 140 |
(when (string=? a "--help") (help #t)) |
| 141 |
(if (= (string-length a) 2) |
| 142 |
(let ([char (string-ref a 1)]) |
| 143 |
(when (char=? char #\h) (help #f)) |
| 144 |
(fill-char-opt char ls fields)) |
| 145 |
(error 'fill-option "not yet"))) |
| 146 |
;;; |
| 147 |
(define (match-fields fields ls) |
| 148 |
(if (null? ls) |
| 149 |
(values fields ls) |
| 150 |
(let ([a (car ls)]) |
| 151 |
(if (option? a) |
| 152 |
(let-values ([(fields ls) (fill-option a (cdr ls) fields)]) |
| 153 |
(match-fields fields ls)) |
| 154 |
(values fields ls))))) |
| 155 |
;;; |
| 156 |
(define (match-args args ls) |
| 157 |
(cond |
| 158 |
[(null? args) (values '() ls)] |
| 159 |
[(null? ls) (unmatched)] |
| 160 |
[else |
| 161 |
(let ([a (car ls)]) |
| 162 |
(when (option? a) (unmatched)) |
| 163 |
(let-values ([(a* ls) (match-args (cdr args) (cdr ls))]) |
| 164 |
(values (cons a a*) ls)))])) |
| 165 |
;;; |
| 166 |
(define (match-args-rest a/f ls) |
| 167 |
(if a/f |
| 168 |
(let-values ([(x ls) |
| 169 |
(let f ([ls ls]) |
| 170 |
(if (null? ls) |
| 171 |
(values '() ls) |
| 172 |
(let ([a (car ls)]) |
| 173 |
(if (string=? a "--") |
| 174 |
(values '() ls) |
| 175 |
(if (option? a) |
| 176 |
(unmatched) |
| 177 |
(let-values ([(a* ls) (f (cdr ls))]) |
| 178 |
(values (cons a a*) ls)))))))]) |
| 179 |
(values (list x) ls)) |
| 180 |
(if (or (null? ls) (string=? (car ls) "--")) |
| 181 |
(values '() ls) |
| 182 |
(unmatched)))) |
| 183 |
;;; |
| 184 |
(define (match-dash-rest a/f ls) |
| 185 |
(if a/f |
| 186 |
(if (null? ls) |
| 187 |
'(()) |
| 188 |
(if (string=? (car ls) "--") |
| 189 |
(list (cdr ls)) |
| 190 |
(unmatched))) |
| 191 |
(if (null? ls) '() (unmatched)))) |
| 192 |
;;; |
| 193 |
(define (fix-field x) |
| 194 |
(let ([type (cadr x)] [value (cddr x)]) |
| 195 |
(case type |
| 196 |
[(ok flag optional) value] |
| 197 |
[(zero-plus) (reverse value)] |
| 198 |
[else (unmatched)]))) |
| 199 |
;;; |
| 200 |
(define (match _help fields _field-ids args args-rest dash-rest) |
| 201 |
(cons (car arguments) |
| 202 |
(let*-values ([(fields ls) (match-fields fields (cdr arguments))] |
| 203 |
[(fields) (map fix-field fields)] |
| 204 |
[(args ls) (match-args args ls)] |
| 205 |
[(args-rest ls) (match-args-rest args-rest ls)] |
| 206 |
[(dash-rest) (match-dash-rest dash-rest ls)]) |
| 207 |
(append fields args args-rest dash-rest)))) |
| 208 |
;;; |
| 209 |
(guard (con |
| 210 |
[(help-condition? con) |
| 211 |
(print-usage (help-extended? con))]) |
| 212 |
(let f ([data* data*] [proc* proc*]) |
| 213 |
(if (null? data*) |
| 214 |
(help #f) |
| 215 |
(guard (con |
| 216 |
[(unmatched-condition? con) |
| 217 |
(f (cdr data*) (cdr proc*))]) |
| 218 |
(apply (car proc*) (apply match (car data*)))))))) |
| 219 |
(arguments-match)) |
| 220 |
|
| 221 |
(define-syntax command-line-interface |
| 222 |
(lambda (stx) |
| 223 |
(define (parse-format stx) |
| 224 |
(define (err str x) |
| 225 |
(syntax-violation #f str stx x)) |
| 226 |
(define (prep-str stx) |
| 227 |
(let* ([str (syntax->datum stx)] |
| 228 |
[n (string-length str)] |
| 229 |
[err (lambda (why) (err why stx))]) |
| 230 |
(when (< n 2) (err "invalid option string")) |
| 231 |
(unless (char=? (string-ref str 0) #\-) |
| 232 |
(err "option string must start with a dash: -")) |
| 233 |
(cons (string-ref str 1) |
| 234 |
(cond |
| 235 |
[(= n 2) #'(required1)] |
| 236 |
[else |
| 237 |
(case (string-ref str 2) |
| 238 |
[(#\?) #'(flag . #f)] |
| 239 |
[(#\*) #'(zero-plus . ())] |
| 240 |
[(#\+) #'(one-plus . ())] |
| 241 |
[(#\=) |
| 242 |
(cons #'optional (substring str 3 n))] |
| 243 |
[else (err "invalid option")])])))) |
| 244 |
(define (dots? x) |
| 245 |
(and (identifier? x) |
| 246 |
(free-identifier=? x #'(... ...)))) |
| 247 |
(define (id? x) |
| 248 |
(and (identifier? x) (not (dots? x)))) |
| 249 |
;;; |
| 250 |
(define (command-line-interface ls) |
| 251 |
(define (str? x) |
| 252 |
(let ([d (syntax->datum x)]) |
| 253 |
(and (string? d) (not (string=? d "--"))))) |
| 254 |
(define (parse-head x) |
| 255 |
(syntax-case x () |
| 256 |
[(prog . rest) |
| 257 |
(if (id? #'prog) |
| 258 |
(values #'prog #'rest) |
| 259 |
(err "pattern head is not an identifier" #'prog))] |
| 260 |
[_ (err "invalid pattern" x)])) |
| 261 |
(define (parse-opts x) |
| 262 |
(syntax-case x () |
| 263 |
[(str id . rest) |
| 264 |
(and (id? #'id) (str? #'str)) |
| 265 |
(let-values ([(opt-strs opt-ids rest) (parse-opts #'rest)]) |
| 266 |
(values (cons (prep-str #'str) opt-strs) |
| 267 |
(cons #'id opt-ids) rest))] |
| 268 |
[_ (values '() '() x)])) |
| 269 |
(define (parse-args x) |
| 270 |
(syntax-case x () |
| 271 |
[(id dots . rest) |
| 272 |
(and (id? #'id) (dots? #'dots)) |
| 273 |
(values '() #'id #'rest)] |
| 274 |
[(id . rest) (id? #'id) |
| 275 |
(let-values ([(args args-rest rest) (parse-args #'rest)]) |
| 276 |
(values (cons #'id args) args-rest rest))] |
| 277 |
[_ (values '() #f x)])) |
| 278 |
(define (parse-tail x) |
| 279 |
(syntax-case x () |
| 280 |
[("--" id) (id? #'id) #'id] |
| 281 |
[() #f] |
| 282 |
[_ (err "invalid pattern segment" x)])) |
| 283 |
(let-values ([(prog ls) (parse-head ls)]) |
| 284 |
(let-values ([(opts opt-ids ls) (parse-opts ls)]) |
| 285 |
(let-values ([(args args-rest ls) (parse-args ls)]) |
| 286 |
(values prog opts opt-ids args args-rest (parse-tail ls)))))) |
| 287 |
(define (get-fmls x ls1 ls2 m1 m2) |
| 288 |
(define (cons? x ls) (if x (cons x ls) ls)) |
| 289 |
(define (bound-id-member? x ls) |
| 290 |
(and (pair? ls) |
| 291 |
(or (bound-identifier=? x (car ls)) |
| 292 |
(bound-id-member? x (cdr ls))))) |
| 293 |
(let ([ls (cons x (append ls1 ls2 (cons? m1 (cons? m2 '()))))]) |
| 294 |
(let f ([x (car ls)] [ls (cdr ls)]) |
| 295 |
(unless (null? ls) |
| 296 |
(if (bound-id-member? x ls) |
| 297 |
(err "duplicate identifier" x) |
| 298 |
(f (car ls) (cdr ls))))) |
| 299 |
ls)) |
| 300 |
(let-values ([(prog opt-strs opt-ids args args-rest dash-rest) |
| 301 |
(command-line-interface stx)]) |
| 302 |
(list (get-fmls prog opt-ids args args-rest dash-rest) |
| 303 |
opt-strs opt-ids args args-rest dash-rest))) |
| 304 |
(define (parse-clause stx) |
| 305 |
(syntax-case stx () |
| 306 |
[(format help-str body body* ...) |
| 307 |
(string? (syntax->datum #'help-str)) |
| 308 |
(with-syntax ([((fmls ...) . args) |
| 309 |
(parse-format #'format)]) |
| 310 |
(list #'(lambda (fmls ...) body body* ...) |
| 311 |
#'(help-str . args)))] |
| 312 |
[(format body body* ...) |
| 313 |
(parse-clause #'(format "" body body* ...))])) |
| 314 |
(syntax-case stx () |
| 315 |
[(_ expr clause* ...) |
| 316 |
(with-syntax ([((proc* data*) ...) |
| 317 |
(map parse-clause #'(clause* ...))]) |
| 318 |
#'(dispatch-opts expr '(data* ...) (list proc* ...)))]))) |
| 319 |
) |