-
Notifications
You must be signed in to change notification settings - Fork 33
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
Add autocompletion callback for Options #27
base: master
Are you sure you want to change the base?
Add autocompletion callback for Options #27
Conversation
Note: `Choices` take precedant over autocompletions
@@ -110,7 +110,10 @@ def get_choices(cli, prog_name, args, incomplete): | |||
break | |||
choices = [] | |||
if optctx: | |||
choices += [c if isinstance(c, tuple) else (c, None) for c in optctx.type.complete(ctx, incomplete)] | |||
choices += [c if isinstance(c, tuple) else(c, None) for c in optctx.type.complete(ctx, incomplete)] | |||
if not choices and hasattr(optctx, 'autocompletion') and optctx.autocompletion is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so it means that the choices provided by the type have the precedence over the autocompletion provided by the user.
Shouldn't it be the reverse? Or maybe merge both?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the order in the click
code: https://github.com/pallets/click/blob/93b1699cde5fbafe8a237f8f0d21c8f687b78f2f/click/_bashcomplete.py#L172
Realistically I guess you shouldn't be using both. But certainly for click.Choice
, I think that should take precedence over the auto-completion, because the input is also validated against the contents of click.Choice
, so it would be erroneous to be offering any other completions.
Co-Authored-By: Gaëtan Lehmann <[email protected]>
To summarize what exists for now:
By chance, the Choice class has a complete method that provided the completion against the choice. What is counter intuitive in the code of click-completion is that we call choice the result of the method complete. That explains why you confounded with the Choice class. In my mind, the more sensible is to first call get_user_autocompletions (instezd of rewriting part of it in click-completion) , then add the results of the complete method of the type. Also, I find getattr(optctx, "autocompletion", None) is not None shorter and more readable than hasattr(optctx, 'autocompletion') and optctx.autocompletion is not None. |
I did not follow what was done in the other PR and in click. Is this still relevant? |
As explained here in the Click documentation, and implemented here: https://github.com/pallets/click/blob/93b1699cde5fbafe8a237f8f0d21c8f687b78f2f/click/_bashcomplete.py#L185, click
Parameter
s have anautocompletion
argument that is currently not supported inclick-completion
.This small addition rectifies that :)