abstract + basic arg
[ekitaihs.git] / Parse.hs
diff --git a/Parse.hs b/Parse.hs
new file mode 100644 (file)
index 0000000..26a0390
--- /dev/null
+++ b/Parse.hs
@@ -0,0 +1,34 @@
+module Parse ( ekitaiOpts ) where
+
+import System.Console.GetOpt
+import System.Environment
+import Data.Maybe
+import Data.Either
+
+data Options = Options
+    { optHelp       :: Bool
+    , optColor      :: Bool
+    }
+
+defaultOptions = Options
+    { optHelp       = False
+    , optColor      = False
+    }
+
+options :: [OptDescr (Options -> Options)]
+options =
+    [ Option ['h'] ["help"]
+        (NoArg (\ opts -> opts { optHelp = True }))
+        "display's this message"
+    , Option ['c'] ["color", "colour"]
+        (NoArg (\ opts -> opts { optColor = True }))
+        "enables color"
+    ]
+
+ekitaiOpts :: [String] -> IO (Options, String)
+ekitaiOpts argv =
+    case getOpt RequireOrder options argv of
+        (o, [n], []) -> return (foldl (flip id) defaultOptions o, n)
+        (o, _, [])   -> ioError $ userError $ "must supply input file"
+        (_, _, errs)  -> ioError $ userError $ (concat errs)
+