8ea5f3ff99
Eliminated some dead code. In other cases, exported a currently unused function, since it was a logical part of the API. Of course this improves the API documentation. It may also sometimes let ghc optimize code better, since it can know a function is internal to a module. 364 modules still to go, according to git grep -E 'module [A-Za-z.]+ where'
49 lines
1.4 KiB
Haskell
49 lines
1.4 KiB
Haskell
{- optparse-applicative additions
|
|
-
|
|
- Copyright 2015 Joey Hess <id@joeyh.name>
|
|
-
|
|
- License: BSD-2-clause
|
|
-}
|
|
|
|
module Utility.OptParse (
|
|
invertableSwitch,
|
|
invertableSwitch',
|
|
) where
|
|
|
|
import Options.Applicative
|
|
import Data.Monoid
|
|
import Prelude
|
|
|
|
-- | 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
|