X-Git-Url: https://git.danieliu.xyz/?p=ekitaihs.git;a=blobdiff_plain;f=Parse.hs;h=2375c4fb4db8674bc05c4cb66f625081872e40e8;hp=26a0390fc08461919616343928bc52be9b2e1714;hb=HEAD;hpb=5b8e53984e97f64f62f5c5f607322006dbddfabc diff --git a/Parse.hs b/Parse.hs index 26a0390..2375c4f 100644 --- a/Parse.hs +++ b/Parse.hs @@ -1,34 +1,41 @@ -module Parse ( ekitaiOpts ) where +module Parse ( ekitaiOpts, optTimeStep, hGetLines ) where import System.Console.GetOpt import System.Environment +import System.Exit +import System.IO import Data.Maybe import Data.Either data Options = Options - { optHelp :: Bool - , optColor :: Bool + { optTimeStep :: Int } defaultOptions = Options - { optHelp = False - , optColor = False + { optTimeStep = 50000 } options :: [OptDescr (Options -> Options)] options = - [ Option ['h'] ["help"] - (NoArg (\ opts -> opts { optHelp = True })) - "display's this message" - , Option ['c'] ["color", "colour"] - (NoArg (\ opts -> opts { optColor = True })) - "enables color" + [ Option ['t'] ["timestep"] + (ReqArg (\t opts -> opts { optTimeStep = read t :: Int }) "timestep") + "sets the simulation time step" ] ekitaiOpts :: [String] -> IO (Options, String) ekitaiOpts argv = case getOpt RequireOrder options argv of (o, [n], []) -> return (foldl (flip id) defaultOptions o, n) - (o, _, []) -> ioError $ userError $ "must supply input file" - (_, _, errs) -> ioError $ userError $ (concat errs) + (o, _, []) -> ioError $ userError $ "missing input file" + (_, _, errs) -> ioError $ userError $ concat errs + +-- reads in file by lines +hGetLines :: Handle -> IO [String] +hGetLines h = do + line <- hGetLine h + isEof <- hIsEOF h + if isEof then return [line] + else do + lines <- hGetLines h + return (line:lines)