Allow optional arguments with default in `cmdline', updated docs.

This commit is contained in:
Eli Barzilay 2010-11-12 11:30:32 -05:00
parent 4f36ce9635
commit c1e1c70dcf
2 changed files with 12 additions and 5 deletions

View File

@ -58,7 +58,7 @@
[(argv-expr lst) [(argv-expr lst)
(extract-arg '#:argv lst #'(current-command-line-arguments))]) (extract-arg '#:argv lst #'(current-command-line-arguments))])
(let-values ([(table args) (let-values ([(table args)
(let loop ([lst lst][accum null]) (let loop ([lst lst] [accum null])
(if (null? lst) (if (null? lst)
(loop (syntax->list #'(#:args () (void))) accum) (loop (syntax->list #'(#:args () (void))) accum)
(let ([a (syntax-e (car lst))] (let ([a (syntax-e (car lst))]
@ -140,6 +140,9 @@
[(arg . rest) [(arg . rest)
(identifier? #'arg) (identifier? #'arg)
(cons #'arg (loop #'rest))] (cons #'arg (loop #'rest))]
[([arg def] . rest)
(identifier? #'arg)
(cons #'[arg def] (loop #'rest))]
[arg [arg
(identifier? #'arg) (identifier? #'arg)
(list #'arg)] (list #'arg)]
@ -151,7 +154,9 @@
(serror "#:args must not be followed by another keyword" (car lst))) (serror "#:args must not be followed by another keyword" (car lst)))
(with-syntax ([formals (car pieces)] (with-syntax ([formals (car pieces)]
[formal-names (map (lambda (x) [formal-names (map (lambda (x)
(symbol->string (syntax-e x))) (let ([d (syntax->datum x)])
(symbol->string
(if (pair? d) (car d) d))))
formal-names)] formal-names)]
[body (cdr pieces)]) [body (cdr pieces)])
(values (reverse accum) (values (reverse accum)

View File

@ -29,9 +29,11 @@
[finish-clause code:blank [finish-clause code:blank
(code:line #:args arg-formals body ...+) (code:line #:args arg-formals body ...+)
(code:line #:handlers handlers-exprs)] (code:line #:handlers handlers-exprs)]
[arg-formals id [arg-formals rest-id
(id ...) (arg ...)
(id ...+ . id)] (arg ...+ . rest-id)]
[arg id
[id default-expr]]
[handlers-exprs (code:line finish-expr arg-strings-expr) [handlers-exprs (code:line finish-expr arg-strings-expr)
(code:line finish-expr arg-strings-expr help-expr) (code:line finish-expr arg-strings-expr help-expr)
(code:line finish-expr arg-strings-expr help-expr (code:line finish-expr arg-strings-expr help-expr