implement withGlobalOptions, and convert Find

This commit is contained in:
Joey Hess 2015-07-10 12:47:35 -04:00
parent 7af0893abd
commit 5cc882a35e
6 changed files with 44 additions and 24 deletions

View file

@ -31,7 +31,6 @@ import Annex.Content
import Annex.Environment import Annex.Environment
import Command import Command
import Types.Messages import Types.Messages
import CmdLine.GlobalSetter
{- Runs the passed command line. -} {- Runs the passed command line. -}
dispatch :: Bool -> CmdParams -> [Command] -> [Parser GlobalSetter] -> [(String, String)] -> IO Git.Repo -> String -> String -> IO () dispatch :: Bool -> CmdParams -> [Command] -> [Parser GlobalSetter] -> [(String, String)] -> IO Git.Repo -> String -> String -> IO ()

View file

@ -56,7 +56,7 @@ import qualified Command.AddUnused
import qualified Command.Unlock import qualified Command.Unlock
import qualified Command.Lock import qualified Command.Lock
import qualified Command.PreCommit import qualified Command.PreCommit
--import qualified Command.Find import qualified Command.Find
--import qualified Command.FindRef --import qualified Command.FindRef
--import qualified Command.Whereis --import qualified Command.Whereis
--import qualified Command.List --import qualified Command.List
@ -183,7 +183,7 @@ cmds =
-- , Command.Unused.cmd -- , Command.Unused.cmd
-- , Command.DropUnused.cmd -- , Command.DropUnused.cmd
, Command.AddUnused.cmd , Command.AddUnused.cmd
-- , Command.Find.cmd , Command.Find.cmd
-- , Command.FindRef.cmd -- , Command.FindRef.cmd
-- , Command.Whereis.cmd -- , Command.Whereis.cmd
-- , Command.List.cmd -- , Command.List.cmd

View file

@ -200,8 +200,8 @@ fileMatchingOptions' =
"match files smaller than a size" "match files smaller than a size"
] ]
parseCombiningOptions :: Parser [GlobalSetter] combiningOptions :: Parser [GlobalSetter]
parseCombiningOptions = combiningOptions =
many $ longopt "not" "negate next option" many $ longopt "not" "negate next option"
<|> longopt "and" "both previous and next option must match" <|> longopt "and" "both previous and next option must match"
<|> longopt "or" "either previous or next option must match" <|> longopt "or" "either previous or next option must match"
@ -211,8 +211,8 @@ parseCombiningOptions =
longopt o h = globalFlag (Limit.addToken o) ( long o <> help h ) longopt o h = globalFlag (Limit.addToken o) ( long o <> help h )
shortopt o h = globalFlag (Limit.addToken [o]) ( short o <> help h) shortopt o h = globalFlag (Limit.addToken [o]) ( short o <> help h)
parseJsonOption :: Parser GlobalSetter jsonOption :: Parser GlobalSetter
parseJsonOption = globalFlag (Annex.setOutput JSONOutput) jsonOption = globalFlag (Annex.setOutput JSONOutput)
( long "json" <> short 'j' ( long "json" <> short 'j'
<> help "enable JSON output" <> help "enable JSON output"
) )

View file

@ -13,6 +13,7 @@ module Command (
noCommit, noCommit,
noMessages, noMessages,
withOptions, withOptions,
withGlobalOptions,
next, next,
stop, stop,
stopUnless, stopUnless,
@ -33,6 +34,7 @@ import Checks as ReExported
import CmdLine.Usage as ReExported import CmdLine.Usage as ReExported
import CmdLine.Action as ReExported import CmdLine.Action as ReExported
import CmdLine.Option as ReExported import CmdLine.Option as ReExported
import CmdLine.GlobalSetter as ReExported
import CmdLine.GitAnnex.Options as ReExported import CmdLine.GitAnnex.Options as ReExported
import Options.Applicative as ReExported hiding (command) import Options.Applicative as ReExported hiding (command)
@ -78,6 +80,19 @@ noRepo a c = c { cmdnorepo = Just (a (cmdparamdesc c)) }
withOptions :: [Option] -> Command -> Command withOptions :: [Option] -> Command -> Command
withOptions o c = c { cmdoptions = cmdoptions c ++ o } withOptions o c = c { cmdoptions = cmdoptions c ++ o }
{- Adds global options to a command's option parser, and modifies its seek
- option to first run actions for them.
-}
withGlobalOptions :: [Parser GlobalSetter] -> Command -> Command
withGlobalOptions os c = c { cmdparser = apply <$> mixin (cmdparser c) }
where
mixin p = (,)
<$> p
<*> combineGlobalSetters os
apply (seek, globalsetters) = do
void $ getParsed globalsetters
seek
{- For start and perform stages to indicate what step to run next. -} {- For start and perform stages to indicate what step to run next. -}
next :: a -> Annex (Maybe a) next :: a -> Annex (Maybe a)
next a = return $ Just a next a = return $ Just a

View file

@ -11,7 +11,7 @@ import Common.Annex
import Command import Command
import CmdLine.Batch import CmdLine.Batch
import qualified Utility.Format import qualified Utility.Format
import Command.Find (formatOption, getFormat, showFormatted, keyVars) import Command.Find (FindOptions(..), showFormatted, keyVars)
import Types.Key import Types.Key
cmd :: Command cmd :: Command

View file

@ -14,7 +14,6 @@ import Common.Annex
import Command import Command
import Annex.Content import Annex.Content
import Limit import Limit
import qualified Annex
import qualified Utility.Format import qualified Utility.Format
import Utility.DataUnits import Utility.DataUnits
import Types.Key import Types.Key
@ -22,27 +21,34 @@ import Types.Key
cmd :: Command cmd :: Command
cmd = withOptions annexedMatchingOptions $ mkCommand $ cmd = withOptions annexedMatchingOptions $ mkCommand $
command "find" SectionQuery "lists available files" command "find" SectionQuery "lists available files"
paramPaths (withParams seek) paramPaths (seek <$$> optParser)
mkCommand :: Command -> Command mkCommand :: Command -> Command
mkCommand = noCommit . noMessages . withOptions [formatOption, print0Option, jsonOption] mkCommand = noCommit . noMessages . withGlobalOptions [jsonOption]
formatOption :: Option data FindOptions = FindOptions
formatOption = fieldOption [] "format" paramFormat "control format of output" { findThese :: CmdParams
, formatOption :: Maybe Utility.Format.Format
}
getFormat :: Annex (Maybe Utility.Format.Format) optParser :: CmdParamsDesc -> Parser FindOptions
getFormat = getOptionField formatOption $ return . fmap Utility.Format.gen optParser desc = FindOptions
<$> cmdParams desc
<*> optional parseFormatOption
print0Option :: Option parseFormatOption :: Parser Utility.Format.Format
print0Option = Option [] ["print0"] (NoArg set) parseFormatOption =
"terminate output with null" option (Utility.Format.gen <$> str)
where ( long "format" <> metavar paramFormat
set = Annex.setField (optionName formatOption) "${file}\0" <> help "control format of output"
)
<|> flag' (Utility.Format.gen "${file}\0")
( long "print0"
<> help "output filenames terminated with nulls"
)
seek :: CmdParams -> CommandSeek seek :: FindOptions -> CommandSeek
seek ps = do seek o = withFilesInGit (whenAnnexed $ start (formatOption o)) (findThese o)
format <- getFormat
withFilesInGit (whenAnnexed $ start format) ps
start :: Maybe Utility.Format.Format -> FilePath -> Key -> CommandStart start :: Maybe Utility.Format.Format -> FilePath -> Key -> CommandStart
start format file key = do start format file key = do