2010-12-30 19:06:26 +00:00
|
|
|
{- git-annex command line parsing
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
module CmdLine (
|
|
|
|
parseCmd,
|
|
|
|
Option,
|
|
|
|
storeOptBool,
|
|
|
|
storeOptString,
|
|
|
|
) where
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
|
|
import System.Console.GetOpt
|
2010-11-11 22:54:52 +00:00
|
|
|
import Control.Monad (when)
|
2010-12-08 18:07:49 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
|
|
import qualified Annex
|
|
|
|
import Types
|
|
|
|
import Command
|
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
{- Each dashed command-line option results in generation of an action
|
|
|
|
- in the Annex monad that performs the necessary setting.
|
|
|
|
-}
|
|
|
|
type Option = OptDescr (Annex ())
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
storeOptBool :: FlagName -> Bool -> Annex ()
|
|
|
|
storeOptBool name val = Annex.flagChange name $ FlagBool val
|
|
|
|
storeOptString :: FlagName -> String -> Annex ()
|
|
|
|
storeOptString name val = Annex.flagChange name $ FlagString val
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-08 18:07:49 +00:00
|
|
|
{- Parses command line, stores configure flags, and returns a
|
|
|
|
- list of actions to be run in the Annex monad. -}
|
2010-12-30 19:06:26 +00:00
|
|
|
parseCmd :: [String] -> String -> [Command] -> [Option] -> Annex [Annex Bool]
|
|
|
|
parseCmd argv header cmds options = do
|
2010-12-08 18:07:49 +00:00
|
|
|
(flags, params) <- liftIO $ getopt
|
2010-12-30 19:06:26 +00:00
|
|
|
when (null params) $ error usagemsg
|
2010-11-06 21:06:19 +00:00
|
|
|
case lookupCmd (head params) of
|
2010-12-30 19:06:26 +00:00
|
|
|
[] -> error usagemsg
|
2010-12-30 18:19:16 +00:00
|
|
|
[command] -> do
|
2010-12-08 18:07:49 +00:00
|
|
|
_ <- sequence flags
|
2010-12-30 18:19:16 +00:00
|
|
|
prepCmd command (drop 1 params)
|
|
|
|
_ -> error "internal error: multiple matching commands"
|
2010-11-02 23:04:24 +00:00
|
|
|
where
|
|
|
|
getopt = case getOpt Permute options argv of
|
|
|
|
(flags, params, []) -> return (flags, params)
|
2010-12-30 19:06:26 +00:00
|
|
|
(_, _, errs) -> ioError (userError (concat errs ++ usagemsg))
|
2010-12-30 18:19:16 +00:00
|
|
|
lookupCmd cmd = filter (\c -> cmd == cmdname c) cmds
|
2010-12-30 19:06:26 +00:00
|
|
|
usagemsg = usage header cmds options
|
|
|
|
|
|
|
|
{- Usage message with lists of commands and options. -}
|
|
|
|
usage :: String -> [Command] -> [Option] -> String
|
|
|
|
usage header cmds options =
|
|
|
|
usageInfo header options ++ "\nSubcommands:\n" ++ cmddescs
|
|
|
|
where
|
|
|
|
cmddescs = unlines $ map (indent . showcmd) cmds
|
|
|
|
showcmd c =
|
|
|
|
cmdname c ++
|
2010-12-30 19:12:55 +00:00
|
|
|
pad (commandlen + 1) (cmdname c) ++
|
2010-12-30 19:06:26 +00:00
|
|
|
cmdparams c ++
|
2010-12-30 19:12:55 +00:00
|
|
|
pad (commandparamlen + 2) (cmdparams c) ++
|
2010-12-30 19:06:26 +00:00
|
|
|
cmddesc c
|
|
|
|
indent l = " " ++ l
|
|
|
|
pad n s = replicate (n - length s) ' '
|
2010-12-30 19:12:55 +00:00
|
|
|
longest l = foldl max 0 $ map length l
|
|
|
|
commandlen = longest $ map cmdname cmds
|
|
|
|
commandparamlen = longest $ map cmdparams cmds
|