Details in my SO answer: https://stackoverflow.com/questions/11517139/optional-option-argument-with-getopts/57295993#57295993
getopts_get_optional_argument() {
eval next_token=\${$OPTIND}
if [[ -n $next_token && $next_token != -* ]]; then
OPTIND=$((OPTIND + 1))
OPTARG=$next_token
else
OPTARG=""