多级 Bash 完成
我目前有一个 Bash 完成文件,它从名为 pbt
的脚本的允许命令列表中完成单个参数。这是工作的 Bash 完成文件:
_pbt_complete()
{
local cur goals
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
goals='asadmin clean deploy'
cur=`echo $cur`
COMPREPLY=($(compgen -W "${goals}" ${cur}))
}
complete -F _pbt_complete pbt
因此,如果我调用
pbt <tab>
Bash 完成所有允许的命令(asadmin
、clean
、deploy
),这是可以的。 现在我想添加一个第二级来完成。例如,如果我输入,
pbt asadmin <tab>
我希望它只完成asadmin“环境”(我还将在 Bash 完成文件中定义)。例如
pbt asadmin [启动域|停止域]
。但如果我输入
pbt deploy <tab>
It 应该完成另一组选项。例如,pbt 部署 [all|current]
。因此第二个命令的选项应始终取决于第一个命令。
我怎样才能在完成文件中做到这一点?
I currently have a Bash completion file which completes a single parameter from a list of allowed commands for a script called pbt
. This is the working Bash completion file:
_pbt_complete()
{
local cur goals
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
goals='asadmin clean deploy'
cur=`echo $cur`
COMPREPLY=($(compgen -W "${goals}" ${cur}))
}
complete -F _pbt_complete pbt
So if I call
pbt <tab>
Bash completes to all allowed commands (asadmin
, clean
, deploy
), which is okay. Now I want to add a second level to the completion. So for example if I type
pbt asadmin <tab>
I want it to complete only options that are available inside the asadmin
"environment" (which I'll also define inside the Bash completion file). For example pbt asadmin [start-domain|stop-domain]
. But if I type
pbt deploy <tab>
It should complete to another set of options. For example, pbt deploy [all|current]
. So the options for the second command should always depend on the first command.
How can I do that in the completion file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢mkb的评论,我研究了
p4
示例,与 Git 示例不同——简单到足以让我适应我的情况。这是工作版本,它完全符合我的要求:Thanks to mkb's comment, I looked into the
p4
example, which was—unlike the Git example—simple enough for me to adapt to my case. Here is the working version which does exactly what I asked for:顺便说一句,这里有两个关于更好的 Tab 补全的有用技巧:
bind 'set show-all-if-ambigously on'
# 这使得只需要一个 Tab 来显示可能的补全以下是有关如何使用
complete
命令调整选项卡补全的更多信息 。此页面有令人难以置信的细分,并讨论使用“cobra (Golang)、click (Python) 和 clap (Rust) 工具”的已知应用程序https://echorand.me/posts/linux_shell_autocompletion/ (以及 Git 如何使用 Compgen)
这里是Debian
bash-completion
的 GH 存储库链接:https://github .com/scop/bash-completion问:每个目标命令的完成文件的搜索顺序是什么?
A. 命令的补全文件由shell函数__load_completion查找。这里,解释 bash-completion >= 2.12 中的搜索顺序。
依次在上述补全目录中查找name或.bash的补全文件,其中是目标命令的名称。使用最先找到的文件。当本过程中任何补全目录都没有找到补全文件时,接下来依次在补全目录中查找名称为_的补全文件。
Btw, here are two useful tips on better tab completion:
bind 'set show-all-if-ambiguous on'
# this makes only one Tab necessary to show possible completionsHere is more info on how to use the
complete
command to adjust tab completetion. This page has an incredible breakdown, and discusses known application using "cobra (Golang), click (Python) and clap (Rust) tools"https://echorand.me/posts/linux_shell_autocompletion/ (and how Git uses Compgen)
Here is a link the the GH repo for Debian
bash-completion
: https://github.com/scop/bash-completionQ. What is the search order for the completion file of each target command?
A. The completion files of commands are looked up by the shell function __load_completion. Here, the search order in bash-completion >= 2.12 is explained.
The completion files of the name or .bash, where is the name of the target command, are searched in the above completion directories in order. The file that is found first is used. When no completion file is found in any completion directories in this process, the completion files of the name _ is next searched in the completion directories in order.
作为示例,您可能想看看 git 的补全是如何完成的。 (这在我的 bash 设置中需要 2257 行函数定义和额外的 14 个变量。)
You may want to look at how the completion for git is done, as an example. (This takes 2257 lines of function definitions and additional 14 variables in my bash setup.)