getopts fails to error on option w/o dash

I have a script with several options and during testing I found that the \? option does not handle options without dashes as I would expect. Then I run the script with any option that does not include a dash, it runs the script when I would expect \? to catch it and error.

I've tried this with OPTERR=0 and OPTERR=1 and don't get any errors. Do I need to write a function to catch options without dashes? Seems that would be something getopts should catch.

 
#!/usr/bin/bash
OPTERR=1
while getopts :P::E::I::L::b:ftdorvxhn optn
do
  case ${optn} in
    P)  echo "PORTX=${OPTARG}" ;;
    E)  echo "EXCLUDE_FILE=${OPTARG}" ;;
    I)  echo "IP_ARRAY=${OPTARG}" ;;
    L)  echo "IP_LIST=${OPTARG}" ;;
    b)  echo "Running function: ${OPTARG}" ;;
    f)  echo "pre_flight chk_preflight" ;;
    t)  echo "flar_size_total" ;;
    d)  echo "dupe_flar_chk" ;;
    o)  echo "old_flar_chk" ;;
    r)  echo "rotate_logs" ;;
    v)  echo "Not yet implemented." ;;
    x)  echo "get expl file" ;;
    h)  echo "USAGE 0; exit 0" ;;
    n)  echo "USAGE 1;exit 0" ;;
    \?)  echo "help or unknown" ;;
    :)  echo "ERROR: Option requires an arguement." ;;
  esac
done
 

Thanks,
HexKnot

All getopts does is look for options applied in the correct manor, ie with a leading dash, which matches the required patern given to getopts, what you're trying to achieve is expecting it to catch something which it isn't looking for.

Being a skeptic, I had to double check but you are correct. For the detail oriented, here's the explanation from Home | Open Source Initiative.

getopts

Specifically, this paragraph.

"When the end of options is encountered, the getopts utility shall exit with a return value greater than zero; the shell variable OPTIND shall be set to the index of the first non-option-argument, where the first "--" argument is considered to be an option-argument if there are no other non-option-arguments appearing before it, or the value "$#" +1 if there are no non-option-arguments; the name variable shall be set to the question-mark character. Any of the following shall identify the end of options: the special option "--", finding an argument that does not begin with a '-', or encountering an error."

Thanks