-main = do
- args <- getArgs
- case args of
- ('-':'f' : ghc) : args -> do
- doIt (dropWhile isSpace ghc) args
- args -> do
- mb_ghc <- findExecutable "ghc"
- case mb_ghc of
- Nothing -> dieProg ("cannot find ghc")
- Just ghc -> doIt ghc args
+main :: IO ()
+main = do
+ args <- getArgs
+ case getGhcLoc args of
+ (Just ghc, args') -> doIt ghc args'
+ (Nothing, args') -> do
+ mbPath <- getExecPath
+ case mbPath of
+ Nothing -> dieProg ("cannot find ghc")
+ Just path ->
+ let ghc = takeDirectory (normalise path) </> "ghc"
+ in doIt ghc args'
+
+getGhcLoc :: [String] -> (Maybe FilePath, [String])
+getGhcLoc args = case args of
+ "-f" : ghc : args' -> f ghc args'
+ ('-' : 'f' : ghc) : args' -> f ghc args'
+ -- If you need the first GHC flag to be a -f flag then
+ -- you can pass -- first
+ "--" : args -> (Nothing, args)
+ args -> (Nothing, args)
+ where f ghc args' = -- If there is another -f flag later on then
+ -- that overrules the one that we've already
+ -- found
+ case getGhcLoc args' of
+ (Nothing, _) -> (Just ghc, args')
+ success -> success