Created
November 20, 2014 00:56
-
-
Save aisamanra/81f0c4bdfec7b8189eea to your computer and use it in GitHub Desktop.
sample "convenient main wrapper" code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import NiceMain | |
main = nicemain go where go x y z = print $ (x && y) || z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE ViewPatterns #-} | |
module NiceMain (nicemain, IsMain) where | |
import System.Environment (getArgs) | |
nicemain :: (IsMain m) => m -> IO () | |
nicemain main = do | |
env <- getArgs | |
mainApp env main | |
readMb :: Read a => String -> Maybe a | |
readMb (reads -> [(s, "")]) = Just s | |
readMb _ = Nothing | |
class IsMain x where | |
mainApp :: [String] -> x -> IO () | |
instance (Read a, IsMain b) => IsMain (a -> b) where | |
mainApp (x:xs) f = case readMb x of | |
Just r -> mainApp xs (f r) | |
Nothing -> putStrLn "Argument of wrong type!" | |
mainApp [] _ = putStrLn "Not enough arguments to main!" | |
instance IsMain (IO ()) where | |
mainApp [] f = f | |
mainApp _ _ = putStrLn "Too many arguments to main!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment