Description
Hi all, reporting 2 issues here:
- When building a Completion Script with dynamic completion of nouns, Cobra doesn't seem to build in support for the custom completion data coming from
ValidArgsFunction
. - Furthermore, if the CLI program option descriptions contain a square bracket
[
or]
, they don't get escaped. This was mentioned in New zsh completion script seems broken #881 (comment) but I don't think it was fixed.
If I edit the generated shell completion script to escape that option description square bracket, then completion works, but only partially.
To provide an example, the dynamic completion in my ValidArgsFunction is simply listing the docker image tags on the local system. Once the square brackets are commented out in the generated script, I'm able to get completion to work, but it doesn't call my ValidArgsFunction
, it just lists the sub commands:
➜ grype git:(shell-completion-using-cobra) ✗ grype <tab><tab>
completion -- Generate a shell completion for Grype (listing local docker images)
db -- vulnerability database operations
help -- Help about any command
version -- show the version
I noticed for both Bash and Fish generated completion scripts that there is handling for the dynamic completion, but not in the Zsh generated scripts.
Here's the generated completion script section with the option descriptions:
#compdef _grype grype
function _grype {
local -a commands
_arguments -C \
'(-c --config)'{-c,--config}'[application config file]:' \
'(-o --output)'{-o,--output}'[report output formatter, options=[json table cyclonedx]]:' \
'(-q --quiet)'{-q,--quiet}'[suppress all logging output]' \
'(-s --scope)'{-s,--scope}'[selection of layers to analyze, options=[Squashed AllLayers]]:' \
'(-v --verbose)'{-v,--verbose}'[increase verbosity (-v = info, -vv = debug)]' \
"1: :->cmnds" \
"*::arg:->args"
Note that for output and scope the descriptions contain square brackets that should be escaped.
Also Note that for bash and fish completions, the generated script works out of the box.