问题描述
假设我在 git-cc
中有一个 PATH
可执行文件。如果 git-cc
支持 --help
,那么很容易提供足够的补全
complete -F _longopt git-cc
这使得 $ git-cc --<TAB>
完整(根据帮助输出)。但是 git cc --<TAB>
不会完成(即使它运行良好)。更重要的是,如果我为自定义子命令创建一个 git 别名,例如cc-sensible-defaults = cc --opt1 ...
,这也不起作用,在这种情况下,简单地删除空格(git-cc
而不是 git cc
)不是一种选择。
怎么办?我试过搞乱 __git_complete [git-]cc _longopt
,但各种组合都没有任何好处。它似乎是为了完成 bash 别名(如 gl = git-log
),而不是子命令。正如预期的那样,git/git-completion.bash 中的介绍不是很有帮助,包含令人困惑的
# If you have a command that is not part of git,but you would still
# like completion,you can use __git_complete:
#
# __git_complete gl git_log
#
# Or if it's a main command (i.e. git or gitk):
#
# __git_complete gk gitk
(WTH 是 _git_log?他们的意思是 _git_log,这确实是一个函数?是某种约定吗?)
解决方法
怎么办?
只需定义一个带有前导 _git_
前缀进行编译的函数。
# you should rather use COMPREPLY+=(..) or call `__gitcomp` to append
$ _git_cc() { COMPREPLY=(-a -b); }
$ git cc <tab>
-a -b
$ git cc -
__git_complete_command () {
local command="$1"
local completion_func="_git_${command//-/_}"
...
if __git_have_func $completion_func
then
$completion_func
return 0
我尝试过 __git_complete
据我所知,__git_complete
正好相反 - 您需要一个像 git subcommand 这样的普通命令。例如:
$ _git_cc() { COMPREPLY=(-a -b); }
$ alias mycommand='git cc'
$ __git_complete mycommand git_cc # or __git_complete mycommand _git_cc
$ mycommand <tab>
-a -b
$ mycommand -
WTH 是 _git_log?
_git_log
是一个为 git log
生成补全的函数。
他们是说_git_log,这确实是一个函数吗?
是的。请参阅 __git_complete 测试以了解是否存在带有 _main
后缀或 _
前缀或不带任何前缀/后缀的函数。
这是某种约定吗?)
是的。