From c1e1c70dcf4d32c417bbd205a214a29c68d3be29 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Fri, 12 Nov 2010 11:30:32 -0500 Subject: [PATCH] Allow optional arguments with default in `cmdline', updated docs. --- collects/racket/cmdline.rkt | 9 +++++++-- collects/scribblings/reference/cmdline.scrbl | 8 +++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/collects/racket/cmdline.rkt b/collects/racket/cmdline.rkt index 2c0371802c..bb6383c734 100644 --- a/collects/racket/cmdline.rkt +++ b/collects/racket/cmdline.rkt @@ -58,7 +58,7 @@ [(argv-expr lst) (extract-arg '#:argv lst #'(current-command-line-arguments))]) (let-values ([(table args) - (let loop ([lst lst][accum null]) + (let loop ([lst lst] [accum null]) (if (null? lst) (loop (syntax->list #'(#:args () (void))) accum) (let ([a (syntax-e (car lst))] @@ -140,6 +140,9 @@ [(arg . rest) (identifier? #'arg) (cons #'arg (loop #'rest))] + [([arg def] . rest) + (identifier? #'arg) + (cons #'[arg def] (loop #'rest))] [arg (identifier? #'arg) (list #'arg)] @@ -151,7 +154,9 @@ (serror "#:args must not be followed by another keyword" (car lst))) (with-syntax ([formals (car pieces)] [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)] [body (cdr pieces)]) (values (reverse accum) diff --git a/collects/scribblings/reference/cmdline.scrbl b/collects/scribblings/reference/cmdline.scrbl index 147d90b5c6..eddb01647c 100644 --- a/collects/scribblings/reference/cmdline.scrbl +++ b/collects/scribblings/reference/cmdline.scrbl @@ -29,9 +29,11 @@ [finish-clause code:blank (code:line #:args arg-formals body ...+) (code:line #:handlers handlers-exprs)] - [arg-formals id - (id ...) - (id ...+ . id)] + [arg-formals rest-id + (arg ...) + (arg ...+ . rest-id)] + [arg id + [id default-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