KAction
KAction

Reputation: 2017

Haskell check for program avaliablity

Is it way to check whether given string represent executable in $PATH(%path%)? It have to be portable to Windows. Idea to just call it and see return status is not apporiate, as non-zero may mean program error or program not found. Well, in code, I want following

possibleCompilers = ["gcc", "icc", "foo", "bar.exe"]
presentCompiler :: IO String

Upvotes: 0

Views: 98

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183948

That task should be doable using System.Directory.findExecutable.

possibleCompiler :: IO (Maybe String)
possibleCompiler = check possibleCompilers
  where
    check [] = return Nothing
    check (c:cs) = do
       mbc <- findExecutable c
       case mbc of
         Just _ -> return mbc
         Nothing -> check cs

I changed the type to IO (Maybe String), since maybe, none of the candidates is found.

Upvotes: 7

Related Questions