[racket] Typed racket and command line parameters

From: Manfred Lotz (manfred.lotz at arcor.de)
Date: Sun Jun 22 12:55:20 EDT 2014

On Sun, 22 Jun 2014 11:25:01 -0400
Matthias Felleisen <matthias at ccs.neu.edu>
wrote:

> 
> I would use Real for the parameter type and then cast twice: 
> 
> (define parms
>   (command-line
>    #:program "mypgm"
>    #:once-each
>    [("-v" "--verbose") "Show verbose output" (opt-verbose-mode #t)]
>    [("-m" "--max-size") 
>     NUM
>     ("Max size." "NUM") 
>     (opt-max-size (cast (string->number (cast NUM String)) Real))]))
> 
> The casts bring out that you are willing to sacrifice precision of
> error messages for concision in code. 
> 

Thanks for this. I like this more than the guard solution although when
typing a non number for instance the error messages are a bit wild for
an ordinary user. Perhaps then I have to write my own string->number in
order to give a good error message if the parameter is a non number.

-- 
Manfred


> 
> 
> 
> 
> On Jun 22, 2014, at 12:06 AM, Manfred Lotz wrote:
> 
> > On Sat, 21 Jun 2014 10:12:27 -0400
> > "Alexander D. Knauth"
> > <alexander at knauth.org> wrote:
> > 
> >> The first thing I notice is that opt-max-size is a (Parameterof
> >> Any), but you use string->number on it. So you should probably put
> >> a guard on the opt-max-size parameter something like this: (:
> >> opt-max-size (Parameterof Any Real)) (: opt-max-size-guard (Any ->
> >> Real)) (define (opt-max-size-guard val)
> >>  (cond [(real? val) val]
> >>        [(string? val) (opt-max-size-guard (string->number val))]
> >>        [else (error "Max size: must be a real number")]))
> >> 
> >> (define opt-max-size (make-parameter 0 opt-max-size-guard))
> >> 
> > 
> > Thanks for this, which works fine. I'm not qite sure I like this as
> > it seems to make the code more complicated. But if this is the only
> > possibility what can be done.
> > 
> > 
> > 
> >> On Jun 21, 2014, at 3:45 AM, Manfred Lotz
> >> <manfred.lotz at arcor.de> wrote:
> >> 
> >>> Hi there,
> >>> I try to change one of my programs to typed racket and fail
> >>> because of errors when dealing with command line arguments.
> >>> 
> >>> Here a minimum example:
> >>> 
> >>> #lang typed/racket/base
> >>> 
> >>> (require racket/cmdline)
> >>> 
> >>> 
> >>> (: opt-verbose-mode (Parameterof Boolean))
> >>> (define opt-verbose-mode (make-parameter #f))
> >>> (: opt-max-size (Parameterof Any))
> >>> (define opt-max-size (make-parameter "0"))
> >>> 
> >>> 
> >>> 
> >>> (define parms
> >>> (command-line
> >>>  #:program "mypgm"
> >>>  #:once-each
> >>>  [("-v" "--verbose") "Show verbose output" (opt-verbose-mode #t)]
> >>>  [("-m" "--max-size") NUM ("Max size." "NUM") (opt-max-size NUM) ]
> >>>  ))
> >>> 
> >>> 
> >>> (define (myfun)
> >>> (when (opt-verbose-mode)
> >>>   (if (> (string->number (opt-max-size)) 0)
> >>>       #t
> >>>       #f)))
> >>> 
> >>> 
> >>> What do I do wrong? Any hint appreciated.
> >>> 
> >>> -- 
> >>> Thanks,
> >>> Manfred
> >>> 
> >>> 
> >>> 



Posted on the users mailing list.