我正在尝试实现一个函数的完成,其中第二个参数的完成取决于第一个参数。
function test_so() {
echo "$1" "$2"
}
function _test_so() {
local state
_arguments '1: :->arg1' '2: :->arg2'
case $state in
arg1) compadd foo 'bar baz' ;;
arg2)
echo " - first arg: ${words[2]} - "
if [[ ${words[2]} == 'bar baz' ]]; then
compadd bar-1 bar-2
else
compadd foo-1 foo-2
fi
;;
esac
}
compdef _test_so test_so
但是,似乎正在为带空格的参数传递文字\
&'
s ${words[2]}
:
$ test_so foo <tab> ... - first arg: foo -
foo-1
foo-2
$ test_so bar\ baz <tab> ... - first arg: bar\ baz - # <- Should be - first arg: bar baz -
foo-1
foo-2
# These should be bar-1 & bar-2
# Same thing for quotes
$ test_so 'bar baz' <tab> ... - first arg: 'bar baz' -
foo-1
foo-2