Is there any way to retrieve a list of the options in a case statement? For example if I have this code:
tool=$1
case ${tool} in
brdf)
# Do stuff
;;
drift)
# Do other stuff
;;
*)
echo "ERROR: I don't know this tool. Valid options are: brdf, drift"
exit 1
;;
esac
This is easy to read, but the error message could easily get out of date when adding/removing tools from the list as I need to remember to change the names there too.
The repetition could be avoided using an array something like this:
tool=$1
validtools=(brdf drift)
case ${tool} in
${validtools[0]})
# Do stuff
;;
${validtools[1]})
# Do other stuff
;;
*)
echo "ERROR: I don't know this tool. Valid options are: ${validtools[@]}"
exit 1
;;
esac
But that is pretty horrible to read, and in any case would be even worse to maintain with the hardcoded array indices.
Is there a good way of doing this, perhaps some variable or command that retrieves a list of the available options, or do I just have to remember to update the error message when I add a new option?