cobra icon indicating copy to clipboard operation
cobra copied to clipboard

command line args should be quoted in generated fish completion script

Open ryenus opened this issue 2 years ago • 3 comments

more specifically here in fish_completions.go:56:

set -l requestComp "%[10]s=0 $args[1] %[3]s $args[2..-1] $lastArg"

where the references $args[1], $args[2..-1] and $lastArg should have been quoted.

Downstream issue: https://github.com/mikefarah/yq/issues/1502

ryenus avatar Mar 18 '23 09:03 ryenus

An easy fix would be just quote the referenced args:

set -l requestComp "%[9]s=0 '$args[1]' %[3]s '$args[2..-1]' '$lastArg'"

But I'm not sure what would happen if $args[2..-1] includes more than one list items, hopefully there's something equivalent to quoted array expansion like "${array[@]}" in bash.

ryenus avatar Mar 18 '23 10:03 ryenus

I stumbled upon the same issue. Regarding quoting $args[2..-1], the following works:

set -l requestComp "%[10]s=0 '$args[1]' %[3]s $(string join ' ' (string escape $args[2..-1])) '$lastArg'"

%[10]s=0 is just the value from the current main branch.

As a workaround, without having to redefine the whole completion command, it's possible to intercept the completion fish command via PersistentPreRunE on the root command, generate the completions manually and write them into a buffer via rootCmd.GenFishCompletion(...), and then just replace the lines and output them.

n9v9 avatar Jun 10 '25 12:06 n9v9

It seems to me this old issue is still there.

I'm glad @n9v9 took the time to look at it, and provide an update and feedbacks here.

Thanks

Do you think you could open a PR ? I feel like you are close to a fix, no?

ccoVeille avatar Jun 10 '25 18:06 ccoVeille