Commit e4178452 authored by Duncan Coutts's avatar Duncan Coutts
Browse files

Treat "cabal --flag command" as "cabal command --flag"

eg "cabal -v configure" to mean "cabal configure -v"
For flags that are not recognised as global flags,
pass them on to the sub-command.
parent befc1c42
......@@ -233,7 +233,7 @@ shortOpt y ys rs optDescr = short ads ys rs
short (OptArg f _:_) [] rest = (Opt (f Nothing),rest)
short (OptArg f _:_) xs rest = (Opt (f (Just xs)),rest)
short [] [] rest = (UnreqOpt optStr,rest)
short [] xs rest = (UnreqOpt optStr,('-':xs):rest)
short [] xs rest = (UnreqOpt (optStr++xs),rest)
-- miscellaneous error formatting
......
......@@ -406,24 +406,33 @@ addCommonFlags showOrParseArgs options =
commandParseArgs :: CommandUI flags -> Bool -> [String]
-> CommandParse (flags -> flags, [String])
commandParseArgs command ordered args =
commandParseArgs command global args =
let options = addCommonFlags ParseArgs
$ commandGetOpts ParseArgs command
order | ordered = GetOpt.RequireOrder
order | global = GetOpt.RequireOrder
| otherwise = GetOpt.Permute
in case GetOpt.getOpt order options args of
(flags, _, _)
| not (null [ () | Left ListOptionsFlag <- flags ])
-> CommandList (commandListOptions command)
| not (null [ () | Left HelpFlag <- flags ])
-> CommandHelp (commandHelp command)
(flags, opts, []) -> CommandReadyToGo (accumFlags flags , opts)
(_, _, errs) -> CommandErrors errs
in case GetOpt.getOpt' order options args of
(flags, _, _, _)
| any listFlag flags -> CommandList (commandListOptions command)
| any helpFlag flags -> CommandHelp (commandHelp command)
where listFlag (Left ListOptionsFlag) = True; listFlag _ = False
helpFlag (Left HelpFlag) = True; helpFlag _ = False
(flags, opts, opts', [])
| global || null opts' -> CommandReadyToGo (accum flags, mix opts opts')
| otherwise -> CommandErrors (unrecognised opts')
(_, _, _, errs) -> CommandErrors errs
where -- Note: It is crucial to use reverse function composition here or to
-- reverse the flags here as we want to process the flags left to right
-- but data flow in function compsition is right to left.
accumFlags flags = foldr (flip (.)) id [ f | Right f <- flags ]
accum flags = foldr (flip (.)) id [ f | Right f <- flags ]
unrecognised opts = [ "unrecognized option `" ++ opt ++ "'\n"
| opt <- opts ]
-- For unrecognised global flags we put them in the position just after
-- the command, if there is one. This gives us a chance to parse them
-- as sub-command rather than global flags.
mix [] ys = ys
mix (x:xs) ys = x:ys++xs
data CommandParse flags = CommandHelp (String -> String)
| CommandList [String]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment