From 7be58b5e115f5c780b042b7f28e010842d5f5974 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 20 Aug 2015 17:18:21 -0400 Subject: [PATCH] make sync --no-content be accepted It's the default, but this is a step toward changing that default later.. --- Command/Sync.hs | 6 +++--- Utility/OptParse.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 Utility/OptParse.hs diff --git a/Command/Sync.hs b/Command/Sync.hs index 46a03a4de8..87a0fbcf6a 100644 --- a/Command/Sync.hs +++ b/Command/Sync.hs @@ -47,6 +47,7 @@ import Annex.AutoMerge import Annex.Ssh import Annex.BloomFilter import Utility.Bloom +import Utility.OptParse import Control.Concurrent.MVar import qualified Data.Map as M @@ -67,9 +68,8 @@ data SyncOptions = SyncOptions optParser :: CmdParamsDesc -> Parser SyncOptions optParser desc = SyncOptions <$> cmdParams desc - <*> switch - ( long "content" - <> help "also transfer file contents" + <*> invertableSwitch "content" False + ( help "also transfer file contents" ) <*> optional (strOption ( long "message" <> short 'm' <> metavar "MSG" diff --git a/Utility/OptParse.hs b/Utility/OptParse.hs new file mode 100644 index 0000000000..f58e8fadf6 --- /dev/null +++ b/Utility/OptParse.hs @@ -0,0 +1,45 @@ +{- optparse-applicative additions + - + - Copyright 2015 Joey Hess + - + - License: BSD-2-clause + -} + +module Utility.OptParse where + +import Options.Applicative +import Data.Monoid + +-- | A switch that can be enabled using --foo and disabled using --no-foo. +-- +-- The option modifier is applied to only the option that is *not* enabled +-- by default. For example: +-- +-- > invertableSwitch "recursive" True (help "do not recurse into directories") +-- +-- This example makes --recursive enabled by default, so +-- the help is shown only for --no-recursive. +invertableSwitch + :: String -- ^ long option + -> Bool -- ^ is switch enabled by default? + -> Mod FlagFields Bool -- ^ option modifier + -> Parser Bool +invertableSwitch longopt defv optmod = invertableSwitch' longopt defv + (if defv then mempty else optmod) + (if defv then optmod else mempty) + +-- | Allows providing option modifiers for both --foo and --no-foo. +invertableSwitch' + :: String -- ^ long option (eg "foo") + -> Bool -- ^ is switch enabled by default? + -> Mod FlagFields Bool -- ^ option modifier for --foo + -> Mod FlagFields Bool -- ^ option modifier for --no-foo + -> Parser Bool +invertableSwitch' longopt defv enmod dismod = collapse <$> many + ( flag' True (enmod <> long longopt) + <|> flag' False (dismod <> long nolongopt) + ) + where + nolongopt = "no-" ++ longopt + collapse [] = defv + collapse l = last l