ksh function getopts get leading underscore unless preceded by hyphen

If I call my function with grouped options: "logm -TDIWEFO 'message' ", then only the "T" gets parsed correctly. The subsequent values returned have underscores prefixed to the value: "_D", "_I", etc. If I "logm -T -DIWEFO 'message' ", the "T" and the "D" are OK, but "I" through "O" get the underscores.

Can someone, please, point out what I'm doing wrong.

Here's the function (with debugging "echo"s included):

function logm
{
echo inside logm
#OPTSTRING=":tTdDiIwWeEfFoO"
#while getopts ${OPTSTRING} opt
while getopts ":tTdDiIwWeEfFoO" opt
        do
echo OPTIND=${OPTIND}
echo OPTARG=${OPTARG}
echo opt=$opt
echo 1=$1
echo 2=$2
echo 3=$3
echo 4=$4
                case ${opt} in
                        t|T)
                          echo log TRACE ${2}
                          log TRACE ${2}
                          ;;
                        d)
                          echo log DEBUG ${2}
                          log DEBUG ${2}
                          ;;
                        D)
                          echo log DEBUG ${2}
                          log DEBUG ${2}
                          ;;
                        i|I)
                          echo log INFO ${2}
                          log INFO ${2}
                          ;;
                        w|W)
                          echo log WARN ${2}
                          log WARN ${2}
                          ;;
                        e|E)
                          echo log ERROR ${2}
                          log ERROR ${2}
                          ;;
                        f|F)
                          echo log FATAL ${2}
                          log FATAL ${2}
                          ;;
                        o|O)
                          echo log OFF ${2}
                          log OFF ${2}
                          ;;
                        *)
                         echo unknown opt $opt
                         ;;
                esac
        done
unset OPTSTRING
unset OPTIND
}

Here's how I call it:

logm -TDIWEFO "this is the tdiwefo message"

Here're the results:

inside logm
OPTIND=1
OPTARG=
opt=T
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
log TRACE this is the tdiwefo message
OPTIND=1
OPTARG=
opt=_D
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _D
OPTIND=1
OPTARG=
opt=_I
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _I
OPTIND=1
OPTARG=
opt=_W
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _W
OPTIND=1
OPTARG=
opt=_E
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _E
OPTIND=1
OPTARG=
opt=_F
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _F
OPTIND=2
OPTARG=
opt=_O
1=-TDIWEFO
2=this is the tdiwefo message
3=
4=
unknown opt _O

Either use positional parameters ($1 , $2 etc.) or "getopts" but not both.

We need to introduce a parameter to contain the message. For example "m".

while getopts ":tTdDiIwWeEfFoOm:M:" opt

And in the case statement

m|M)
       MESSAGE_TEXT="${OPTARG}"
       ;;

We can then call the script as:

logm -TDIWEFO -m "this is the tdiwefo message"

However this creates a logic problem in the script where the message gets processed after the "log" command. So in the case statement we need to just store the error message category e.g. "WARN". then issue the "log" command after the end of the case statement.

Thanks for the suggestion, but that won't do what I need: it would only process the last option (tdiwefo) specified before the -m. I want to 'log' for each message category specified in the -tdiwefo option.

What I really want to do is:

function logm
{
for mc in $(some_command_to_separate $1 into letters)
do
  log $mc $2
done
}

where $1 is some combination of the letters t, d, i, w, e, f, o.

I thought I could use "getopts" instead of "$(some_command_to_separate $1 into letters)".

Anyway ... any idea about the mysterious underscores in my original message (or an elegant way to "$(some_command_to_separate $1 into letters)" )?