This commit is contained in:
Joey Hess 2021-02-02 14:27:42 -04:00
parent d0fe0c5e10
commit 4cc65d97fc
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38

View file

@ -71,7 +71,6 @@ dispatch fuzzyok allargs allcmds globaloptions fields getgitrepo progname progde
res -> handleresult res res -> handleresult res
where where
autocorrect = Git.AutoCorrect.prepare (fromJust inputcmdname) cmdname cmds autocorrect = Git.AutoCorrect.prepare (fromJust inputcmdname) cmdname cmds
(fuzzy, cmds, inputcmdname, args) = findCmd fuzzyok allargs allcmds
name name
| fuzzy = case cmds of | fuzzy = case cmds of
(c:_) -> Just (cmdname c) (c:_) -> Just (cmdname c)
@ -81,6 +80,9 @@ dispatch fuzzyok allargs allcmds globaloptions fields getgitrepo progname progde
Nothing -> allargs Nothing -> allargs
Just n -> n:args Just n -> n:args
(inputcmdname, args) = findCmdName allargs
(fuzzy, cmds) = findCmd fuzzyok allcmds inputcmdname
{- Parses command line, selecting one of the commands from the list. -} {- Parses command line, selecting one of the commands from the list. -}
parseCmd :: String -> String -> [GlobalOption] -> CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v, GlobalSetter) parseCmd :: String -> String -> [GlobalOption] -> CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v, GlobalSetter)
parseCmd progname progdesc globaloptions allargs allcmds getparser = parseCmd progname progdesc globaloptions allargs allcmds getparser =
@ -99,25 +101,27 @@ parseCmd progname progdesc globaloptions allargs allcmds getparser =
intro = mconcat $ concatMap (\l -> [H.text l, H.line]) intro = mconcat $ concatMap (\l -> [H.text l, H.line])
(synopsis progname progdesc : commandList allcmds) (synopsis progname progdesc : commandList allcmds)
{- Parses command line params far enough to find the Command to run, and {- Finds the Command that matches the subcommand name.
- returns the remaining params.
- Does fuzzy matching if necessary, which may result in multiple Commands. -} - Does fuzzy matching if necessary, which may result in multiple Commands. -}
findCmd :: Bool -> CmdParams -> [Command] -> (Bool, [Command], Maybe String, CmdParams) findCmd :: Bool -> [Command] -> Maybe String -> (Bool, [Command])
findCmd fuzzyok argv cmds findCmd fuzzyok cmds (Just n)
| not (null exactcmds) = ret (False, exactcmds) | not (null exactcmds) = (False, exactcmds)
| fuzzyok && not (null inexactcmds) = ret (True, inexactcmds) | fuzzyok && not (null inexactcmds) = (True, inexactcmds)
| otherwise = ret (False, []) | otherwise = (False, [])
where
exactcmds = filter (\c -> cmdname c == n) cmds
inexactcmds = Git.AutoCorrect.fuzzymatches n cmdname cmds
findCmd _ _ Nothing = (False, [])
{- Parses command line params far enough to find the subcommand name. -}
findCmdName :: CmdParams -> (Maybe String, CmdParams)
findCmdName argv = (name, args)
where where
ret (fuzzy, matches) = (fuzzy, matches, name, args)
(name, args) = findname argv [] (name, args) = findname argv []
findname [] c = (Nothing, reverse c) findname [] c = (Nothing, reverse c)
findname (a:as) c findname (a:as) c
| "-" `isPrefixOf` a = findname as (a:c) | "-" `isPrefixOf` a = findname as (a:c)
| otherwise = (Just a, reverse c ++ as) | otherwise = (Just a, reverse c ++ as)
exactcmds = filter (\c -> name == Just (cmdname c)) cmds
inexactcmds = case name of
Nothing -> []
Just n -> Git.AutoCorrect.fuzzymatches n cmdname cmds
prepRunCommand :: Command -> GlobalSetter -> Annex () prepRunCommand :: Command -> GlobalSetter -> Annex ()
prepRunCommand cmd globalconfig = do prepRunCommand cmd globalconfig = do