Read filename from commandline argument

This commit is contained in:
Dimitri Lozeve 2018-01-17 22:27:14 +00:00
parent 3c5ef50b13
commit 3f8d761d8e

View file

@ -13,11 +13,17 @@ import qualified Data.ByteString.Lazy.Char8 as B
import Lib import Lib
data CmdLnOptions = CmdLnOptions data CmdLnOptions = CmdLnOptions
{ optionIterations :: Integer { optionFilename :: String
, optionIterations :: Integer
, optionColor :: Color , optionColor :: Color
, optionWhiteBg :: Bool , optionWhiteBg :: Bool
} }
filenameParser :: Parser String
filenameParser = strArgument
(metavar "FILENAME"
<> help "JSON file specifying an L-system")
iterationsParser :: Parser Integer iterationsParser :: Parser Integer
iterationsParser = option auto iterationsParser = option auto
(long "iterations" (long "iterations"
@ -58,7 +64,7 @@ whiteBackgroundParser = switch
optionsParser :: Parser CmdLnOptions optionsParser :: Parser CmdLnOptions
optionsParser = CmdLnOptions <$> optionsParser = CmdLnOptions <$>
iterationsParser <*> colorParser <*> whiteBackgroundParser filenameParser <*> iterationsParser <*> colorParser <*> whiteBackgroundParser
opts :: ParserInfo CmdLnOptions opts :: ParserInfo CmdLnOptions
opts = info (optionsParser <**> helper) opts = info (optionsParser <**> helper)
@ -73,7 +79,7 @@ createDisplay fgColor wbg n ls = display (InWindow (name ls) (200, 200) (10, 10)
main :: IO () main :: IO ()
main = do main = do
lsStr <- B.getContents CmdLnOptions f n fgColor wbg <- execParser opts
lsStr <- B.readFile f
let Just ls = decode lsStr :: Maybe (LSystem Char) let Just ls = decode lsStr :: Maybe (LSystem Char)
CmdLnOptions n fgColor wbg <- execParser opts
createDisplay fgColor wbg n ls createDisplay fgColor wbg n ls