Docopt required flag where the value for it can only be one of a few options

247 views Asked by At

I am using the shell implementation of docopt, called docopts, to write a script. I want the script to have a flag argument, --myflag, where the value can only be one of a few valid choices: foo, bar, or baz. So, I tried this:

eval "$(docopts -h - : "$@" <<DOCOPTS
My program: do some stuff

Usage:
  myprogram --myflag foo
  myprogram --myflag bar
  myprogram --myflag baz

Options:
  -m --myflag=MYVALUE    This should only be foo, bar, or baz. 
DOCOPTS
)"

echo the value of myflag is: $myflag

But I do not get an error when I try running the program with a different value for --myflag:

$ myprogram --myflag dog
the value of myflag is: dog

I was expecting to see the usage message instead. Obviously, I could do the validation myself, but I was hoping docopt could handle that. What am I doing wrong?

Note: my use case seems different enough (different programming language, different argument structure) from this old question, so I don't think this is a duplicate.

0

There are 0 answers