add --and --or --not -( and -)

I dislike -( and -), but without using a different option parser, can't
easily use bare parens.

--and and --or will become more useful once there are more interesting
limits than --exclude
This commit is contained in:
Joey Hess 2011-09-18 18:21:42 -04:00
parent 8a5a92480b
commit b9aa944b09
3 changed files with 22 additions and 9 deletions

View file

@ -98,8 +98,6 @@ options = commonOptions ++
"specify to where to transfer content" "specify to where to transfer content"
, Option ['f'] ["from"] (ReqArg setfrom paramRemote) , Option ['f'] ["from"] (ReqArg setfrom paramRemote)
"specify from where to transfer content" "specify from where to transfer content"
, Option ['x'] ["exclude"] (ReqArg (Limit.exclude) paramGlob)
"skip files matching the glob pattern"
, Option ['N'] ["numcopies"] (ReqArg setnumcopies paramNumber) , Option ['N'] ["numcopies"] (ReqArg setnumcopies paramNumber)
"override default number of copies" "override default number of copies"
, Option [] ["trust"] (ReqArg (Remote.forceTrust Trusted) paramRemote) , Option [] ["trust"] (ReqArg (Remote.forceTrust Trusted) paramRemote)
@ -110,7 +108,9 @@ options = commonOptions ++
"override trust setting to untrusted" "override trust setting to untrusted"
, Option ['c'] ["config"] (ReqArg setgitconfig "NAME=VALUE") , Option ['c'] ["config"] (ReqArg setgitconfig "NAME=VALUE")
"override git configuration setting" "override git configuration setting"
] , Option ['x'] ["exclude"] (ReqArg (Limit.exclude) paramGlob)
"skip files matching the glob pattern"
] ++ matcherOptions
where where
setto v = Annex.changeState $ \s -> s { Annex.toremote = Just v } setto v = Annex.changeState $ \s -> s { Annex.toremote = Just v }
setfrom v = Annex.changeState $ \s -> s { Annex.fromremote = Just v } setfrom v = Annex.changeState $ \s -> s { Annex.fromremote = Just v }

View file

@ -35,7 +35,7 @@ getMatcher = do
Annex.changeState $ \s -> s { Annex.limit = Right matcher } Annex.changeState $ \s -> s { Annex.limit = Right matcher }
return matcher return matcher
{- Adds something to the limit list. -} {- Adds something to the limit list, which is built up reversed. -}
add :: Limit -> Annex () add :: Limit -> Annex ()
add l = Annex.changeState $ \s -> s { Annex.limit = append $ Annex.limit s } add l = Annex.changeState $ \s -> s { Annex.limit = append $ Annex.limit s }
where where
@ -43,16 +43,16 @@ add l = Annex.changeState $ \s -> s { Annex.limit = append $ Annex.limit s }
append _ = error "internal" append _ = error "internal"
{- Adds a new limit. -} {- Adds a new limit. -}
addl :: (FilePath -> Annex Bool) -> Annex () addlimit :: (FilePath -> Annex Bool) -> Annex ()
addl = add . Utility.Matcher.Operation addlimit = add . Utility.Matcher.Operation
{- Adds a new token. -} {- Adds a new token. -}
addt :: String -> Annex () token :: String -> Annex ()
addt = add . Utility.Matcher.Token token = add . Utility.Matcher.Token
{- Add a limit to skip files that do not match the glob. -} {- Add a limit to skip files that do not match the glob. -}
exclude :: String -> Annex () exclude :: String -> Annex ()
exclude glob = addl $ return . notExcluded exclude glob = addlimit $ return . notExcluded
where where
notExcluded f = isNothing $ match cregex f [] notExcluded f = isNothing $ match cregex f []
cregex = compile regex [] cregex = compile regex []

View file

@ -14,6 +14,7 @@ import Control.Monad.State (liftIO)
import qualified Annex import qualified Annex
import Types import Types
import Command import Command
import Limit
{- Each dashed command-line option results in generation of an action {- Each dashed command-line option results in generation of an action
- in the Annex monad that performs the necessary setting. - in the Annex monad that performs the necessary setting.
@ -47,3 +48,15 @@ commonOptions =
setforcebackend v = Annex.changeState $ \s -> s { Annex.forcebackend = Just v } setforcebackend v = Annex.changeState $ \s -> s { Annex.forcebackend = Just v }
setdebug = liftIO $ updateGlobalLogger rootLoggerName $ setdebug = liftIO $ updateGlobalLogger rootLoggerName $
setLevel DEBUG setLevel DEBUG
matcherOptions :: [Option]
matcherOptions =
[ longopt "not" "negate next option"
, longopt "and" "both previous and next option must match"
, longopt "or" "either previous or next option must match"
, shortopt "(" "open group of options"
, shortopt ")" "close group of options"
]
where
longopt o d = Option [] [o] (NoArg (token o)) d
shortopt o d = Option o [] (NoArg (token o)) d