访问bash的完成为特定的命令编程 [英] Accessing bash completions for specific commands programmatically
问题描述
我试图写一个小的命令启动应用程序,并想使用bash的标签补全在我自己完成的系统。我已经能够得到落成使用 compgen -abck
通用命令的列表。结果
不过,我也想获得完成为特定的命令:例如,输入 git的P
应该显示Git的命令完成。
I'm trying to write a small command launcher application, and would like to use bash's tab completions in my own completion system. I've been able to get a list of completions for general commands using compgen -abck
.
However, I would also like to get completions for specific commands: for instance, the input git p
should display completion for git's commands.
有什么办法,我可以使用 compgen
来做到这一点?如果没有,是否有任何其他的方式,我可以得到完整的列表编程?
Is there any way I can use compgen
to do this? If not, are there any other ways I can get a list of completions programmatically?
的 的
推荐答案
我真的不知道它是如何工作的,但是的真棒窗口管理器使用下面的Lua code获取访问bash补全的结果是:
I don't really know how it works, but the awesome window manager uses the following Lua code for getting access to bash completion's result:
的https://github.com/awesomeWM/awesome/blob/master/lib/awful/completion.lua#L119
- 通过
完整-p
我们发现完整-o bashdefault -o默认-o -F无空间混帐_git
。我们还记得_git供以后使用。 - 混帐L的长度是5,所以我们设置COMP_COUNT = 6。我们即将完成的第一个参数混帐,所以COMP_CWORD = 1。
- Via
complete -p
we findcomplete -o bashdefault -o default -o nospace -F _git git
. We remember "_git" for later. - The length of "git l" is 5, so we set COMP_COUNT=6. We are completing the first argument to "git", so COMP_CWORD=1.
总之,我们使用下面的脚本:
All together we use the following script:
__print_completions() {
printf '%s\n' "${COMPREPLY[@]}"
}
# load bash-completion functions
source /etc/bash_completion
# load git's completion function
_completion_loader git
COMP_WORDS=(git l)
COMP_LINE='git l'
COMP_POINT=6
COMP_CWORD=1
_git
__print_completions
输出:日志
这篇关于访问bash的完成为特定的命令编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!