easy --no-switch complement to --switch

Aperta
#148 6 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
haskell
Ambito
cli

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Haskell
Stelle
957
Fork
124
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pcapriotti/optparse-applicative

Tutte le issue di pcapriotti/optparse-applicative

Issue simili

Altre issue su Haskell

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.