Simplify process

Note to self: This is a lot like foldr or traverse, and would be trivial to
implement as such if it didn't need to peek ahead when takesArg is true. I
wonder if there's a clean way to implement it in terms of one of them anyway.
This commit is contained in:
Joseph C. Sible 2020-04-05 16:38:52 -04:00
parent fb55072302
commit d22e0aa4a7
1 changed files with 8 additions and 13 deletions

View File

@ -919,20 +919,15 @@ getOpts string flags = process flags
flagMap = Map.fromList $ ("", False) : flagList string
process [] = return []
process [(token, flag)] = do
process ((token1, flag):rest1) = do
takesArg <- Map.lookup flag flagMap
guard $ not takesArg
return [(flag, token)]
process ((token1, flag1):rest2@((token2, flag2):rest)) = do
takesArg <- Map.lookup flag1 flagMap
if takesArg
then do
guard $ null flag2
(token, rest) <- if takesArg
then case rest1 of
(token2, ""):rest2 -> return (token2, rest2)
_ -> fail "takesArg without valid arg"
else return (token1, rest1)
more <- process rest
return $ (flag1, token2) : more
else do
more <- process rest2
return $ (flag1, token1) : more
return $ (flag, token) : more
supportsArrays Bash = True
supportsArrays Ksh = True