From c50d9e35dc25162427f0480db2ad8bdbb4a6f0b1 Mon Sep 17 00:00:00 2001 From: thepenguin Date: Wed, 26 Oct 2022 12:37:48 +0200 Subject: [PATCH] created an args system --- app/Main.hs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/app/Main.hs b/app/Main.hs index 75ad35c..1d1bd25 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,6 +1,29 @@ module Main where +import Control.Monad import System.Environment +import System.Exit +import Text.ParserCombinators.ReadP + +parse :: ReadP String -> String -> String +parse rules = fst . last .readP_to_S rules main :: IO () -main = putStrLn =<< readFile =<< head <$> getArgs +main = useArgs =<< ( parse argParse . unlines) <$> getArgs + +argParse :: ReadP String +argParse = + string "-" >> (many1 (satisfy (/= ' '))) + +useArgs :: String -> IO a +useArgs [] = exit +useArgs ('h':_) = usage >> exit +useArgs ('v':_) = version >> exit +useArgs (_:xs) = useArgs xs + +usage :: IO () +usage = putStrLn "Usage: \"PENDINGNAME\" [-hv] [file]" +version :: IO () +version = putStrLn "\"PENDINGNAME\" 0.1" +exit :: IO a +exit = exitWith ExitSuccess