为什么我的 bash 函数无法解析可选参数?

Why my bash function can't parse optional argument?

Each long option name in longopts may be followed by one colon to indicate it has a required argument, and by two colons to indicate it has an optional argument.

man bash.

我写了一个bash函数optionarg:

optionarg (){
    opts=$(getopt -o , --long name:,pass:: -- "$@")
    eval set -- "$opts"
    while true; do
        case "" in
            --name)
                name=
                echo  "name is " $name
                shift 2;;
            --pass)
                case "" in 
                    '')
                        echo "no pass argument"
                        shift 2;; 
                    *)
                        pass=
                        echo  "pass" $pass
                        shift 2;;
                esac;;               
            --)
                shift 1;;
            *)  break;;
        esac
    done }

在这里测试我的选项参数。
1.Assign 与 pass 没有争论。

optionarg --name tom --pass
name is  tom
no pass argument

2.Assign 参数 xxxxpass

optionarg --name tom --pass xxxx
name is  tom
no pass argument

如何修复我的 optionarg 函数以获得这样的输出?

optionarg --name tom --pass xxxx
name is  tom
pass xxxx

长选项使用=分配可选值:

$ optionarg --name tom --pass=xxxx
name is  tom
pass xxxx

这在 man getopt 中有记录:

If the option has an optional argument, it must be written directly after the long option name, separated by =, if present (if you add the = but nothing behind it, it is interpreted as if no argument was present; this is a slight bug, see the BUGS).

就我个人而言,我只会使用 case

optionarg () {
    while [[ $@ ]]; do
        case "" in
            --name|-n) name=;;
            --pass|-p) pass=;;
        esac               
        shift
    done
    echo "name=$name"
    echo "pass=$pass"
}

用法

$ optionarg --name user --pass dfgd
name=user
pass=dfgd

$ optionarg -n user -p dfgd
name=user
pass=dfgd