On Wednesday, October 12, 2016 at 10:19:28 AM UTC-4, Ben Greenman wrote:
> Alright, here's something that I think does what you want.
> 
> 
> 
> #lang racket
> 
> 
> (require racket/base
>  racket/cmdline)
> 
> 
> (parse-command-line "com_line"
>  ;; argv
>  (current-command-line-arguments)
>  ;; table
>  `((once-each
>          [("-i" "--input-file")
>          ,(lambda (flag) (displayln "Using <in>"))
>          ("Use input file as the input file.")]))
>  ;; finish-proc
>  (lambda (flag-accum file) file)
>  ;; arg-help-strs
>  '("Input file.")
>  ;; help-proc
>  (lambda (flag) (raise-user-error "com_line -i | --input-file input file")))
> 
> 
> ;; $ ./com_line --input-file "hacking.rkt"
> ;; Using <in>
> ;; "hacking.rkt"
> 
> 
> ;; $ ./com_line --help
> ;; com_line -i | --input-file input file
> 
> 
> 
> The code in your last message expects 1 flag, 1 argument to the flag  (`in`), 
> and 1 `file` argument.

Okay - I've figured this out. Output from testing below. I'll add in exit codes 
for the help-proc and unknown-proc so the finish-proc is never executed in 
cases where the user asks for help or uses an unknown flag. I'd rather return 
exit codes instead of raising an error as this code will likely be interacting 
with other code via a shell. Thanks for the exception handling suggestion.

$ ./com_line --input-file hacking.rkt
Once-each: Using hacking.rkt
finish-proc: Exit 0 if we make it this far.

$ ./com_line --help
com_line -i <input file>
help-proc: Exit 1 here. Skip finish-proc.
finish-proc: Exit 0 if we make it this far.

$ ./com_line --unknown
Unknown flag: --unknown
com_line -i <input file>
unknown-proc: Exit 1 here. Skip finish-proc.
finish-proc: Exit 0 if we make it this far.

Code that generates the above output below. (Let me know if there's a way to 
make code look better in these posts)

#lang racket

(require racket/base
         racket/cmdline)

(define usage "com_line -i <input file>")

(parse-command-line "com_line"
                    ;;argv                                                      
                                                                                
                              
                    (current-command-line-arguments)
                    ;;table                                                     
                                                                                
                              
                    `((once-each
                       [("-i" "--input-file")
                        ,(lambda (flag in)
                           (displayln (string-append "Once-each: Using " in)))
                        ("Use input file as the input file." "Input file.")]))
                    ;;finish-proc                                               
                                                                                
                              
                    (lambda (flag-accum)
                      (displayln "finish-proc: Exit 0 if we make it this far."))
                    ;;arg-help-strs                                             
                                                                                
                              
                    '()
                    ;;help-proc                                                 
                                                                                
                              
                    (lambda (flag)
                      (displayln usage)
                      (displayln "help-proc: Exit 1 here. Skip finish-proc."))
                    ;;unknown-proc                                              
                                                                                
                              
                    (lambda (unknown_flag)
                      (displayln (string-append "Unknown flag: " unknown_flag))
                      (displayln usage)
                      (displayln "unknown-proc: Exit 1 here. Skip 
finish-proc.")))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to