写入 __fish_is_first_arg,但包括 arg 的参数

Writing __fish_is_first_arg, but includes the arg's parameter

我正在为 cwebp、Google 的到 webp 转换器编写一个完成文件。它的帮助说 -preset 应该出现在所有其他参数之前。考虑到这一点,我尝试使用 __fish_is_first_arg 来限制其可用性,如下所示:

complete -c cwebp -x -n '__fish_is_first_arg' -o preset -a 'default photo picture drawing icon text' -d 'Preset setting'

这将使 cwebp -o -pres<Tab> 不会 建议 -preset,这正是我想要的。

与此同时,cwebp -pres<Tab> 会完整地填写参数 -preset,这也是我想要的。

然而,当我在cwebp -preset <Tab>处按Tab键时,给出的唯一建议是当前目录中的文件和目录。这不是我想要的。


考虑到这一点,我认为我必须编写一个 "is first or second option" 函数。然而,它并不顺利。这是我目前所拥有的:

function __fish_cwebp_is_first_option_or_its_argument
    set -l tokens (commandline -co)
    # line alpha
    switch (count tokens)
        case 1
            return 0
        case 2
            if test \( "$tokens[2]" = '-preset' \)
                return 0
            end
            return 1
        case '*'
            # line beta
            breakpoint
            return 1
    end
end

据我所知,这个函数体的工作方式与 return 0 ((true)) 相同。无论如何,-pres<Tab> 完成为 -preset,即使该行看起来像 cwebp -h -H -version -pres<Tab>.

当我在 alpha 行放置一个断点时,我可以 echo $tokens 并看到我完全输入的所有标记(最后一个标记之间至少需要一个 space和光标)。但是,当我在 beta 线上只有一个断点时,我什至无法触发断点。即使使用上面提到的 cwebp -h -H -version -pres<Tab> 也不行。

我做错了什么?

switch (count tokens)

应该是:

switch (count $tokens)

(对于阅读本文的其他人:$ 激活变量扩展。count $tokens 扩展变量 tokens 并计算其值,而 count tokens 仅计算单个文字 "tokens").