Add support for user autocompletion functions #34
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✨ Add support for user
autocompletion
functions.The current implementation provides completion for options, arguments, and sub-commands with help strings. But if the user provides an
autocompletion
function it is not used. Although it would be used by plain Click (only in Bash and Zsh).This PR continues the idea from @chrisjsewell in #27. And adds the ideas in the review from @Konubinix.
I added comments to the sections I added that are a direct copy from Click, to try and minimize confusion by the duplicate code. I updated a couple of parts with logic from Click that is not yet here.
I made
get_choices
return a list instead of usingyield
, this allows returning early withget_user_autocompletions
.Here's an example small app that I used to test it. I wrote it with Typer but I can adapt it to use Click directly if you prefer, if the Typer parts are confusing, etc.
typertest/main.py
:If you create a file
pyproject.toml
with:And a file
typertest/__init__.py
Then you can install it with:
$ poetry install
With that, you can test with: