diff options
| author | David Lord <davidism@gmail.com> | 2020-02-26 19:33:03 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-26 19:33:03 -0800 |
| commit | 29e7de55d6c1c41987a734c00da37e8a66ecb211 (patch) | |
| tree | a50c3f6b97a0be70f3708d7efd37ceda3262f323 | |
| parent | 0fc0a42d46790aad6266af140a7b78700273632b (diff) | |
| parent | 8f0f4e6b811138cdd661a2e21b63d698dd186185 (diff) | |
| download | click-29e7de55d6c1c41987a734c00da37e8a66ecb211.tar.gz | |
Merge pull request #1248 from bertolinocastro/master
don't complete args that start with dash after double dash separator
| -rw-r--r-- | CHANGES.rst | 2 | ||||
| -rw-r--r-- | click/_bashcomplete.py | 4 | ||||
| -rw-r--r-- | tests/test_bashcomplete.py | 27 |
3 files changed, 32 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 2fc1612..edf89a2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -60,6 +60,8 @@ Unreleased - Add ZSH completion autoloading and error handling. :issue:`1348` - Add a repr to ``Command``, ``Group``, ``Option``, and ``Argument``, showing the name for friendlier debugging. :issue:`1267` +- Completion doesn't consider option names if a value starts with + ``-`` after the ``--`` separator. :issue:`1247` Version 7.0 diff --git a/click/_bashcomplete.py b/click/_bashcomplete.py index e97c539..a31b8e6 100644 --- a/click/_bashcomplete.py +++ b/click/_bashcomplete.py @@ -237,6 +237,8 @@ def get_choices(cli, prog_name, args, incomplete): if ctx is None: return [] + has_double_dash = "--" in all_args + # In newer versions of bash long opts with '='s are partitioned, but it's easier to parse # without the '=' if start_of_option(incomplete) and WORDBREAK in incomplete: @@ -247,7 +249,7 @@ def get_choices(cli, prog_name, args, incomplete): incomplete = '' completions = [] - if start_of_option(incomplete): + if not has_double_dash and start_of_option(incomplete): # completions for partial options for param in ctx.command.params: if isinstance(param, Option) and not param.hidden: diff --git a/tests/test_bashcomplete.py b/tests/test_bashcomplete.py index 7214d1a..5eb4068 100644 --- a/tests/test_bashcomplete.py +++ b/tests/test_bashcomplete.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +import pytest + import click from click._bashcomplete import get_choices @@ -407,3 +409,28 @@ def test_hidden(): # If the user exactly types out the hidden command, complete its subcommands. assert choices_without_help(cli, ['hgroup'], '') == ['hgroupsub'] assert choices_without_help(cli, ['hsub'], '--h') == ['--hname'] + + +@pytest.mark.parametrize( + ("args", "part", "expect"), + [ + ([], "-", ["--opt"]), + (["value"], "--", ["--opt"]), + ([], "-o", []), + (["--opt"], "-o", []), + (["--"], "", ["name", "-o", "--opt", "--"]), + (["--"], "--o", ["--opt"]), + ], +) +def test_args_with_double_dash_complete(args, part, expect): + def _complete(ctx, args, incomplete): + values = ["name", "-o", "--opt", "--"] + return [x for x in values if x.startswith(incomplete)] + + @click.command() + @click.option("--opt") + @click.argument("args", nargs=-1, autocompletion=_complete) + def cli(opt, args): + pass + + assert choices_without_help(cli, args, part) == expect |
