mirror of
https://github.com/The1Penguin/org-to-mafiauniverse.git
synced 2024-11-22 02:08:13 +00:00
Some funky command line parsing of stuffs
This commit is contained in:
parent
a5c53412a6
commit
93509d0ac8
32
app/Main.hs
32
app/Main.hs
@ -1,29 +1,39 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
|
import Data.Functor
|
||||||
import System.Environment
|
import System.Environment
|
||||||
import System.Exit
|
import System.Exit
|
||||||
import Text.ParserCombinators.ReadP
|
import Text.ParserCombinators.ReadP
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
parse :: ReadP String -> String -> String
|
parse :: ReadP String -> String -> String
|
||||||
parse rules = fst . last .readP_to_S rules
|
parse rules = fst . last .readP_to_S rules
|
||||||
|
|
||||||
main :: IO ()
|
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 :: ReadP String
|
||||||
argParse =
|
argParse =
|
||||||
string "-" >> (many1 (satisfy (/= ' ')))
|
string "-" >> (many1 (satisfy (/= ' ')))
|
||||||
|
|
||||||
useArgs :: String -> IO a
|
useArgs :: String -> String
|
||||||
useArgs [] = exit
|
useArgs [] = ""
|
||||||
useArgs ('h':_) = usage >> exit
|
useArgs ('h':_) = usage
|
||||||
useArgs ('v':_) = version >> exit
|
useArgs ('v':_) = version
|
||||||
useArgs (_:xs) = useArgs xs
|
useArgs (_:xs) = useArgs xs
|
||||||
|
|
||||||
usage :: IO ()
|
usage :: String
|
||||||
usage = putStrLn "Usage: otm [-hv] [file]"
|
usage = "Usage: otm [-hv] [file]"
|
||||||
version :: IO ()
|
version :: String
|
||||||
version = putStrLn "otm 0.1"
|
version = "otm 0.1"
|
||||||
exit :: IO a
|
|
||||||
exit = exitWith ExitSuccess
|
|
||||||
|
Loading…
Reference in New Issue
Block a user