2

假设我git-ccPATH. 如果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,这确实是一个函数吗?是某种约定吗?)

4

1 回答 1

4

该怎么办?

只需定义一个使用前导_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

__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 子命令。例如:

$ _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后缀或_前缀或没有任何前缀/后缀的函数。

这是某种约定吗?)

是的。

于 2021-04-07T13:49:02.050 回答