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

Add autocompletion callback for Options #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions click_completion/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def get_choices(cli, prog_name, args, incomplete):
choices = []
if optctx:
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:
Copy link
Collaborator

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?

Copy link
Author

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.

dynamic_completions = optctx.autocompletion(ctx=ctx, args=args, incomplete=incomplete)
choices += [c if isinstance(c, tuple) else (c, None) for c in dynamic_completions]
else:
for param in ctx.command.get_params(ctx):
if (completion_configuration.complete_options or incomplete and not incomplete[:1].isalnum()) and isinstance(param, Option):
Expand Down