I want getopts
to recognize when a user passes "-?
" and "-h
" and send to my help function.
I also don't want getopts
to send the user to the help function if an invalid option is presented. I want a different message to display telling them it was an invalid option and send to stderr.
Here is my options section:
options(){
# grab options flags before entering application:
while getopts ":h?vu:d" opts; do
case "${opts}"
in
h\?) # Help
help_section
exit 0
;;
v) # Version
version
exit 0
;;
u) # units
other_units="$OPTARG"
;;
d) # Debug
debug=1
;;
\?) # Invalid options
echo "no here"
>&2 echo "[!] ERROR: Invalid Option: -$OPTARG"
exit 1
;;
esac
done
shift $((OPTIND -1))
# Run main function
main_run "$@"
}
Problem: getopts
keeps sending the user to help when they put an invalid option in. I need to make sure the user recognizes they provided an invalid parameter; I do not want to send them to help.
Is there a way to implement this with getopts
? Or build logic outside of getopts
to capture and perform what I need?