I thought of giving this answer too, but if this is about testing let me 
propose a slightly different approach: 

#lang racket

;; assume main has two arguments
(define (main)
  (define args (current-command-line-arguments))
  (if (= (vector-length args) 0)
      (do-it)
      (with-input-from-file (vector-length args) do-it)))

(define (do-it)
  (read-line))

(module+ test
  (require rackunit)

  (define my-file (open-input-string "hello world\n line 2"))

  (check-equal? (parameterize ([current-input-port my-file]) (do-it)) "hello 
world"))
  

Also you can exercise main in DrRacket as if it were invoked via Language > 
Show Details > command line arguments . . . 



> On Feb 8, 2017, at 2:02 PM, Ben Greenman <benjaminlgreen...@gmail.com> wrote:
> 
> One idea: you can put the argument-parsing code in the "main" submodule, then 
> tell DrRacket not to run the main submodule.
> 
> 
> #lang racket
> 
> (define (run in-port)
>   "testing...")
> 
> (module+ main
>   (require racket/cmdline)
>   (command-line
>     #:args args
>     (run (if (null? args) (current-input-port) (open-input-file (car 
> args))))))
> 
> Then in DrRacket, click "Language -> Choose Language -> Show Details -> 
> Submodules to Run" and un-check "main".
> 
> 
> On Wed, Feb 8, 2017 at 1:51 PM, Steve Byan's Lists 
> <steve-l...@byan-roper.org> wrote:
> I'm working on a script that I eventually plan to invoke from the command 
> line. I'd like the script to either take a file name argument or, if no 
> arguments, read from stdin. However, while developing the script in DrRacket, 
> I'd like to not invoke the top-level function, and to instead define an input 
> port on a test file for convenience when exercising the code from the 
> DrRacket REPL.
> 
> Is there a way for a module to distinguish between running in DrRacket and 
> running from the racket or gracket command?
> 
> Best regards,
> -Steve
> 
> --
> Steve Byan
> steveb...@me.com
> Littleton, MA
> 
> 
> 
> --
> 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.
> 
> 
> -- 
> 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.

-- 
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