#!/bin/sh

c=1
while getopts M:nv myflag ; do
  echo "called getopts ${c}th time"
  echo "\$myflag = $myflag"
  echo "\$OPTIND = $OPTIND"
  echo "\$OPTARG = '$OPTARG'"
  echo
  c=$((c+1))
done

echo "called getopts ${c}th time"
echo "Final \$myflag = $myflag"
echo "Final \$OPTIND = $OPTIND"
echo "Final \$OPTARG = '$OPTARG'"


# OPTIND has two interpretations:
#   1 + where getopts stopped
#   where getopts would resume next time
# Therefore, can restart from beginning by resetting OPTIND.
# "Here we go again":

echo
echo Reprise:
echo

OPTIND=1
c=1
while getopts M:nv myflag ; do
  echo "called getopts ${c}th time"
  echo "\$myflag = $myflag"
  echo "\$OPTIND = $OPTIND"
  echo "\$OPTARG = '$OPTARG'"
  echo
  c=$((c+1))
done

echo "called getopts ${c}th time"
echo "Final \$myflag = $myflag"
echo "Final \$OPTIND = $OPTIND"
echo "Final \$OPTARG = '$OPTARG'"
