Support exactly-one mutually exclusive options
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 48/100
Direzione di ricerca
Start with the current oneOf recommendation from #111 and the parser combinators shown in this issue. Investigate where multiple alternatives become an unknown-flag error, then define support for exactly-one parsing so multiple selections produce a clear error during parsing rather than in main; done means the no-option, one-option, and multiple-option cases have useful messages.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Related: #111
I want to allow the user to specify exactly one of --modified-files, --staged-files, or --all-files with a nice error. If I follow the current recommendation in #111, I get the following behavior:
oneOf
[ switch (long "modified-files")
, switch (long "staged-files")
, switch (long "all-files")
]
oneOf = foldr (<|>) empty
$ example
Missing: (--modified-files | --staged-files | --all-files)
$ example --modified-files
Works
$ example --modified-files --staged-files
Unknown flag: --staged-files
This has the correct semantics, and the error message when no options are passed is pretty good. The only issue is that passing multiple flags shows an unhelpful error message, when clearly a good error message is possible.
My current workaround is:
oneOf :: [Opt.Parser a] -> Opt.Parser a
oneOf parsers = validate <$> traverse Opt.optional parsers
where
validate results =
case catMaybes results of
[a] -> a
_ -> error $ "Expected exactly one of: " <> (Text.intercalate ", " . map Text.pack) optNames
optNames = concatMap getOptNames parsers
getOptNames :: Opt.Parser x -> [String]
getOptNames = \case
Opt.Internal.NilP _ -> []
Opt.Internal.OptP opt -> [getOptName opt]
Opt.Internal.MultP p1 p2 -> getOptNames p1 <> getOptNames p2
Opt.Internal.AltP p1 p2 -> getOptNames p1 <> getOptNames p2
Opt.Internal.BindP p _ -> getOptNames p
getOptName opt =
case Opt.Internal.optMain opt of
Opt.Internal.OptReader names _ _ | Just name <- getName names -> name
Opt.Internal.FlagReader names _ | Just name <- getName names -> name
_ -> Opt.Internal.propMetaVar $ Opt.Internal.optProps opt
getName names
| n : _ <- [n | Opt.Internal.OptLong n <- names] = Just ("--" <> n)
| c : _ <- [c | Opt.Internal.OptShort c <- names] = Just ['-', c]
| otherwise = Nothing
but it's super hacky and only fails when the result is used in main, instead of during the Parser evaluation. It would be great to have something officially supported here.
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pcapriotti/optparse-applicative
-
Allow QuickCheck 2.17Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 75/100
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
pcapriotti/optparse-applicative#524 · 2 commenti ·
-
Allow replacing commands with metavar when using parserOptionGroupForse già presa @tbidne l’ha presa 86 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
pcapriotti/optparse-applicative#523 · 1 commento · 1 reazione ·
-
Nushell CompletionsAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
pcapriotti/optparse-applicative#495 · 2 commenti ·
Tutte le issue di pcapriotti/optparse-applicative
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
IntersectMBO/cardano-ledger#6276 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
OpenZeppelin/canton-contracts#92 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
hasura/graphql-engine#10954 ·
I maintainer di solito rispondono entro 1 giorno