We are no longer actively maintaining bash completion v1 in favor of its
more rich v2 version. Previously, using bash completion v2 required
projects to be aware of its existence and to explicitly call
GenBashCompletionV2().
With this commit, any projects calling GenBashCompletion() will
automatically be redirected to using the v2 version.
One exception is if the project uses the legacy custom completion logic
which is not supported in v2. We can detect that by looking for the
use of the field `BashCompletionFunction` on the root command.
Note that descriptions are kept off when calling GenBashCompletion().
This means that to enable completion descriptions for bash, a project
must still explicitly call GenBashCompletionV2().
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
For legacy bash completions, similarly as commit
4f0facbcee is for bash completions v2.
As a side effect, fixes test suite with shellcheck 0.8.0 installed;
apparently the 0.7.0 that's in GitHub Actions' ubuntu-latest at the
moment does not flag the array quoting related issue that was provoked
from 0.8.0 before this change.
when `set -o nounset` in Bash,
the warnings of unbound variables will break the bash completion.
use `kubectl` as example:
```sh
$ set -o nounset
$ my-cli <Tab>-bash: BASH_COMP_DEBUG_FILE: unbound variable
$
```
the warning break bash completion without any completion result,
and cause my cursor move to the newline.
Use `${variable:-}` substitution in Bash,
that assign an empty string as default for unbound variables to fix the warnings.
When a command has set DisableFlagParsing=true, it means Cobra should
not be handling flags for that command but should let the command
handle them itself. In fact, Cobra normally won't have been told at all
about flags for this command.
Not knowing about the flags of the command also implies that Cobra
cannot perform flag completion as it does not have the necessary info.
This commit still tries to do flag name completion, but when
DisableFlagParsing==true, it continues on so that ValidArgsFunction will
be called; this allows the program to handle completion for its own
flags when DisableFlagParsing==true.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
* 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>
* Complete subcommands when TraverseChildren is true in custom completion
The current custom completion logic does not complete
subcommands when a local flag is set. This is good unless
TraverseChildren is set to true where local flags
can be set on parent commands.
This commit allows subcommands to be completed
if TraverseChildren is set to true on the root cmd.
Closes#1170
Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
* Complete subcommands when TraverseChildren is true in bash completion
The current bash completion logic does not complete
subcommands when a local flag is set. There is also a bug
where subcommands are sometimes still getting completed. see: #1172
If TraverseChildren is true we should allow subcommands
to be completed even if a local flag is set.
Closes#1172
Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
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.
* Don't exclude 'help' from bash completions
Fixes#1000.
* Provide completion for the help command
1- Show 'help' as a possible completion
2- Provide completions for the help command itself
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
Co-authored-by: Zaven Muradyan <voithos@google.com>
Since the completion directives will be used for all shells, and that
these names will be consumed by users, this is a more appropriate name.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
This commit allows programs using Cobra to code their custom completions
in Go instead of Bash.
The new ValidArgsFunction field is added for commands, similarly to
ValidArgs. For flags, the new function
Command.RegisterFlagCompletionFunc() is added.
When either of the above functions is used, the bash completion script
will call the new hidden command '__complete', passing it all
command-line arguments. The '__complete' command will call
the function specified by Command.ValidArgsFunction or by
Command.RegisterFlagCompletionFunc to obtain completions from the
program itself.
Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
PR #889 introduced a regression where the global variable $c is no
longer set when *custom_func is called. This is because $c is re-used
by mistake in the read loop.
This PR simply changes the name of the variable used in the loop.
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
- Moved some general function to a more generic shell_completions file.
- Added functions to mark flag as directory completion.
- Started making the global functions docs more generic (not bash
specific) and added compatibility matrix.
* Qualify custom bash func name
- fixes issue where multiple cobra apps using custom bash completion
would have their __custom_func collide
- support fallback to plain __custom_func to maintain compatibility
#694
* Improve tests for bash completion __custom_func
- check for the correct number of occurrences of function name
#694
* Prefix bash functions with root command name
Prior to this commit the autocomplete bash functions were being prefixed
with the root command name, but references to those functions from
subcommands were having the subcommands prefixed instead - causing the
function lookups to fail and error out.
For example (as observed in kubernetes/kubernetes#60517):
kubectl create -f [Tab] failed with the following message: kubectl
create -f __create_handle_filename_extension_flag: command not found
in this case the function being invoked should be __kubectl_handle_filename_extension_flag
Signed-off-by: John McCabe <john@johnmccabe.net>
* Test filename extension and subdirs_in_dir for subcommands
This commit adds two regex based tests to ensure that the handle
filename extension and handle subdirs in dir functions are prefixed by
the root command when present in subcommands.
Previously they had been prefixed incorrectly with the subcommand name.
Signed-off-by: John McCabe <john@johnmccabe.net>
* Make preamble functions unique to command
Prior to this commit the functions in the preamble had names that didn't
vary based on the command for which the bash completion was generated.
This meant that if you had two bash completions with differences in the
preamble functions then only the last loaded function would be
available.
This commit prefixes all of these functions with the name of the command
so that multiple cobra generated completion files won't clash.
Signed-off-by: John McCabe <john@johnmccabe.net>
* Fix function names in writeFlagHandler
The references to the `__handle_filename_extension_flag` and
`__handle_subdirs_in_dir_flag` functions in `writeFlagHandler` hadn't
been updated correctly in the previous commits.
Signed-off-by: John McCabe <john@johnmccabe.net>
* Pass cmd into writeFlagHandler
This commit passes the cmd pointer into the writeFlagHandler so that the
`__handle_filename_extension_flag` and `__handle_subdirs_in_dir_flag`
functions can have the `cmd.Name()` prefixed.
* Update Bash completion tests
Prefixes the tested `__handle_filename_extension_flag` and
`__handle_subdirs_in_dir_flag` functions with the command name.
* Fix attemptd assignment to non-variable in bash 3
flaghash variable is an associative array which is only supported in
bash > 3.
* Use -gt instead of >
* Fix shellcheck
Before this change:
In - line 204:
declare -F $next_command >/dev/null && $next_command
^-- SC2086: Double quote to prevent globbing and word splitting.
--- FAIL: TestBashCompletions (0.34s)
bash_completions_test.go:138: shellcheck failed: exit status 1
* Avoid storing pointer to nil
Before this change, the new test fails with:
--- FAIL: TestSetOutput (0.00s)
command_test.go:198: expected setting output to nil to revert back to stdout, got <nil>
* bash_completions: cleanup for go vet
The gnarly block of string in the Fprint tripped up go vet and was not
easy to read.
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
* test: cleanup for go vet
Looks like copy'pasta and an unused variable
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
If a user specifies a flag to a command which doesn't make sense to a
subcommand do not show subcommands as a suggestion.
This also changes things to show both 'required flags' and 'commands'
instead of only 'required flags'