Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zsh Completion Issues #1226

Closed
dakaneye opened this issue Sep 11, 2020 · 6 comments
Closed

Zsh Completion Issues #1226

dakaneye opened this issue Sep 11, 2020 · 6 comments

Comments

@dakaneye
Copy link

Hi all, reporting 2 issues here:

  1. 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.
  2. 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.

@marckhouzam
Copy link
Collaborator

Hi @dakaneye, Cobra 1.0 does not support ValidArgsFunction for zsh, see the doc on the 1.0.0 branch tag: https://github.com/spf13/cobra/blob/v1.0.0/zsh_completions.md#whats-not-yet-supported

The next release of Cobra will add this support which is already part of the master branch. If you have been reading the documentation of the master branch, then your confusion is very understandable.

@dakaneye
Copy link
Author

Yep that's why I was confused! Thanks for the update

@marckhouzam
Copy link
Collaborator

@dakaneye considering the situation I don't believe there is anything to fix (except maybe making a release 😉). Would you mind closing this issue in this case?

@dakaneye
Copy link
Author

dakaneye commented Sep 11, 2020

No problem! Btw, @marckhouzam any estimated time frame in the release?

@marckhouzam
Copy link
Collaborator

No problem! Btw, @marckhouzam any estimated time frame in the release?

I don't know myself. Maybe @jharshman or @jpmcb would know the plan for the next release?

@alfredodeza
Copy link

For clarity, it looks like ValidArgsFunction is now fully supported for ZSH since 1.1.0 https://github.com/spf13/cobra/releases/tag/v1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants