如何使用`cat`将文件的内容传递给`_values`(zsh完成) [英] How to pass the contents of a file using `cat` to `_values` (zsh completion)
问题描述
是否可以使用cat
将文件的内容传递给_values
(zsh完成)?
Is it possible to pass the contents of a file using cat
to _values
(zsh completion)?
如果我取消注释行_values cat .test_tasks~
并在下面注释_values
,则行不通,我得到:_values:compvalues:10: invalid value definition: 1test[1
.
If I uncomment the line _values cat .test_tasks~
and comment _values
below, it doesn't work, I get: _values:compvalues:10: invalid value definition: 1test[1
.
#compdef test
#autoload
local curcontext="$curcontext" state line ret=1
local -a _configs
_arguments -C \
'1: :->cmds' \
'2:: :->args' && ret=0
_test_tasks() {
# _values "test" $(cat .test_tasks~)
_values "test" \
"1test[1 test test]" \
"2test[2 test test]"
}
case $state in
cmds)
_test_tasks
ret=0
;;
args)
_test_tasks
ret=0
;;
esac
return ret
.test_tasks〜
1test [1测试测试]
2test [2测试测试]
1test[1 test test]
2test[2 test test]
这似乎是空格的问题.如果我删除空格:
It seems to be a problem with whitespaces. If I remove the whitespaces:
1test [1testtest]
2test [2testtest]
1test[1testtest]
2test[2testtest]
有效.
有什么想法吗?
OLD_IFS=$IFS
IFS=$'\n'
_values $(< .cap_tasks~)
IFS=$OLD_IFS
参考: Bash cat命令空间问题已解释.
推荐答案
您可以使用mapfile
模块:
zmodload zsh/mapfile
_values ${(f)mapfile[.test_tasks~]}
mapfile
关联数组提供对外部文件内容的访问.参数扩展标志(f)
在换行符处拆分结果扩展,以便文件的每一行将形成_values
的单独参数.
The mapfile
associative array provides access to the contents of external files. The parameter expansion flag (f)
splits the resulting expansion on newlines, so that each line of the file will form a separate argument to _values
.
这篇关于如何使用`cat`将文件的内容传递给`_values`(zsh完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!