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

Flags are treated as regular boolean values #116

Open
ChristopherHammond13 opened this issue Mar 4, 2024 · 0 comments
Open

Flags are treated as regular boolean values #116

ChristopherHammond13 opened this issue Mar 4, 2024 · 0 comments

Comments

@ChristopherHammond13
Copy link

ChristopherHammond13 commented Mar 4, 2024

I have started the process of exploring whether we can move one of our Cmd2 tools over to click-repl to benefit from the Prompt Toolkit backend, but seem to have hit an edge case with Click flags.

Minimal reproduction:

import click

from click_repl import repl
from prompt_toolkit.shortcuts import CompleteStyle


@click.group
def my_cli():
    pass


@my_cli.command(name="test")
@click.argument("arg1", type=click.STRING)
@click.option(
    "-b",
    "--some-option",
    "some_option",
    help="Toggle on a flag",
    is_flag=True,
    show_default=False,
    default=False,
)
def my_command(arg1, some_option):
    print(f"{arg1} // {some_option}")


@my_cli.command
def myrepl():
    prompt_kwargs = {
        "complete_style": CompleteStyle.COLUMN,
        "validate_while_typing": False,
    }
    repl(click.get_current_context(), prompt_kwargs=prompt_kwargs)


my_cli(["myrepl"])

This results in the following UI being shown to the user, which erroneously suggests that a value must be provided for the boolean flag. However, as this click argument definition should result in a flag that somewhat mirrors the argparse store_true option, this is wrong:
image

Furthermore, if one of the options shown (true/false) is chosen, it takes the place of the positional argument arg1 resulting in the following error:
image

Expected result: when show_flag=True, click-repl should not suggest a parameter for the option as it's a flag. Instead, the user should be prompted to just type the value of the positional argument to get a response, like this:
image

I suspect the issue lies somewhere in this function, but I don't know enough about Prompt Toolkit completers to fix this myself in the case that is_flag is True.

Thank you in advance!

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

1 participant