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

Document how to source completions #2488

Closed
2 tasks done
ajeetdsouza opened this issue May 20, 2021 · 7 comments
Closed
2 tasks done

Document how to source completions #2488

ajeetdsouza opened this issue May 20, 2021 · 7 comments
Labels
A-completion Area: completion generator A-docs Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much

Comments

@ajeetdsouza
Copy link
Contributor

ajeetdsouza commented May 20, 2021

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Clap Version

3.0.0-beta.2

Where?

clap_generate

What's wrong?

Currently, clap generates completions without providing any insight into how to actually get them running in the shell.

How to fix?

It would be nice if we could specify (for each shell):

  1. How to source the generated completions as a file
  2. How to source the generated completions when printed to stdout
  3. How to add the above to your shell configuration file
@ajeetdsouza ajeetdsouza added the A-docs Area: documentation, including docs.rs, readme, examples, etc... label May 20, 2021
@pksunkara pksunkara added the A-completion Area: completion generator label May 20, 2021
@kidonng
Copy link

kidonng commented Jun 20, 2021

I'd like to give a simple and general description:

  • Bash: install bash-completion (some distros have this by default), then the completions can be sourced

  • Zsh: the optimal way is to use $fpath, but if one would really like to go the manual way:

    autoload -U compinit
    compinit
    source <(program completions zsh | sed '$d') # can also be `head -n -1` or whatever as long as the last line is removed
    compdef _program program

    FWIW, it is said that completions generated by cobra (clap counterpart in Go) can be directly sourced (i.e. no need to strip the last line), may worth looking into

  • Fish: directly source-able 🎉

I don't have much experience with other shells so additions welcome.

@nicholastmosher
Copy link

Hmm, that makes me wonder if there wouldn't be any reason not to update the output of program completions zsh to just include the autoload and compdef commands. It'd be nice to just be able to do

source <(program completions zsh)

Where program completions zsh would just look like

autoload -U compinit
compinit
<old content of 'program completions zsh'>
compdef _program program

One could certainly do that at the app-level, but is there no reason to build that directly into the completions output of clap?

@kidonng
Copy link

kidonng commented Jul 4, 2021

@nicholastmosher I can't say for sure, but I think generally clap completions are generated at packaging time. That is to say, when you install a program from the package manager, it comes with the completions placed at the suitable location so shells will pick them up automatically. Manually sourcing the completions is not the usual way people use it.

@nicholastmosher
Copy link

Ah I see. I was taking inspiration from the oh-my-zsh kubectl completions plugin: https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/kubectl/kubectl.plugin.zsh#L1-L11, though it seems like that is more geared towards dynamic completions like described in #1232

@epage epage added E-easy Call for participation: Experience needed to fix: Easy / not much C-enhancement Category: Raise on the bar on expectations labels Dec 13, 2021
@gakonst
Copy link

gakonst commented Dec 22, 2021

For me I had to do 2 things to get it working with ZSH 5.8 (program is your command's name):

  1. Remove the last line (program "$@")
  2. Replace the first line from #compdef program to #compdef _program program

Unsure why this is not happening by default, is this a bug?

Also, if there's any backticks in any help message, the shell tries to interpret it due to shell expansion, suspect it's related to #1596

@epage
Copy link
Member

epage commented Jul 19, 2024

The feature=unstable-dynamic completions now have documentation on how to source them, see #5586.

I'm inclined to consider this closed but would be open to input from others on if there is concern over that vs the current code-generated completions.

@epage
Copy link
Member

epage commented Aug 10, 2024

As this was resolved for dynamic completions in #5586, I'm going to go ahead and close this.

To track stabilization, see #3166

@epage epage closed this as completed Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator A-docs Area: documentation, including docs.rs, readme, examples, etc... C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much
Projects
None yet
Development

No branches or pull requests

6 participants