From 93509d0ac88ec39698c0f516be5eff55c71d1628 Mon Sep 17 00:00:00 2001 From: thepenguin Date: Wed, 26 Oct 2022 13:59:46 +0200 Subject: [PATCH] Some funky command line parsing of stuffs --- app/Main.hs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index ffc2016..708b333 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,29 +1,39 @@ module Main where import Control.Monad +import Data.Functor 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 = useArgs =<< ( parse argParse . unlines) <$> getArgs +main = putStrLn =<< helper =<< getArgs + +helper :: [String] -> IO String +helper [] = return usage +helper (x:[]) = if (head x /= '-') + then readFile x + else return $ useArgs $ parse argParse x +helper (x:_:[]) = return $ useArgs $ parse argParse x +helper _ = fail "Too many arugemnts" + argParse :: ReadP String argParse = string "-" >> (many1 (satisfy (/= ' '))) -useArgs :: String -> IO a -useArgs [] = exit -useArgs ('h':_) = usage >> exit -useArgs ('v':_) = version >> exit +useArgs :: String -> String +useArgs [] = "" +useArgs ('h':_) = usage +useArgs ('v':_) = version useArgs (_:xs) = useArgs xs -usage :: IO () -usage = putStrLn "Usage: otm [-hv] [file]" -version :: IO () -version = putStrLn "otm 0.1" -exit :: IO a -exit = exitWith ExitSuccess +usage :: String +usage = "Usage: otm [-hv] [file]" +version :: String +version = "otm 0.1"