Sunday, 1 May 2016

Haskell: command line arguments to standalone (main) program


In previous post, I explained how to develop a standalone program. In this post I am going to explain how to pass command line arguments to a standalone program.

System.Environment module provide ‘getArgs :: IO [String]’ function, it returns a list of the program's command line arguments (not including the program name).

Test.hs
module Main where
    import System.Environment

    main = do x <- getArgs; print x

$ ghc --make -o cmdLine Test.hs
[1 of 1] Compiling Main             ( Test.hs, Test.o )
Linking cmdLine ...
$ ./cmdLine 1 2 3
["1","2","3"]
$ ./cmdLine Hello Hari How are you
["Hello","Hari","How","are","you"]
 
 
Previous                                                 Next                                                 Home

No comments:

Post a Comment