-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
55 lines (47 loc) · 1.93 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Main where
import System.Environment (getArgs)
import System.IO (stderr)
import Data.Maybe (isJust)
import SimpleParser.Parser (parseMain, parseTest)
import SimpleParser.ArgParser (Args (..), Param (..), Positional (..), Optional (..), parseArgs, getParamValue)
getInput :: Param -> String
getInput p = case getParamValue "INPUT" p of
Just [input] -> input
_ -> ""
parsePositional :: Int -> Maybe Positional
parsePositional x = case x of
0 -> Just $ MandatoryParam "INPUT" ""
_ -> Nothing
parseFlag :: String -> Maybe Optional
parseFlag x = case x of
-- Flags that do not consume extra parameters
"--help" -> Just $ OptionalFlag "help"
"-h" -> Just $ OptionalFlag "help"
"--file" -> Just $ OptionalFlag "file"
"-f" -> Just $ OptionalFlag "file"
"--test" -> Just $ OptionalFlag "test"
"-t" -> Just $ OptionalFlag "test"
-- Flags that consume one extra parameter
"-o" -> Just $ OptionalParam "output" ""
"--output" -> Just $ OptionalParam "output" ""
"--" -> Just $ OptionalParam "INPUT" ""
_ -> Nothing
helpString :: String
helpString = "usage: simple-parser [-h] INPUT\n\
\\n\
\Parses an expression and returns the parse tree\n\
\\n\
\positional arguments:\n\
\ INPUT input string or path of input file\n\
\\n\
\options:\n\
\ -h, --help show this help message and exit"
main :: IO ()
main = do
args <- getArgs
case parseArgs parsePositional parseFlag args of
Args (Left e) -> putStrLn $ "\ESC[91m\ESC[1mError\ESC[0m: " ++ show e
Args (Right (p, _))
| isJust $ getParamValue "help" p -> putStrLn helpString
| isJust $ getParamValue "test" p -> putStrLn $ parseTest (getInput p)
| otherwise -> putStr $ parseMain (getInput p)