@@ -7,6 +7,7 @@ import CabalApi
77import Check
88import Control.Applicative
99import Control.Exception
10+ import Control.Monad
1011import Cradle
1112import Data.Typeable
1213import Data.Version
@@ -108,8 +109,8 @@ main = flip catches handlers $ do
108109 res <- case cmdArg0 of
109110 " browse" -> concat <$> mapM (browseModule opt) remainingArgs
110111 " list" -> listModules opt
111- " check" -> nArgs 1 $ withFile (checkSyntax opt cradle) cmdArg1
112- " expand" -> nArgs 1 $ withFile (checkSyntax opt { expandSplice = True } cradle) cmdArg1
112+ " check" -> withFiles (checkSyntax opt cradle) remainingArgs
113+ " expand" -> withFiles (checkSyntax opt { expandSplice = True } cradle) remainingArgs
113114 " debug" -> nArgs 1 $ withFile (debugInfo opt cradle strVer) cmdArg1
114115 " type" -> nArgs 4 $ withFile (typeExpr opt cradle cmdArg2 (read cmdArg3) (read cmdArg4)) cmdArg1
115116 " info" -> nArgs 3 $ withFile (infoExpr opt cradle cmdArg2 cmdArg3) cmdArg1
@@ -148,6 +149,11 @@ main = flip catches handlers $ do
148149 if exist
149150 then cmd file
150151 else throw (FileNotExist file)
152+ withFiles cmd files = do
153+ missing <- findM ((not <$> ) . doesFileExist) files
154+ case missing of
155+ Just file -> throw (FileNotExist file)
156+ Nothing -> cmd files
151157 xs !. idx
152158 | length xs <= idx = throw SafeList
153159 | otherwise = xs !! idx
@@ -159,6 +165,21 @@ main = flip catches handlers $ do
159165 where
160166 mPkgConf = cradlePackageConf cradle
161167
168+ -- | Returns the first Just produced, if any.
169+ findJust :: (Monad m ) => [m (Maybe a )] -> m (Maybe a )
170+ findJust [] = return Nothing
171+ findJust (mma: mmas) = do
172+ m <- mma
173+ case m of Nothing -> findJust mmas
174+ just -> return just
175+
176+ -- | Returns the first result that fulfills the check.
177+ findM :: (Monad m ) => (a -> m Bool ) -> [a ] -> m (Maybe a )
178+ findM f xs = findJust $ map justIfTrue xs
179+ where
180+ justIfTrue x = (\ b -> if b then Just x else Nothing ) `liftM` f x
181+
182+
162183----------------------------------------------------------------
163184
164185preBrowsedModules :: [String ]
0 commit comments