2011-01-16 20:05:05 +00:00
|
|
|
|
{- git-annex command line parsing and dispatch
|
2010-11-02 23:04:24 +00:00
|
|
|
|
-
|
2015-07-08 16:33:27 +00:00
|
|
|
|
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
2010-11-02 23:04:24 +00:00
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
|
module CmdLine (
|
2010-12-30 20:52:24 +00:00
|
|
|
|
dispatch,
|
2010-12-31 00:08:22 +00:00
|
|
|
|
usage,
|
2010-12-30 19:06:26 +00:00
|
|
|
|
) where
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
2015-07-08 23:38:56 +00:00
|
|
|
|
import qualified Options.Applicative as O
|
2015-07-09 15:49:52 +00:00
|
|
|
|
import qualified Options.Applicative.Help as H
|
2011-11-16 04:49:09 +00:00
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
|
import Control.Exception (throw)
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
|
import Common.Annex
|
2010-11-02 23:04:24 +00:00
|
|
|
|
import qualified Annex
|
2011-06-30 17:16:57 +00:00
|
|
|
|
import qualified Git
|
2012-04-12 19:34:41 +00:00
|
|
|
|
import qualified Git.AutoCorrect
|
2014-08-23 23:51:33 +00:00
|
|
|
|
import qualified Git.Config
|
2015-07-31 20:00:13 +00:00
|
|
|
|
import Annex.Action
|
2013-04-22 19:36:34 +00:00
|
|
|
|
import Annex.Environment
|
2010-11-02 23:04:24 +00:00
|
|
|
|
import Command
|
2013-07-31 00:24:27 +00:00
|
|
|
|
import Types.Messages
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
2010-12-30 20:52:24 +00:00
|
|
|
|
{- Runs the passed command line. -}
|
2015-07-10 17:18:46 +00:00
|
|
|
|
dispatch :: Bool -> CmdParams -> [Command] -> [GlobalOption] -> [(String, String)] -> IO Git.Repo -> String -> String -> IO ()
|
2015-07-10 04:55:53 +00:00
|
|
|
|
dispatch fuzzyok allargs allcmds globaloptions fields getgitrepo progname progdesc = do
|
2011-03-12 19:30:17 +00:00
|
|
|
|
setupConsole
|
2015-07-08 16:33:27 +00:00
|
|
|
|
go =<< (E.try getgitrepo :: IO (Either E.SomeException Git.Repo))
|
|
|
|
|
where
|
|
|
|
|
go (Right g) = do
|
|
|
|
|
state <- Annex.new g
|
|
|
|
|
Annex.eval state $ do
|
|
|
|
|
checkEnvironment
|
|
|
|
|
forM_ fields $ uncurry Annex.setField
|
2015-09-09 19:55:13 +00:00
|
|
|
|
(cmd, seek, globalconfig) <- parsewith False cmdparser
|
2015-07-08 23:38:56 +00:00
|
|
|
|
(\a -> inRepo $ a . Just)
|
2015-09-09 19:55:13 +00:00
|
|
|
|
(liftIO . O.handleParseResult)
|
2015-07-08 16:33:27 +00:00
|
|
|
|
when (cmdnomessages cmd) $
|
|
|
|
|
Annex.setOutput QuietOutput
|
2015-07-10 06:03:03 +00:00
|
|
|
|
getParsed globalconfig
|
2015-07-08 16:33:27 +00:00
|
|
|
|
whenM (annexDebug <$> Annex.getGitConfig) $
|
|
|
|
|
liftIO enableDebugOutput
|
|
|
|
|
startup
|
|
|
|
|
performCommandAction cmd seek $
|
|
|
|
|
shutdown $ cmdnocommit cmd
|
2015-07-08 19:39:05 +00:00
|
|
|
|
go (Left norepo) = do
|
2015-09-09 19:55:13 +00:00
|
|
|
|
let ingitrepo = \a -> a =<< Git.Config.global
|
|
|
|
|
-- Parse command line with full cmdparser first,
|
|
|
|
|
-- so that help can be displayed for bad parses
|
|
|
|
|
-- even when not run in a repo.
|
|
|
|
|
res <- parsewith False cmdparser ingitrepo return
|
|
|
|
|
case res of
|
|
|
|
|
Failure _ -> void (O.handleParseResult res)
|
|
|
|
|
_ -> do
|
|
|
|
|
-- Parse command line in norepo mode.
|
|
|
|
|
(_, a, _globalconfig) <- parsewith True
|
|
|
|
|
(fromMaybe (throw norepo) . cmdnorepo)
|
|
|
|
|
ingitrepo
|
|
|
|
|
O.handleParseResult
|
|
|
|
|
a
|
2015-07-08 16:33:27 +00:00
|
|
|
|
|
2015-09-09 19:55:13 +00:00
|
|
|
|
parsewith secondrun getparser ingitrepo handleresult =
|
2015-07-10 06:03:03 +00:00
|
|
|
|
case parseCmd progname progdesc globaloptions allargs allcmds getparser of
|
2015-07-08 23:38:56 +00:00
|
|
|
|
O.Failure _ -> do
|
|
|
|
|
-- parse failed, so fall back to
|
|
|
|
|
-- fuzzy matching, or to showing usage
|
2015-09-09 19:55:13 +00:00
|
|
|
|
when (fuzzy && not secondrun) $
|
2015-07-08 23:38:56 +00:00
|
|
|
|
ingitrepo autocorrect
|
2015-09-09 19:55:13 +00:00
|
|
|
|
handleresult (parseCmd progname progdesc globaloptions correctedargs allcmds getparser)
|
|
|
|
|
res -> handleresult res
|
2015-07-08 23:38:56 +00:00
|
|
|
|
where
|
2015-07-09 15:49:52 +00:00
|
|
|
|
autocorrect = Git.AutoCorrect.prepare (fromJust inputcmdname) cmdname cmds
|
|
|
|
|
(fuzzy, cmds, inputcmdname, args) = findCmd fuzzyok allargs allcmds
|
2015-07-08 23:38:56 +00:00
|
|
|
|
name
|
|
|
|
|
| fuzzy = case cmds of
|
2015-07-09 15:49:52 +00:00
|
|
|
|
(c:_) -> Just (cmdname c)
|
2015-07-08 23:38:56 +00:00
|
|
|
|
_ -> inputcmdname
|
|
|
|
|
| otherwise = inputcmdname
|
2015-07-09 15:49:52 +00:00
|
|
|
|
correctedargs = case name of
|
|
|
|
|
Nothing -> allargs
|
|
|
|
|
Just n -> n:args
|
2015-07-08 16:33:27 +00:00
|
|
|
|
|
2015-07-08 19:39:05 +00:00
|
|
|
|
{- Parses command line, selecting one of the commands from the list. -}
|
2015-07-10 17:18:46 +00:00
|
|
|
|
parseCmd :: String -> String -> [GlobalOption] -> CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v, GlobalSetter)
|
2015-07-10 06:03:03 +00:00
|
|
|
|
parseCmd progname progdesc globaloptions allargs allcmds getparser =
|
|
|
|
|
O.execParserPure (O.prefs O.idm) pinfo allargs
|
2015-07-08 16:33:27 +00:00
|
|
|
|
where
|
2015-07-10 06:18:08 +00:00
|
|
|
|
pinfo = O.info (O.helper <*> subcmds) (O.progDescDoc (Just intro))
|
2015-07-09 05:53:15 +00:00
|
|
|
|
subcmds = O.hsubparser $ mconcat $ map mkcommand allcmds
|
2015-07-09 15:49:52 +00:00
|
|
|
|
mkcommand c = O.command (cmdname c) $ O.info (mkparser c) $ O.fullDesc
|
|
|
|
|
<> O.header (synopsis (progname ++ " " ++ cmdname c) (cmddesc c))
|
|
|
|
|
<> O.footer ("For details, run: " ++ progname ++ " help " ++ cmdname c)
|
2015-07-10 06:18:08 +00:00
|
|
|
|
mkparser c = (,,)
|
2015-07-08 16:33:27 +00:00
|
|
|
|
<$> pure c
|
2015-07-08 19:39:05 +00:00
|
|
|
|
<*> getparser c
|
2015-07-10 17:18:46 +00:00
|
|
|
|
<*> combineGlobalOptions globaloptions
|
2015-07-09 15:49:52 +00:00
|
|
|
|
synopsis n d = n ++ " - " ++ d
|
|
|
|
|
intro = mconcat $ concatMap (\l -> [H.text l, H.line])
|
|
|
|
|
(synopsis progname progdesc : commandList allcmds)
|
2010-12-30 19:44:15 +00:00
|
|
|
|
|
2012-04-12 19:34:41 +00:00
|
|
|
|
{- Parses command line params far enough to find the Command to run, and
|
|
|
|
|
- returns the remaining params.
|
|
|
|
|
- Does fuzzy matching if necessary, which may result in multiple Commands. -}
|
2015-07-09 15:49:52 +00:00
|
|
|
|
findCmd :: Bool -> CmdParams -> [Command] -> (Bool, [Command], Maybe String, CmdParams)
|
|
|
|
|
findCmd fuzzyok argv cmds
|
|
|
|
|
| not (null exactcmds) = ret (False, exactcmds)
|
|
|
|
|
| fuzzyok && not (null inexactcmds) = ret (True, inexactcmds)
|
|
|
|
|
| otherwise = ret (False, [])
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
2015-07-09 15:49:52 +00:00
|
|
|
|
ret (fuzzy, matches) = (fuzzy, matches, name, args)
|
2012-11-11 04:51:07 +00:00
|
|
|
|
(name, args) = findname argv []
|
|
|
|
|
findname [] c = (Nothing, reverse c)
|
|
|
|
|
findname (a:as) c
|
|
|
|
|
| "-" `isPrefixOf` a = findname as (a:c)
|
|
|
|
|
| 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
|