Skip to content

Bash completion is broken when a default value is specified for a Choice Argument #925

@atherra

Description

@atherra

Adding a default value to an argument of the Choice type will hose autocompletion. Can be tested as follows:

Slightly modifying test_argument_choice test example in test_bashcomplete.py to add a default argument:

def test_argument_choice():
    @click.command()
    @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))
    @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']), default='arg21')
    @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))
    def cli():
        pass

    assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']
    assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12']
    assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22']
    assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument']
    assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument']

Result:

Traceback (most recent call last):
  File "test_compl.py", line 19, in <module>
    test_argument_choice()
  File "test_compl.py", line 14, in test_argument_choice
    assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22']
AssertionError

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions