a one dimensional cellular automata, using comonads
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6KB

  1. module Options where
  2. import System.Environment
  3. import System.Console.GetOpt
  4. import System.Process
  5. ------------------------
  6. -- command line flags --
  7. ------------------------
  8. -- structure containing the programs options
  9. data Options = Options
  10. { optWidth :: Int
  11. , optGenerations :: Int
  12. , optHeight :: Int
  13. } deriving Show
  14. -- the default options for the program
  15. -- the width and generations are injected
  16. -- and intended to be gotten at runtime
  17. -- to match the window dimensions
  18. defaultOptions :: Int -> Int -> Options
  19. defaultOptions w h = Options
  20. { optWidth = w
  21. , optGenerations = 40
  22. , optHeight = h
  23. }
  24. -- the avaliable options
  25. options :: [OptDescr (Options -> Options)]
  26. options =
  27. [ Option ['w'] ["width"]
  28. (ReqArg (\w opts -> opts { optWidth = (read w) }) "WIDTH")
  29. "term width"
  30. , Option ['g'] ["generations"]
  31. (ReqArg (\t opts -> opts { optGenerations = (read t) }) "GENERATIONS")
  32. "time steps to simulate"
  33. , Option ['h'] ["height"]
  34. (ReqArg (\t opts -> opts { optHeight = (read t) }) "HEIGHT")
  35. "term height"
  36. ]
  37. -- parse the options into the structure
  38. -- erroring if encountering a flag not known to us
  39. parseArgs :: IO Options
  40. parseArgs = do
  41. argv <- getArgs
  42. progName <- getProgName
  43. tw <- readProcess "tput" [ "cols" ] ""
  44. th <- readProcess "tput" [ "lines" ] ""
  45. case getOpt RequireOrder options argv of
  46. (opts, [], []) -> return (foldl (flip id) (defaultOptions (read tw) (read th)) opts)
  47. (_, _, errs) -> ioError (userError (concat errs ++ helpMessage))
  48. where
  49. header = "Usage: " ++ progName ++ " [OPTION...]"
  50. helpMessage = usageInfo header options