I am writing a program that should be able to read input either from the command line directly or from a file depending on what args are passed. For example. However I am having trouble making the IO event conditional. My first thought was to do:
import System.Environment
main :: IO ()
main = do
cla <- getArgs
let flags = parseFlags (filter isFlag cla) []
let args = filter (not.isFlag) cla
input <- if (elem "e" flags) then (head args) else (readFile (head args))
However this doesn't work because (head args)
is not a IO action. My next thought was to do:
import System.Environment
main :: IO ()
main = do
cla <- getArgs
let flags = parseFlags (filter isFlag cla) []
let args = filter (not.isFlag) cla
file <- (readFile (head args))
let input = if (elem "e" flags) then (head args) else (file)
But this will error if the input is not the name of a file. Is there a sensible way to do this? Am I approaching the problem incorrectly?