Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read stdin in a Haskell program with command-line arguments?

I know I can use interact :: (String -> String) -> IO () to conveniently read from stdin and write to stdout in a simple Haskell program (see http://learnyouahaskell.com/input-and-output).

Now I would like to add command-line arguments to make my simple program "configurable".

Is there a way to do this and still use interact (so as to obtain a configurable program with minimum effort)?

like image 457
bli Avatar asked May 18 '26 05:05

bli


1 Answers

This can be done by "sequencing" IO computations with do and use getArgs before using interact:

import System.Environment (getArgs)
main :: IO ()
main = do
    args <- getArgs
    interact (<whatever using args>)
like image 171
bli Avatar answered May 21 '26 09:05

bli