这两种zsh完成方法有什么不同?

时间:2018-01-18 22:12:06

标签: zsh tab-completion

我创建了一个zsh完成脚本。如果我对选项列表进行硬编码,它会按预期工作,但如果我尝试使用bash命令生成相同的硬编码值,则我的结果是意外的。出于这个问题的目的,我将两种方法组合到同一个完成脚本中。 "傻"是一个奇怪的。 " silly2"表现得像预期的那样。

#compdef silly

_silly() {
    local -a options
    options=($(cat in_file.txt))
    _describe 'silly command' options && ret=0
    return ret
}

_silly2() {
    local -a options
    options=("one:Does the 1 thing" "two:Does the 2 thing")
    _describe 'silly2 command' options && ret=0
    return ret
}

compdef _silly silly
compdef _silly2 silly2

#contents of in_file.txt
#"one:Does the 1 thing" "two:Does the 2 thing"

正如你所看到的,第二种方法(硬编码)采用输入字符串"一种:是......"而第一种方法(动态)通过对文件in_file.txt的cat调用生成完全相同的输出,该文件具有相同的文本。

理想情况下,结果如下所示:

one  -- Does the 1 thing
two  -- Does the 2 thing

第二个确实产生了这个结果。然而,第一个产生如下结果:

"two  "one  -- Does
1                          2             the            thing"

我不能为我的生活弄清楚这里的区别是什么!

1 个答案:

答案 0 :(得分:0)

在这里,我正在回答我自己的问题。答案来自一个相关的问题:How to pass the contents of a file using `cat` to `_values` (zsh completion)

基本上,如果空格来自bash命令的结果而不是字符串文字,则对它们的处理方式不同。解决方案是在命令期间更改bash内部字段分隔符,然后将其更改回来。

OLD_IFS=$IFS
IFS=$'\n'
options=($(cat in_file.txt))
_describe 'silly command' options && ret=0
IFS=$OLD_IFS

这使得两个命令的行为方式相同。