optparse-applicative require at least one of two switches - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

J C

Hello, is there some way to require at least one of two switches, when using optparse-applicative? So all of:

  • myexe -a
  • myexe -b
  • myexe -ab or myexe -ba

should work, but not just myexe without any options.

Torsten Schmits

you could do onlyA <|> onlyB <|> both where each of those contain one or both of the individual Parsers

J C

Torsten Schmits said:

you could do onlyA <|> onlyB <|> both where each of those contain one or both of the individual Parsers

Really? I'm not getting that to work. Seems like the parser doesn't have backtracking, so I think it would be difficult: https://github.com/pcapriotti/optparse-applicative/issues/404

Torsten Schmits

guess you'll have to throw an error yourself

Georgi Lyubenov // googleson78

it might be the case that they're more suited to be sub-commands?

J C

Georgi Lyubenov // googleson78 said:

what are -a and -b actually?

It's for a small tool to create initial Cabal/Nix project setups, so the switches are supposed to be translated to --lib, --exe, and --libandexe.

Georgi Lyubenov // googleson78

so how about --create which accepts lib or exe or both? (translating to a data Mode = Lib | Exe | Both)

Georgi Lyubenov // googleson78

you can easily have a default value too, that way