gdritter repos telml / daac7bb
Add basic option-handling for telml2html Getty Ritter 6 years ago
1 changed file(s) with 44 addition(s) and 4 deletion(s). Collapse all Expand all
44 import qualified Data.TeLML as TeLML
55 import qualified Data.TeLML.Markup as TeLML
66 import qualified Text.Blaze.Renderer.String as B
7 import qualified System.Console.GetOpt as Opt
78 import qualified System.Exit as Sys
9 import qualified System.Environment as Env
10
11 data Options = Options
12 { optInput :: Maybe FilePath
13 , optOutput :: Maybe FilePath
14 }
15
16 defaultOptions :: Options
17 defaultOptions = Options
18 { optInput = Nothing
19 , optOutput = Nothing
20 }
21
22 options :: [Opt.OptDescr (Options -> Options)]
23 options =
24 [ Opt.Option ['i'] ["input"]
25 (Opt.ReqArg (\ path opt -> opt { optInput = Just path }) "file")
26 "Read input from this file"
27 , Opt.Option ['o'] ["output"]
28 (Opt.ReqArg (\ path opt -> opt { optOutput = Just path }) "file")
29 "Read input from this file"
30 ]
831
932 doRender :: String -> Either String String
1033 doRender = TeLML.parse >=> TeLML.render >=> return . B.renderMarkup
1134
35 runPipeline :: Maybe FilePath -> Maybe FilePath -> IO ()
36 runPipeline inF outF = do
37 cs <- case inF of
38 Nothing -> getContents
39 Just f -> readFile f
40 case doRender cs of
41 Left err -> Sys.die err
42 Right rs -> case outF of
43 Nothing -> putStrLn rs
44 Just f -> writeFile f rs
45
1246 main :: IO ()
1347 main = do
14 cs <- getContents
15 case doRender cs of
16 Left err -> Sys.die err
17 Right x -> putStrLn x
48 opts <- Opt.getOpt Opt.Permute options `fmap` Env.getArgs
49 case opts of
50 (fs, [], []) -> do
51 let Options
52 { optInput = inF
53 , optOutput = outF
54 } = foldr id defaultOptions fs
55 runPipeline inF outF
56 (_, _, _) -> do
57 Sys.die (Opt.usageInfo "telml2html" options)