Structopt: EITHER -a OR (-b AND -c) ... with defaults

I have a CLI written in structopt. It should provide different ways of specifying a thingy:

  • The default thingy
  • A thingy explicitly defined on the command line
  • The first thingy found in the given file
  • The nth thingy found in the given file

This leads to the following use cases:

prog              # default thingy
prog -t abc       # parse abc into thingy
prog -f file      # first thingy in file
prog -f file -n 3 # 3rd thingy in file
prog -n 3         # ERROR: -n requires -f
prog -t abc -f f  # ERROR: -t and -f conflict
prog -t abc -n 3  # ERROR: -t and -n conflict / -n requires -f

Can you suggest a clean way to express these relationships in structopt?

structopt is built on clap, and I know that clap-3.0 can do what you're asking. In clap at least it's possible to define an exclusive group (i.e. only one flag from the group can be given) and at the same time link two flags together such that one expects the other.

So -t and -f would be group = "thingy" while -f expects/requires -n. Check out the structopt docs specifically and look for the terms "group" and "requires".

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.