Main.hs 1.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Main where

import Mock
import Data.List
import Data.Char
import Data.Maybe
import System.Environment


main :: IO ()
main = do
    args <- getArgs
    case length args of
        0 -> putStrLn help
15
        1 -> getLine >>= (\text -> handle [head args, text])
16
17
18
        _ -> handle args

handle :: [String] -> IO ()
Nicolas Lenz's avatar
Nicolas Lenz committed
19
handle (style:text) = transform (intercalate " " text) >>= putStrLn where
20
    transform :: String -> IO String
Nicolas Lenz's avatar
Nicolas Lenz committed
21
    transform = case lookup style styles of
22
23
24
        Just f -> f
        Nothing -> const $ return help

Nicolas Lenz's avatar
Nicolas Lenz committed
25
26
styles :: [(String, String -> IO String)]
styles = [
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    ("random", mockRandom),
    ("alternate", toIO mockAlternate),
    ("space", toIO $ mockSpace 1),
    ("space2", toIO $ mockSpace 2),
    ("space3", toIO $ mockSpace 3),
    ("upper", toIO $ map toUpper),
    ("lower", toIO $ map toLower)
    ]

toIO :: (a -> b) -> (a -> IO b)
toIO f = (\x -> return $ f x)

help :: String
help = "Mock - a program to transform text.\n\n\
       \Usage: mock [STYLE] [TEXT]\n\
Nicolas Lenz's avatar
Nicolas Lenz committed
42
       \Styles: " ++ (intercalate ", " $ map fst styles)