X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fmain%2FCmdLineParser.hs;h=d237ad70e25efec18b002fc557bfc6fb191b3581;hb=2eb04ca0f8d0ec72b417cddc60672c696b4a3daa;hp=dfe3125bd13696ac68321ebedf618a25c7bcbb3f;hpb=bec18cb3a1dcbc70b0257a367091c9a5948da6f6;p=ghc-hetmet.git diff --git a/compiler/main/CmdLineParser.hs b/compiler/main/CmdLineParser.hs index dfe3125..d237ad7 100644 --- a/compiler/main/CmdLineParser.hs +++ b/compiler/main/CmdLineParser.hs @@ -28,11 +28,12 @@ data OptKind m -- Suppose the flag is -f | Prefix (String -> m ()) -- -farg | OptPrefix (String -> m ()) -- -f or -farg (i.e. the arg is optional) | OptIntSuffix (Maybe Int -> m ()) -- -f or -f=n; pass n to fn + | IntSuffix (Int -> m ()) -- -f or -f=n; pass n to fn | PassFlag (String -> m ()) -- -f; pass "-f" fn | AnySuffix (String -> m ()) -- -f or -farg; pass entire "-farg" to fn | PrefixPred (String -> Bool) (String -> m ()) | AnySuffixPred (String -> Bool) (String -> m ()) - + processArgs :: Monad m => [(String, OptKind m)] -- cmdline parser spec -> [String] -- args @@ -61,10 +62,11 @@ processOneArg :: OptKind m -> String -> String -> [String] -> Either String (m (), [String]) processOneArg action rest arg args = let dash_arg = '-' : arg + rest_no_eq = dropEq rest in case action of NoArg a -> ASSERT(null rest) Right (a, args) - HasArg f | notNull rest -> Right (f rest, args) + HasArg f | notNull rest_no_eq -> Right (f rest_no_eq, args) | otherwise -> case args of [] -> missingArgErr dash_arg (arg1:args1) -> Right (f arg1, args1) @@ -73,24 +75,27 @@ processOneArg action rest arg args [] -> unknownFlagErr dash_arg (arg1:args1) -> Right (f arg1, args1) - Prefix f | notNull rest -> Right (f rest, args) + Prefix f | notNull rest_no_eq -> Right (f rest_no_eq, args) | otherwise -> unknownFlagErr dash_arg - PrefixPred p f | notNull rest -> Right (f rest, args) - | otherwise -> unknownFlagErr dash_arg + PrefixPred p f | notNull rest_no_eq -> Right (f rest_no_eq, args) + | otherwise -> unknownFlagErr dash_arg PassFlag f | notNull rest -> unknownFlagErr dash_arg | otherwise -> Right (f dash_arg, args) - OptIntSuffix f | Just n <- parseInt rest -> Right (f n, args) + OptIntSuffix f | null rest -> Right (f Nothing, args) + | Just n <- parseInt rest_no_eq -> Right (f (Just n), args) | otherwise -> Left ("malformed integer argument in " ++ dash_arg) - OptPrefix f -> Right (f rest, args) + IntSuffix f | Just n <- parseInt rest_no_eq -> Right (f n, args) + | otherwise -> Left ("malformed integer argument in " ++ dash_arg) + + OptPrefix f -> Right (f rest_no_eq, args) AnySuffix f -> Right (f dash_arg, args) AnySuffixPred p f -> Right (f dash_arg, args) - findArg :: [(String,OptKind a)] -> String -> Maybe (String,OptKind a) findArg spec arg = case [ (removeSpaces rest, k) @@ -105,23 +110,21 @@ arg_ok (NoArg _) rest arg = null rest arg_ok (HasArg _) rest arg = True arg_ok (SepArg _) rest arg = null rest arg_ok (Prefix _) rest arg = notNull rest -arg_ok (PrefixPred p _) rest arg = notNull rest && p rest +arg_ok (PrefixPred p _) rest arg = notNull rest && p (dropEq rest) arg_ok (OptIntSuffix _) rest arg = True +arg_ok (IntSuffix _) rest arg = True arg_ok (OptPrefix _) rest arg = True arg_ok (PassFlag _) rest arg = null rest arg_ok (AnySuffix _) rest arg = True arg_ok (AnySuffixPred p _) rest arg = p arg -parseInt :: String -> Maybe (Maybe Int) +parseInt :: String -> Maybe Int -- Looks for "433" or "=342", with no trailing gubbins --- empty string => Just Nothing --- n or =n => Just (Just n) +-- n or =n => Just n -- gibberish => Nothing -parseInt s - | null s = Just Nothing - | otherwise = case reads (dropEq s) of - ((n,""):_) -> Just (Just n) - other -> Nothing +parseInt s = case reads s of + ((n,""):_) -> Just n + other -> Nothing dropEq :: String -> String -- Discards a leading equals sign