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

_is_incomplete_option incorrectly processes last_option in loop #2813

Open
msaipraneeth opened this issue Nov 30, 2024 · 1 comment
Open

Comments

@msaipraneeth
Copy link

msaipraneeth commented Nov 30, 2024

The _is_incomplete_option function in the Click library has a logic issue in its loop that sets the last_option variable. The function does not break the loop after assigning last_option, which leads to wrong shell completion.

Replication:

See https://github.com/msaipraneeth/click-tuple-param

RCA:

We can reproduce issue If we have flag option before tuple option

Environment:

  • Python version: 3.11
  • Click version: 8.1.7

Possible solution :

Once we identify the last option we have the break the loop in _is_incomplete_option function

def _is_incomplete_option(ctx: Context, args: t.List[str], param: Parameter) -> bool:
    """Determine if the given parameter is an option that needs a value.

    :param args: List of complete args before the incomplete value.
    :param param: Option object being checked.
    """
    if not isinstance(param, Option):
        return False

    if param.is_flag or param.count:
        return False

    last_option = None

    for index, arg in enumerate(reversed(args)):
        if index + 1 > param.nargs:
            break

        if _start_of_option(ctx, arg):
            last_option = arg
            break  # <-------------------------------------------------- break the loop on finding the option

    return last_option is not None and last_option in param.opts
@seebi
Copy link

seebi commented Dec 2, 2024

@msaipraneeth Thanks for looking into this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants