1 -----------------------------------------------------------------------------
5 -- This is an abstract command-line parser used by both StaticFlags and
8 -- (c) The University of Glasgow 2005
10 -----------------------------------------------------------------------------
12 module CmdLineParser (
13 processArgs, OptKind(..),
14 CmdLineP(..), getCmdLineState, putCmdLineState
17 -- XXX This define is a bit of a hack, and should be done more nicely
18 #define FAST_STRING_NOT_NEEDED 1
19 #include "HsVersions.h"
24 data OptKind m -- Suppose the flag is -f
25 = NoArg (m ()) -- -f all by itself
26 | HasArg (String -> m ()) -- -farg or -f arg
27 | SepArg (String -> m ()) -- -f arg
28 | Prefix (String -> m ()) -- -farg
29 | OptPrefix (String -> m ()) -- -f or -farg (i.e. the arg is optional)
30 | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn
31 | IntSuffix (Int -> m ()) -- -f or -f=n; pass n to fn
32 | PassFlag (String -> m ()) -- -f; pass "-f" fn
33 | AnySuffix (String -> m ()) -- -f or -farg; pass entire "-farg" to fn
34 | PrefixPred (String -> Bool) (String -> m ())
35 | AnySuffixPred (String -> Bool) (String -> m ())
37 processArgs :: Monad m
38 => [(String, OptKind m)] -- cmdline parser spec
41 [String], -- spare args
44 processArgs spec args = process spec args [] []
46 process _spec [] spare errs =
47 return (reverse spare, reverse errs)
49 process spec (dash_arg@('-':arg):args) spare errs =
50 case findArg spec arg of
52 case processOneArg action rest arg args of
53 Left err -> process spec args spare (err:errs)
54 Right (action,rest) -> action >> process spec rest spare errs
55 Nothing -> process spec args (dash_arg:spare) errs
57 process spec (arg:args) spare errs =
58 process spec args (arg:spare) errs
61 processOneArg :: OptKind m -> String -> String -> [String]
62 -> Either String (m (), [String])
63 processOneArg action rest arg args
64 = let dash_arg = '-' : arg
65 rest_no_eq = dropEq rest
67 NoArg a -> ASSERT(null rest) Right (a, args)
69 HasArg f | notNull rest_no_eq -> Right (f rest_no_eq, args)
70 | otherwise -> case args of
71 [] -> missingArgErr dash_arg
72 (arg1:args1) -> Right (f arg1, args1)
74 SepArg f -> case args of
75 [] -> unknownFlagErr dash_arg
76 (arg1:args1) -> Right (f arg1, args1)
78 Prefix f | notNull rest_no_eq -> Right (f rest_no_eq, args)
79 | otherwise -> unknownFlagErr dash_arg
81 PrefixPred _ f | notNull rest_no_eq -> Right (f rest_no_eq, args)
82 | otherwise -> unknownFlagErr dash_arg
84 PassFlag f | notNull rest -> unknownFlagErr dash_arg
85 | otherwise -> Right (f dash_arg, args)
87 OptIntSuffix f | null rest -> Right (f Nothing, args)
88 | Just n <- parseInt rest_no_eq -> Right (f (Just n), args)
89 | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
91 IntSuffix f | Just n <- parseInt rest_no_eq -> Right (f n, args)
92 | otherwise -> Left ("malformed integer argument in " ++ dash_arg)
94 OptPrefix f -> Right (f rest_no_eq, args)
95 AnySuffix f -> Right (f dash_arg, args)
96 AnySuffixPred _ f -> Right (f dash_arg, args)
99 findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a)
101 = case [ (removeSpaces rest, k)
103 Just rest <- [maybePrefixMatch pat arg],
109 arg_ok :: OptKind t -> [Char] -> String -> Bool
110 arg_ok (NoArg _) rest _ = null rest
111 arg_ok (HasArg _) _ _ = True
112 arg_ok (SepArg _) rest _ = null rest
113 arg_ok (Prefix _) rest _ = notNull rest
114 arg_ok (PrefixPred p _) rest _ = notNull rest && p (dropEq rest)
115 arg_ok (OptIntSuffix _) _ _ = True
116 arg_ok (IntSuffix _) _ _ = True
117 arg_ok (OptPrefix _) _ _ = True
118 arg_ok (PassFlag _) rest _ = null rest
119 arg_ok (AnySuffix _) _ _ = True
120 arg_ok (AnySuffixPred p _) _ arg = p arg
122 parseInt :: String -> Maybe Int
123 -- Looks for "433" or "=342", with no trailing gubbins
125 -- gibberish => Nothing
126 parseInt s = case reads s of
130 dropEq :: String -> String
131 -- Discards a leading equals sign
135 unknownFlagErr :: String -> Either String a
136 unknownFlagErr f = Left ("unrecognised flag: " ++ f)
138 missingArgErr :: String -> Either String a
139 missingArgErr f = Left ("missing argument for flag: " ++ f)
141 -- -----------------------------------------------------------------------------
142 -- A state monad for use in the command-line parser
144 newtype CmdLineP s a = CmdLineP { runCmdLine :: s -> (a, s) }
146 instance Monad (CmdLineP s) where
147 return a = CmdLineP $ \s -> (a, s)
148 m >>= k = CmdLineP $ \s -> let
149 (a, s') = runCmdLine m s
150 in runCmdLine (k a) s'
152 getCmdLineState :: CmdLineP s s
153 getCmdLineState = CmdLineP $ \s -> (s,s)
154 putCmdLineState :: s -> CmdLineP s ()
155 putCmdLineState s = CmdLineP $ \_ -> ((),s)