pcapriotti / pcapriotti/optparse-applicative
easy --no-switch complement to --switch
Nobody has claimed this yet.
- Dominant language
- Haskell
- Stars
- 959
- Forks
- 123
- Avg merge
- 3d 19h
- Merged PRs (30d)
- 1
Description
A fairly common idiom in option parsing libraries is for --no-switch to be available as a way to disable an earlier --switch on the command line.
This is useful eg, when you have a shell alias foo = foo --switch, so foo --no-switch can be used to override . Another use case is to help future-proof a program; if it later changes so --switch is enabled by default, then users of both the old and the new version can use --no-switch to get the old behavior.
This is a bit clumsy to do with optparse-applicative, unless I'm missing an easy way to do it. Here's an implementation that makes it easy. I'd be happy if this or something like it were added to your library.
-- | 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
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no repository files or tests; start by locating the existing Parser, flag', long, many, and Mod FlagFields definitions in optparse-applicative. Compare their current behavior with the proposed invertableSwitch and invertableSwitch' entry points. Done means the library offers a documented --foo/--no-foo complement with the stated default and last-occurrence behavior, supported by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- cli
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100