How to make `switch` parse successfully only when the flag is passed?
dariooddenino opened this issue · comments
Hello,
not sure if the title is correct, but I'll explain my use case:
I want to be able to pass a --version
to show the current version, so I had this (not working) implementaiton:
parser =
SomeCommand1 <$ T.subcommand ...
T.<|> SomeCommand2 <$ T.subcommand...
T.<|> Version <$ T.switch "version" 'v' "print the version"
The problem is that Version
always parse successfully, so, for example, when I run my script without any command instead of the help message I get the version.
What's the correct way to handle this?
Thanks a lot!
@dariooddenino: One solution is to restructure the options like this:
data Options = Options { command :: Command, version :: Bool }
parser = Options <$> commands <*> switch "version" 'v' "print the version"
commands = SomeCommand1 <$ T.subcommand ... <|> ...
Another option is to use the lower-level optparse-applicative
library and use the flag'
utility, which fails to parse if you don't supply the flag:
Thanks! I went with the second approach :)
You're welcome! 🙂