[racket] Typed racket and command line parameters

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Jun 22 11:25:01 EDT 2014

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. 

-- Matthias




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
>>> 
>>> 
>>> 
>>> 
>>> ____________________
>>> Racket Users list:
>>> http://lists.racket-lang.org/users
>> 
>> 
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/users
>> 
> 
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.