-
Notifications
You must be signed in to change notification settings - Fork 0
/
EdOption.hs
38 lines (28 loc) · 1.03 KB
/
EdOption.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
module EdOption (EdOption(..), parseArgs) where
import EdError (EdError(ILLIGAL_OPTION))
data EdOption = OHelp
| OVersion
| OScript
| OPrompt String
| OFile String
| OError EdError String
deriving (Show, Eq)
parseArgs :: [String] -> [EdOption]
parseArgs [] = []
parseArgs xs = let (opt, rem) = getOption xs
in opt : parseArgs rem
getOption :: [String] -> (EdOption, [String])
getOption xs@(x:xs') = if isOption x
then encode xs
else (OFile x, xs')
isOption :: String -> Bool
isOption (c:cs) = if c == '-'
then True
else False
encode :: [String] -> (EdOption, [String])
encode (x:xs) = case x of
"-h" -> (OHelp, xs)
"-v" -> (OVersion, xs)
"-p" -> (OPrompt (head xs), tail xs)
"-s" -> (OScript, xs)
otherwise -> (OError ILLIGAL_OPTION x, [])