getopts optstring var [arg ...]
    The POSIX getopts command, not to be confused with the Bell
    Labs-derived getopt(1).

    The first argument should be a series of letters, each of which
    may be optionally followed by a colon to indicate that the option
    requires an argument.  The variable specified is set to the parsed
    option.

    The getopts command deprecates the older getopt(1) utility due to
    its handling of arguments containing whitespace.

    The getopts builtin may be used to obtain options and their
    arguments from a list of parameters.  When invoked, getopts places
    the value of the next option from the option string in the list in
    the shell variable specified by var and its index in the shell
    variable OPTIND.  When the shell is invoked, OPTIND is initialized
    to 1.  For each option that requires an argument, the getopts
    builtin will place it in the shell variable OPTARG.  If an option
    is not allowed for in the optstring, then OPTARG will be unset.

    By default, the variables $1, ..., $n are inspected; if args are
    specified, they'll be parsed instead.

    optstring is a string of recognized option letters (see
    getopt(3)).  If a letter is followed by a colon, the option is
    expected to have an argument which may or may not be separated
    from it by white space.  If an option character is not found where
    expected, getopts will set the variable var to a “?”; getopts will
    then unset OPTARG and write output to standard error.  By
    specifying a colon as the first character of optstring all errors
    will be ignored.

    After the last option getopts will return a non-zero value and set
    var to “?”.

    The following code fragment shows how one might process the
    arguments for a command that can take the options [a] and [b], and
    the option [c], which requires an argument.

          while getopts abc: f; do
                  case $f in
                  a | b)  flag=$f;;
                  c)      carg=$OPTARG;;
                  \?)     echo $USAGE; exit 1;;
                  esac
          done
          shift "$((OPTIND - 1))"

    This code will accept any of the following as equivalent:

          cmd -acarg file file
          cmd -a -c arg file file
          cmd -carg -a file file
          cmd -a -carg -- file file
