Listing 2. Options Function

options()
{
# return values
typeset OPTIONS_WRONG_ARGS=1
# Initialisation
  typeset opts argname num
  options_unknown_option=
  options_missing_arg=
  options_shift_val=0
  options_num_args_left=0
  # ensure we have at least 2 arguments (getopts
  # spec and at least one other)
  [ $# -gt 1 ] || return $OPTIONS_NO_ARGS
  opts=":$1" # get the getopts option
             # argument and add a leading
  shift      # colon to stop getopts
  # printing error messages
  OPTERR=0
  OPTIND=1
  while getopts $opts argname
  do
    if [ "$argname" = "?" ]
    then # code handles bash getopts bug
      if echo $opts | grep $OPTARG >/dev/null 2>&1
      then     # option is really known so it must
        options_missing_arg=$OPTARG # have a
                          # missing arg
      else
        options_unknown_option=$OPTARG
      fi
      continue
    fi
    if [ "$argname" = ":" ]
    then
      options_missing_arg=$OPTARG
      continue
    fi
    if [ "$OPTARG" != "" ]
    then
      eval opt_$argname=$OPTARG  # set option name
                               # to value
    else
      eval opt_$argname=1   # set option name to 1
    fi
  done