We capture the commandline tokens using fish's "commandline --tokenize" (-o).
Given a commandline of
some-cobra-command 'some argument $(123)' "$HOME"
into three arguments while removing the quotes
some-cobra-command
some argument $(123)
$HOME
Later we pass "some argument $(123)" without quotes to the shell's
"eval". This is wrong and causes spurious evaluation of the parenthesis
as command substitution.
Fix this by escaping the arguments.
The upcoming fish 3.8 has a new flag, "commandline -x"
which will expand variables like $HOME properly, see
https://github.com/fish-shell/fish-shell/pull/10212 Use that if
available.
Reproduce the issue by pasting the completion script at
https://github.com/fish-shell/fish-shell/issues/10194#issuecomment-1879563545
to a file "grafana-manager.fish" and running
function grafana-manager; end
source grafana-manager.fish
Then type (without pressing Enter)
grafana-manager public-dashboards delete --organization-id 3 --dashboard-name "k8s (public)" <TAB>
Fixes https://github.com/fish-shell/fish-shell/issues/10194
* Fix flag completion
The flag completion functions should not be stored in the root cmd.
There is no requirement that the root cmd should be the same when
`RegisterFlagCompletionFunc` was called. Storing the flags there does
not work when you add the the flags to your cmd struct before you add the
cmd to the parent/root cmd. The flags can no longer be found in the rigth
place when the completion command is called and thus the flag completion
does not work.
Also #1423 claims that this would be thread safe but we still have a map
which will fail when accessed concurrently. To truly fix this issue use a
RWMutex.
Fixes#1437Fixes#1320
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
* Fix trailing whitespaces in fish comp scripts
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
* Fix fish for ShellDirectiveNoSpace and file comp
For fish shell we achieve ShellDirectiveNoSpace by outputing a fake
second completion with an extra character. However, this extra
character was being added after the description string, instead of
before. This commit fixes that.
It also cleans up the script of useless code, now that fish completion
details are better understood.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Handle case when completion starts with a space
Fixes#1303
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Support fish completion with env vars in the path
Fixes https://github.com/spf13/cobra/issues/1214
Fixes https://github.com/spf13/cobra/issues/1306
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* Update based on review
1- We use `set -l` for local variable to make sure there are no
conflicts with global variables
2- We use `commandline -opc` which:
a) splits the command line into tokens (-o)
b) only considers the current command (-p) (e.g., echo hello; helm <TAB>)
c) stops at the cursor (-c)
3- We extract the last arg with `commandline -ct` and escape it to handle
the case where it is a space, or unmatched quote.
4- We avoid looping when filtering on prefix.
5- We don't add a fake comp for ShellCompDirectiveNoSpace when the
completion ends with any of @=/:., as fish won't add a space
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Some programs may output extra empty lines after the directive.
Those lines must be ignored for fish shell completion to work.
zsh and bash are not impacted.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
Replace the current Zsh completion with a Zsh completion solution based
on Go completions. This allows to support custom completions (based
on Go completions), but also to standardize the behavior of completion
across all shells.
Also, add support to Go completions for the bash completion annotations:
BashCompFilenameExt (including Command.MarkFlagFilename() family)
- still supported by zsh
BashCompSubdirsInDir - now supported by zsh
BashCompOneRequiredFlag (including Command.MarkFlagRequired() family)
- now supported by zsh and fish
Finally, remove the suggestin of the = form of flag completion.
The = form is supported, but it will not be suggested to avoid having
duplicated suggestions.