It will be easier if you rework your own mainto 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
EOF stdin GHCi. , stdin :
Prelude> getLine
*** Exception: <stdin>: hGetLine: illegal operation (handle is closed)
Prelude> getContents
*** Exception: <stdin>: hGetContents: illegal operation (handle is closed)