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.

62 lines
1.8KB

  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. , optTime :: Int
  14. } deriving Show
  15. -- the default options for the program
  16. -- the width and generations are injected
  17. -- and intended to be gotten at runtime
  18. -- to match the window dimensions
  19. defaultOptions :: Int -> Int -> Options
  20. defaultOptions w h = Options
  21. { optWidth = w
  22. , optGenerations = 40
  23. , optHeight = h
  24. , optTime = 7
  25. }
  26. -- the avaliable options
  27. options :: [OptDescr (Options -> Options)]
  28. options =
  29. [ Option ['w'] ["width"]
  30. (ReqArg (\w opts -> opts { optWidth = (read w) }) "WIDTH")
  31. "term width"
  32. , Option ['g'] ["generations"]
  33. (ReqArg (\t opts -> opts { optGenerations = (read t) }) "GENERATIONS")
  34. "time steps to simulate"
  35. , Option ['h'] ["height"]
  36. (ReqArg (\t opts -> opts { optHeight = (read t) }) "HEIGHT")
  37. "term height"
  38. , Option ['t'] ["time"]
  39. (ReqArg (\t opts -> opts { optTime = (read t) }) "TIME")
  40. "delay time"
  41. ]
  42. -- parse the options into the structure
  43. -- erroring if encountering a flag not known to us
  44. parseArgs :: IO Options
  45. parseArgs = do
  46. argv <- getArgs
  47. progName <- getProgName
  48. tw <- readProcess "tput" [ "cols" ] ""
  49. th <- readProcess "tput" [ "lines" ] ""
  50. case getOpt RequireOrder options argv of
  51. (opts, [], []) -> return (foldl (flip id) (defaultOptions (read tw) (read th)) opts)
  52. (_, _, errs) -> ioError (userError (concat errs ++ helpMessage))
  53. where
  54. header = "Usage: " ++ progName ++ " [OPTION...]"
  55. helpMessage = usageInfo header options