1

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.

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
  • I'm not familiar with docopt or docopts, but I can see that the code posted will generate errors if pasted into the shell linting service at https://shellcheck.net . You'll need to include the appropriate `#!/bin/bash` as the first line. Good luck. – shellter Sep 23 '21 at 14:24
  • Also, try to make a simpler test case to prove to yourself that `docopts` is working the way you need it to work. Add in the `eval`, `$(...cmd < – shellter Sep 23 '21 at 14:28
  • And, I see that is a closing paren, not a curly brace, (as I originally thought), so you may not get any errors from shellcheck ;-)! . – shellter Sep 23 '21 at 14:30
  • @shellter for the sake of making the question not too long, I didn't include the shebang. Also this is already a simpler test case! You do not want to see the real code, this proof of concept is sufficient for considering the problem. :) – 2rs2ts Sep 23 '21 at 17:23

0 Answers0