ghc -e

Nils Anders Danielsson nad at
Wed Nov 17 09:06:54 EST 2004

On Wed, 17 Nov 2004, Benjamin Franksen wrote:

> I want to use ghc as a script interpreter, using the '-e' option. The
> problem is that I can't give the script any command line arguments,
> because 'ghc -e' intepretes everything argument as the name of a module
> to compile.

I once wrote a script which circumvented the issue above by using
System.Environment.withArgs. It works, but it is of course a little

See the attached sources.

-------------- next part --------------
module Main where

import IO
import System.Environment
import System.Posix.Process

ghc = ... -- Path to GHC.
stripFirstLine = ... -- Path to the program below.

--   #!/bin/sh
--   grep -v '^#!' $2 > $3

main = do
  args <- getArgs
  if length args < 1 then
    hPutStr stderr usage
   else do
    let file:progArgs = args
        cmd = "System.Environment.withProgName " ++ show file ++
              " $ System.Environment.withArgs " ++ show progArgs ++
              " Main.main"
        ghcArgs = ["-F", "-pgmF", stripFirstLine, "-e", cmd, file]
    executeFile ghc False ghcArgs Nothing

usage =
  "Usage: runghc <Haskell source file> [<Extra arguments to the program>...]\n"

More information about the Glasgow-haskell-users mailing list