Haskell检查程序可用性

时间:2012-07-13 09:42:14

标签: windows shell haskell path portability

是否可以检查给定字符串是否代表$ PATH(%path%)中的可执行文件? 它必须可移植到Windows。想要调用它并查看返回状态的想法不适用, 非零可能意味着程序错误或程序未找到。 好吧,在代码中,我想要关注

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

1 个答案:

答案 0 :(得分:7)

使用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

我将类型更改为IO (Maybe String),因为可能没有找到任何候选人。