Warm tip: This article is reproduced from serverfault.com, please click

其他-Zsh依赖完成,参数中有空格

(其他 - Zsh dependent completion with space in arguments)

发布于 2020-11-17 17:49:36

我正在尝试实现一个函数的完成,其中第二个参数的完成取决于第一个参数。

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
Questioner
Halil Özgür
Viewed
0
Marlon Richert 2020-11-28 22:38:29

你可以\通过用Q参数扩展标志“取消引号”来摆脱

${(Q)words[2]}

现在bar\ baz'bar baz'在命令行上都将在你的代码中产生相同的字符串:bar baz