/
Options.hs
51 lines (35 loc) · 1.19 KB
/
Options.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
module Options
( option
, anyString
, string
, initialChar
, module Text.ParserCombinators.Parsec
) where
import Text.ParserCombinators.Parsec hiding
( parse
, string
, oneOf
, option
, (<|>)
)
import Control.Monad
import Control.Monad.Instances
noMore = string "--"
stringPrim = tokenPrim show nextPos
where
nextPos sp _ _ = incSourceLine sp 1
anyString = stringPrim Just
string s = stringPrim $ (>> Just s) . guard . (== s)
initialChar c = stringPrim test
where
test s@(c:_) = Just s
test _ = Nothing
oneOf strings = choice $ map (try . string) strings
option :: String -> [String] -> SParser String
option (s:hort) long = return [s] << option' (s:hort) long
option short (lon:g) = return lon << option' short (lon:g)
option [ ] [ ] = return [ ]
option' short long = do
oneOf $ map (('-':) . (:[])) short ++ map ("--" ++) long
(<<) = flip (>>)
type SParser res = GenParser String () res