-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
While working on adding descriptions to the custom completions for helm
, I ran into two bugs with zsh
and ShellCompDirectiveNoSpace
.
Say I have the following ValidArgsFunction
for command nospace
:
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return []string{"when\tThe time", "who\tThe person"}, cobra.ShellCompDirectiveNoSpace
}
return []string{"why\tThe reason"}, cobra.ShellCompDirectiveNoSpace
},
Bug 1: NoSpace is not respected
If I run in zsh
:
$ ./testprog nospace whe<TAB>
a space is added after ./testprog nospace when
even though the ShellCompDirectiveNoSpace
was given.
This is because the script only handles ShellCompDirectiveNoSpace
if a single completion is returned:
Line 215 in 02a0d2f
elif [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ] && [ ${compCount} -eq 1 ]; then |
But my ValidArgsFunction
does not filter the completion but instead returns all of them, leaving zsh
to filter them. In this case:
$ ./testprog __complete nospace whe<ENTER>
when The time
who The person
:2
Completion ended with directive: ShellCompDirectiveNoSpace
Notice that with the result above the completion script would receive two completions even though the who
completion is not technically valid. But this is allowed.
Bug 2: Descriptions are not handled with ShellCompDirectiveNoSpace
The zsh
script was using compadd
directly to handle ShellCompDirectiveNoSpace
:
Line 219 in 02a0d2f
compadd -S '' "${lastComp}" |
The problem is that compadd
does not handle descriptions and just prints them as part of the completion. So, in my case:
$ ./testprog nospace when w<TAB>
will complete to:
./testprog nospace when why:The\ reason
I will post a PR to handle this better.