improve autocorrection code so that --bash-completion-script etc will work

git-annex --bash-completion-script git-annex  will now work; before
the command autocorrection would screw it up
This commit is contained in:
Joey Hess 2015-07-08 19:38:56 -04:00
parent 3332df2c52
commit 463709ab2a

View file

@ -13,10 +13,10 @@ module CmdLine (
shutdown shutdown
) where ) where
import qualified Options.Applicative as O
import qualified Control.Exception as E import qualified Control.Exception as E
import qualified Data.Map as M import qualified Data.Map as M
import Control.Exception (throw) import Control.Exception (throw)
import qualified Options.Applicative as O
#ifndef mingw32_HOST_OS #ifndef mingw32_HOST_OS
import System.Posix.Signals import System.Posix.Signals
#endif #endif
@ -41,11 +41,9 @@ dispatch fuzzyok allargs allcmds commonoptions fields header getgitrepo = do
state <- Annex.new g state <- Annex.new g
Annex.eval state $ do Annex.eval state $ do
checkEnvironment checkEnvironment
when fuzzy $
inRepo $ autocorrect . Just
forM_ fields $ uncurry Annex.setField forM_ fields $ uncurry Annex.setField
(cmd, seek) <- liftIO $ (cmd, seek) <- parsewith cmdparser
O.handleParseResult (parseCmd (name:args) allcmds cmdparser) (\a -> inRepo $ a . Just)
when (cmdnomessages cmd) $ when (cmdnomessages cmd) $
Annex.setOutput QuietOutput Annex.setOutput QuietOutput
-- TODO: propigate global options to annex state (how?) -- TODO: propigate global options to annex state (how?)
@ -55,21 +53,29 @@ dispatch fuzzyok allargs allcmds commonoptions fields header getgitrepo = do
performCommandAction cmd seek $ performCommandAction cmd seek $
shutdown $ cmdnocommit cmd shutdown $ cmdnocommit cmd
go (Left norepo) = do go (Left norepo) = do
when fuzzy $ (_, a) <- parsewith
autocorrect =<< Git.Config.global (fromMaybe (throw norepo) . cmdnorepo)
let norepoparser = fromMaybe (throw norepo) . cmdnorepo (\a -> a =<< Git.Config.global)
(_cmd, a) <- O.handleParseResult (parseCmd (name:args) allcmds norepoparser)
a a
autocorrect = Git.AutoCorrect.prepare inputcmdname cmdname cmds parsewith getparser ingitrepo =
err msg = msg ++ "\n\n" ++ usage header allcmds case parseCmd allargs allcmds getparser of
(fuzzy, cmds, inputcmdname, args) = findCmd fuzzyok allargs allcmds err O.Failure _ -> do
name -- parse failed, so fall back to
| fuzzy = case cmds of -- fuzzy matching, or to showing usage
[c] -> cmdname c when fuzzy $
_ -> inputcmdname ingitrepo autocorrect
| otherwise = inputcmdname liftIO (O.handleParseResult (parseCmd (name:args) allcmds getparser))
res -> liftIO (O.handleParseResult res)
where
autocorrect = Git.AutoCorrect.prepare inputcmdname cmdname cmds
err msg = msg ++ "\n\n" ++ usage header allcmds
(fuzzy, cmds, inputcmdname, args) = findCmd fuzzyok allargs allcmds err
name
| fuzzy = case cmds of
(c:_) -> cmdname c
_ -> inputcmdname
| otherwise = inputcmdname
{- Parses command line, selecting one of the commands from the list. -} {- Parses command line, selecting one of the commands from the list. -}
parseCmd :: CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v) parseCmd :: CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v)