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

✨ Upgrade autocompletion functionality for compatibility with shell_complete #1006

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

bckohan
Copy link
Contributor

@bckohan bckohan commented Sep 29, 2024

Cf issue: #949

Note, this includes (and supersedes) the changes from #974 and implements these suggestions #949 (comment), with a few differences.

I attempted to push these changes to #974 but was asked to open a new PR here instead.

This PR does 3 things:

  1. Allows autocompletion functions to accept a click.core.Parameter argument. I think this is necessary because without it you can't define generic completer functions that don't know what CLI options or arguments they may be attached to and that need to alter their behavior based on how click has processed the arguments before the incomplete argument. Concrete example: you have an option or argument that accepts multiple values but you do not wish those values to be repeated.

  2. Allows CompletionItems to be returned in addition to strings and 2-tuples from autocompletion functions.

  3. Fixes arg: List[str] parameter to be passed what it used to be given in click 7 (i.e. the raw command line string minus the script and incomplete string). In click 7 this parameter was being set to the arguments passed in the environment variables via the completion scripts installed on the system that are invoked when you hit tab. These parameters do not show up in ctx.args or sys.argv. Click has a general purpose obj attribute on the context that is available for downstream use. This PR sets that object to a dictionary in the completer classes and attaches the args from the environment variables to it. If obj is already defined it'll just not do that and nothing will break accept the args will be passed as an empty list to the autocompletion functions - which is what the original PR was doing anyway even though the tutorial docs said otherwise.

I think numbers 1 and 2 are important. Number 3 is mostly for backwards compatibility but I don't think its that critical - I just wanted to show that there is a way to do it using public Click 8 interfaces.

A reusable completer function tutorial has been added to the autocompletion tutorials

@svlandeg svlandeg changed the title Upgrade autocompletion for compatibility with shell_complete ✨ Upgrade autocompletion functionality for compatibility with shell_complete Oct 25, 2024
@svlandeg svlandeg linked an issue Oct 25, 2024 that may be closed by this pull request
1 task
@svlandeg svlandeg self-assigned this Oct 25, 2024
@svlandeg
Copy link
Member

Thanks for the PR! We'll get this reviewed and get back to you 🙏

@svlandeg svlandeg added the feature New feature, enhancement or request label Oct 25, 2024
@svlandeg
Copy link
Member

@bckohan: the tests are failing because there isn't a 100% code coverage with the new commits. Could you look into this? 🙏

@svlandeg svlandeg marked this pull request as draft October 25, 2024 09:18
@bckohan bckohan marked this pull request as ready for review November 8, 2024 23:26
@bckohan
Copy link
Contributor Author

bckohan commented Nov 8, 2024

@svlandeg I've updated the docs and added the necessary tests. Should be good to go!

@svlandeg
Copy link
Member

Great, thanks! I've got this on my TODO list to review in detail, but realistically it might take me 1 or 2 more weeks to get to it 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve support for autocompletion: shell_complete vs autocompletion
2 participants