[英]Creating a tab completion script. In one case I want to use the completion for another command (e.g. git), is it possible?
So I have some commands I am scripting up: 所以我有一些我正在编写脚本的命令:
all-build.sh all-git.sh all-build.sh all-git.sh
These run commands on multiple folders and spawn output windows (running tail -f on logs) and all sorts of other things. 这些命令运行在多个文件夹和生成输出窗口(在日志上运行tail -f)和各种其他事情。
However I they each take different commands and I want tab completion 但是,他们每个人都采取不同的命令,我希望标签完成
So I read a tutorial on tab completion and I have the all-completion.bash script as shown here: 所以我阅读了关于制表符完成的教程,我有all-completion.bash脚本,如下所示:
#!/usr/bin/env bash
complete -W "param1 param2 param3" all-build.sh
complete -W "status log push pull" all-git.sh
As you can see the all-build auto complete params are my own custom ones and I am happy with that. 正如你所看到的那样,所有构建的自动完成参数都是我自己的定制参数,我很满意。 However the git ones are a repeat of some of the actual git parameters.
然而,git是重复的一些实际的git参数。 So really what I want is something like:
所以我真正想要的是:
complete <use git completion> all-git.sh
So that when I do all-git submodule upd<tab>
it auto completes to all-git submodule update
using git's auto complete. 因此,当我执行
all-git submodule upd<tab>
它会使用git的auto complete自动完成all-git submodule update
。
Is that possible? 那可能吗?
We might use _completion_loader
, (which is used to define completions on the fly ) in order to load git
completions. 我们可能会使用
_completion_loader
,(这是用来定义在飞行的完成)以加载git
落成。
Then we might observe in official git-completion.sh file, that function used to complete git "submodules" is called _git
, so we can simply pass it to complete
via -F <function>
option, see complete documentation . 然后我们可以在官方的git-completion.sh文件中观察到,用于完成git“子模块”的函数叫做
_git
,所以我们可以简单地传递它来complete
via -F <function>
选项,参见完整的文档 。
So by writing 所以通过写作
source /usr/share/bash-completion/bash_completion
_completion_loader git
complete -F _git all-git.sh
we can achieve desired behaviour (if I got your issue correctly). 我们可以实现理想的行为(如果我正确地解决了您的问题)。
Note : That bash_completion
script, might be located on your machine in different folder. 注意 :
bash_completion
脚本可能位于您的计算机上不同的文件夹中。 I've tested it on Ubuntu 16.04 with bash 4.3.48 . 我在Ubuntu 16.04上用bash 4.3.48测试了它。
EDIT : In case this doesn't fully solve your issue, I believe this is the way you want to go :). 编辑 :如果这不能完全解决您的问题,我相信这是你想要的方式:)。 You can pretty much bend the solution for your needs.
您可以根据需要改进解决方案。
EDIT-2 : I've also assumed, you have already installed and working git completions. 编辑-2 :我也假设,你已经安装并运行git完成。
EDIT-3 : I've found a few interesting links, which are not solving your particular issue, but can give some light and deeper knowledge: 编辑3 :我发现了一些有趣的链接,这些链接并没有解决您的特定问题,但可以提供一些更轻松,更深入的知识:
It's very simple: 这很简单:
source /usr/share/bash-completion/completions/git
__git_complete all-git.sh __git_main
The __git_complete()
function is a helper to create Git completions, it's used by Git itself like this: __git_complete git __git_main
. __git_complete()
函数是创建Git完成的辅助函数,它由Git本身使用,如下所示: __git_complete git __git_main
。 You can use it for specific commands, like __git_complete all-submodule.sh _git_submodule
. 您可以将它用于特定命令,例如
__git_complete all-submodule.sh _git_submodule
。
It's specifically designed for this, so no need to complicate it more. 它是专门为此而设计的,因此无需进一步复杂化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.