git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Make directive parser recognize only unambiguous commands
9f938b8Łukasz Hanuszczak
psci/Directive.hs+5−3
@@ -15,8 +15,7 @@
15
15
16module Directive where
16module Directive where
17
17
18−import Control.Applicative
19−import Data.List (find, isPrefixOf)
18+import Data.List (isPrefixOf)
20
19
21data Directive
20data Directive
22 = Help
21 = Help
@@ -47,7 +46,10 @@ commands Show = ["show"]
47-- Tries to parse given string into a directive.
46-- Tries to parse given string into a directive.
48--
47--
49parseDirective :: String -> Maybe Directive
48parseDirective :: String -> Maybe Directive
50−parseDirective cmd = fst <$> find (matches . snd) mapping
49+parseDirective cmd =
50+ case filter (matches . snd) mapping of
51+ [directive] -> Just $ fst directive
52+ _ -> Nothing
51 where
53 where
52 mapping :: [(Directive, [String])]
54 mapping :: [(Directive, [String])]
53 mapping = zip directives (map commands directives)
55 mapping = zip directives (map commands directives)