@@ -28,6 +28,7 @@ import Control.Monad.Trans.State
2828import Data.List (intercalate, isPrefixOf, nub, sortBy)
2929import Data.Maybe (mapMaybe)
3030import Data.Traversable (traverse)
31+import Data.Version (showVersion)
3132
3233import Parser
3334
@@ -36,6 +37,7 @@ import System.Directory (doesFileExist, findExecutable, getHomeDirectory)
3637import System.Exit
3738import System.Environment.XDG.BaseDir
3839import System.FilePath ((</>), isPathSeparator)
40+import qualified System.Console.CmdTheLine as Cmd
3941import System.Process
4042
4143import Text.Parsec (ParseError)
@@ -256,7 +258,6 @@ getCommand = do
256258-- Performs an action for each meta-command given, and also for expressions..
257259--
258260handleCommand :: Command -> StateT PSCI (InputT IO) ()
259−handleCommand Empty = return ()
260261handleCommand (Expression val) = get >>= inputTToState . handleDeclaration val
261262handleCommand (Let l) = modify (updateLets l)
262263handleCommand Help = inputTToState $ outputStrLn helpMessage
@@ -268,7 +269,7 @@ handleCommand (LoadFile filePath) = do
268269 either outputTStrLn (modify . updateModules) =<< ioToState (loadModule absPath)
269270 else
270271 outputTStrLn $ "Couldn't locate: " ++ filePath
271−handleCommand Reload = do
272+handleCommand Reset = do
272273 (Right prelude) <- ioToState $ loadModule =<< getPreludeFilename
273274 put (PSCI defaultImports prelude [])
274275handleCommand (TypeOf val) = get >>= inputTToState . handleTypeOf val
@@ -288,24 +289,44 @@ outputTStrLn = inputTToState . outputStrLn
288289outprintT :: Show a => a -> StateT PSCI (InputT IO) ()
289290outprintT = outputTStrLn . show
290291
292+inputFiles :: Cmd.Term [FilePath]
293+inputFiles = Cmd.value $ Cmd.posAny [] $ Cmd.posInfo { Cmd.posDoc = "The input .ps files" }
294+
291295-- |
292296-- The PSCI main loop.
293297--
294−main :: IO ()
295−main = do
298+loop :: [FilePath] -> IO ()
299+loop files = do
296300 preludeFilename <- getPreludeFilename
297− (Right prelude) <- loadModule preludeFilename
298− historyFilename <- getHistoryFilename
299− let settings = defaultSettings {historyFile = Just historyFilename}
300− runInputT (setComplete (completion prelude) settings) $ do
301− outputStrLn prologueMessage
302− evalStateT go (PSCI defaultImports prelude [])
303− where
304− go :: StateT PSCI (InputT IO) ()
305− go = do
306− c <- inputTToState getCommand
307− case c of
308− Left err -> outputTStrLn (show err) >> go
309− Right Nothing -> go
310− Right (Just Quit) -> outputTStrLn quitMessage
311− Right (Just c') -> handleCommand c' >> go
301+ modulesOrFirstError <- fmap concat . sequence <$> mapM loadModule (preludeFilename : files)
302+ case modulesOrFirstError of
303+ Left err -> putStrLn err >> exitFailure
304+ Right modules -> do
305+ historyFilename <- getHistoryFilename
306+ let settings = defaultSettings {historyFile = Just historyFilename}
307+ runInputT (setComplete (completion modules) settings) $ do
308+ outputStrLn prologueMessage
309+ evalStateT go (PSCI defaultImports modules [])
310+ where
311+ go :: StateT PSCI (InputT IO) ()
312+ go = do
313+ c <- inputTToState getCommand
314+ case c of
315+ Left err -> outputTStrLn (show err) >> go
316+ Right Nothing -> go
317+ Right (Just Quit) -> outputTStrLn quitMessage
318+ Right (Just c') -> handleCommand c' >> go
319+
320+term :: Cmd.Term (IO ())
321+term = loop <$> inputFiles
322+
323+termInfo :: Cmd.TermInfo
324+termInfo = Cmd.defTI
325+ { Cmd.termName = "psci"
326+ , Cmd.version = showVersion $ Paths.version
327+ , Cmd.termDoc = "Interactive mode for PureScript"
328+ }
329+
330+main :: IO ()
331+main = Cmd.run (term, termInfo)
332+