org-to-mafiauniverse/app/Main.hs

59 lines
1.4 KiB
Haskell
Raw Normal View History

2022-10-26 09:57:55 +00:00
module Main where
2022-10-26 22:15:32 +00:00
import Control.Applicative hiding (some)
2022-10-26 10:37:48 +00:00
import Control.Monad
import Data.Functor
2022-10-26 22:15:32 +00:00
import Data.Void
2022-10-26 10:03:45 +00:00
import System.Environment
2022-10-26 10:37:48 +00:00
import System.Exit
2022-10-26 22:15:32 +00:00
import Text.Megaparsec hiding (satisfy)
import Text.Megaparsec.Char
import Text.ParserCombinators.ReadP hiding (string)
2022-10-26 10:37:48 +00:00
2022-10-26 22:15:32 +00:00
type Parser = Parsec Void String
parseFile :: String -> String
2022-10-26 22:15:32 +00:00
parseFile =
unlines
. map
( \x -> case parse fileParser "" x of
Left bundle -> error ("Error parsing text" ++ errorBundlePretty bundle)
Right text -> text
)
. lines
fileParser :: Parser String
fileParser = undefined
2022-10-26 10:03:45 +00:00
2022-10-26 09:57:55 +00:00
main :: IO ()
main = putStr =<< parseCli =<< getArgs
parseCli :: [String] -> IO String
parseCli [] = return usage
parseCli [x] =
if head x /= '-'
then parseFile <$> readFile x
2022-10-26 22:15:32 +00:00
else case parse argParse "" x of
Left bundle -> fail $ "Failed parsing args" ++ errorBundlePretty bundle
Right text -> return $ useArgs text
parseCli [x, _] = case parse argParse "" x of
Left bundle -> fail $ "Failed parsing args" ++ errorBundlePretty bundle
Right text -> return $ useArgs text
parseCli _ = fail "Too many arugemnts"
2022-10-26 10:37:48 +00:00
2022-10-26 22:15:32 +00:00
argParse :: Parser String
2022-10-26 10:37:48 +00:00
argParse =
2022-10-26 22:15:32 +00:00
string "-" >> some alphaNumChar
2022-10-26 10:37:48 +00:00
useArgs :: String -> String
useArgs [] = ""
useArgs ('h' : _) = usage
useArgs ('v' : _) = version
useArgs (_ : xs) = useArgs xs
2022-10-26 10:37:48 +00:00
usage :: String
usage = "Usage: otm [-hv] [file]"
version :: String
version = "otm 0.1"