How do you read a file specified as a parameter to a function when running GHCI

This will be easier if you rework your main to open the file itself.

import System.Environment
import System.IO

main :: IO ()
main = do
    args <- getArgs
    case args of
      [] -> doStuff stdin
      file:_ ->
        withFile file ReadMode doStuff

doStuff :: Handle -> IO ()
doStuff = …
*Main> System.Environment.withArgs ["main.txt"] main

Don't give a EOF on stdin while within GHCi. If you do, all further attempts to read from stdin will fail:

Prelude> getLine
*** Exception: <stdin>: hGetLine: illegal operation (handle is closed)
Prelude> getContents
*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)

You CAN type :main in GHCi to invoke command line parameters. I'm afraid you'll probably just want to use that.

Tags:

Haskell

Ghci