Skip to content

Instantly share code, notes, and snippets.

@aisamanra
Created November 20, 2014 00:56
Show Gist options
  • Save aisamanra/81f0c4bdfec7b8189eea to your computer and use it in GitHub Desktop.
Save aisamanra/81f0c4bdfec7b8189eea to your computer and use it in GitHub Desktop.
sample "convenient main wrapper" code
module Main where
import NiceMain
main = nicemain go where go x y z = print $ (x && y) || z
{-# 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