Reading command line arguments

Haskell:
Reading command line arguments

How to:

import System.Environment (getArgs)

main :: IO ()
main = do
  args <- getArgs
  putStrLn ("Hello, " ++ show args ++ "!")

Run it passing “world” as an argument:

$ runhaskell yourprogram.hs world
Hello, ["world"]!

Deep Dive

Haskell’s a neat language, with roots in the 80s, favoring purity and static typing. It’s had a way to handle command line arguments since the early days. In other languages, this might be pretty procedural stuff, but here, we’re in the realm of IO monads to deal with the wild outside world.

Alternatives? You can go nuts with libraries like optparse-applicative for complex stuff, but for simple cases, getArgs does the trick.

Under the hood? getArgs is a function that dives into your system, fetches whatever followed the program name in the terminal, and hands you a list of strings. It’s implemented in Haskell’s base library, leaning on lower-level C functions to do the grunt work. Neat, right?

See Also