summaryrefslogtreecommitdiff
path: root/Utility/OptParse.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-08-20 17:18:21 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-08-20 17:21:14 -0400
commit2c7e74b116e10112e9f4f5d9f22a93ae7e4bee9e (patch)
tree8a1665fa6cdb115fbaa57210a882aa4fdb6124aa /Utility/OptParse.hs
parent18067656af26fea271a0b75246cbf71af9c72114 (diff)
make sync --no-content be accepted
It's the default, but this is a step toward changing that default later..
Diffstat (limited to 'Utility/OptParse.hs')
-rw-r--r--Utility/OptParse.hs45
1 files changed, 45 insertions, 0 deletions
diff --git a/Utility/OptParse.hs b/Utility/OptParse.hs
new file mode 100644
index 000000000..f58e8fadf
--- /dev/null
+++ b/Utility/OptParse.hs
@@ -0,0 +1,45 @@
+{- optparse-applicative additions
+ -
+ - Copyright 2015 Joey Hess <id@joeyh.name>
+ -
+ - 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