重击完成从另一个完成 [英] Bash completion from another completion
问题描述
我有一个脚本,要求以命令名称和该命令的参数作为参数.
I have a script that requires as an argument the name of command and arguments of that command.
所以我想编写一个完成函数,该函数可以完成命令的名称并可以完成该命令的参数.
So I want to write a completion function that would complete the name of the command and would complete the arguments for that command.
所以我可以像这样完成命令的名称
So I can complete the name of the command like this
if [[ "$COMP_CWORD" == 1 ]]; then
COMPREPLY=( $( compgen -c ${COMP_WORDS[COMP_CWORD]} ))
else
#Don't know what to write here
fi
因此,这将完成可运行的Shell命令列表的第一个参数.在第二和其他论点上,我需要完成
${COMP_WORDS[COMP_CWORD]}
命令.
So this will complete the first argument to list of shell commands that are available to run. And on second and other arguments I need a completion for
${COMP_WORDS[COMP_CWORD]}
command.
我考虑过从COMP_WORDS
中删除第一个元素,将COMP_CWORD
减1并调用函数_${COMP_WORDS[0]}
名称带有命令前缀"_"的名称,因为在许多示例中,完成命令的功能都具有这样的名称,但是当我在bash中执行complete -p
时,我发现许多命令都是由具有不同名称的功能完成的.对于我来说,这种解决方案看起来真的很糟糕.
I thought about removing first element from COMP_WORDS
, decreasing COMP_CWORD
by one and call a function _${COMP_WORDS[0]}
with name that prefixes "_" to the command, because in many examples the function that completes a command has such name, but when I executed complete -p
in bash I found that many commands are completed with functions that has different names. And as for me, such solution looks really bad.
我不是bash脚本专家,所以我只是不知道从哪里开始寻找解决方案.
I'm not a bash scripting guru so I just don't know where to start searching the solution.
推荐答案
您的要求类似于exec
或time
或xargs
的命令完成.这些命令也采用命令&该命令的参数作为完成选项.
Your requirement is similar to the command completion for exec
or time
or xargs
. These commands also take a command & that command's arguments as completion options.
检查exec
的bash_completion选项:
Checking the bash_completion option for exec
:
$ complete -p exec
complete -F _command exec
您可以重复使用与完成功能相同的功能_command
.
You can re-use the same function _command
as your completion function..
用法:
complete -F _command your-script.sh
这篇关于重击完成从另一个完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!