I'm writing a completions file for cwebp, Google's to-webp converter. Its help says that -preset
should come before all other arguments. With that in mind, I tried restricting its availability with __fish_is_first_arg
, like this:
complete -c cwebp -x -n '__fish_is_first_arg' -o preset -a 'default photo picture drawing icon text' -d 'Preset setting'
This would make it so cwebp -o -pres<Tab>
would not suggest -preset
, which is what I wanted.
Meanwhile, cwebp -pres<Tab>
would fill out the argument to its full -preset
, which is also what I wanted.
However, when I press the Tab key at cwebp -preset <Tab>
, the only suggestions given are the files and directories in the current directory. This is not what I wanted.
With this in mind, I figured I had to write a "is first or second option" function. However, it's not going well. Here's what I have so far:
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
This function body, as far as I can tell, works the same way as return 0
((true)
). No matter what, -pres<Tab>
completes to -preset
, even when the line looks like cwebp -h -H -version -pres<Tab>
.
When I put a breakpoint on line alpha, I can echo $tokens
and see all the tokens that I've totally typed out (there needs to be at least one space between the last token and the cursor). However, when I have only a breakpoint on line beta as shown here, I can't even get the breakpoint to trigger. Not even with cwebp -h -H -version -pres<Tab>
as mentioned above.
What am I doing wrong?
switch (count tokens)
should be:
switch (count $tokens)
(For others reading this: the $ activates variable expansion. count $tokens
expands the variable tokens
and counts its values, while count tokens
counts only the single literal "tokens").