summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2021-07-04 06:55:27 -0700
committerGitHub <noreply@github.com>2021-07-04 06:55:27 -0700
commite5536b2d24f1ce388e1b13b850222104a2ee1d51 (patch)
treed22ec904a8a4d268b19f6b1cadb12f6eeea3bd7e
parent2184ab1fdaf2956457c866dc68f87d09ca9af101 (diff)
parent91e388027d2270966e5a75e7a55b8cea3becc66b (diff)
downloadclick-e5536b2d24f1ce388e1b13b850222104a2ee1d51.tar.gz
Merge pull request #1930 from SamSchott/shell-completion-fix
Fix shell completion for arguments with non-alphanumeric characters
-rw-r--r--CHANGES.rst2
-rw-r--r--src/click/shell_completion.py7
-rw-r--r--tests/test_shell_completion.py8
3 files changed, 16 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 8f978ff..e5876af 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -10,6 +10,8 @@ Unreleased
- Bash version detection is locale independent. :issue:`1940`
- Empty ``default`` value is not shown for ``multiple=True``.
:issue:`1969`
+- Fix shell completion for arguments that start with a forward slash
+ such as absolute file paths. :issue:`1929`
Version 8.0.1
diff --git a/src/click/shell_completion.py b/src/click/shell_completion.py
index b444bae..cad080d 100644
--- a/src/click/shell_completion.py
+++ b/src/click/shell_completion.py
@@ -450,7 +450,12 @@ def _is_incomplete_argument(ctx: Context, param: Parameter) -> bool:
def _start_of_option(value: str) -> bool:
"""Check if the value looks like the start of an option."""
- return not value[0].isalnum() if value else False
+ if not value:
+ return False
+
+ c = value[0]
+ # Allow "/" since that starts a path.
+ return not c.isalnum() and c != "/"
def _is_incomplete_option(args: t.List[str], param: Parameter) -> bool:
diff --git a/tests/test_shell_completion.py b/tests/test_shell_completion.py
index ac34ef9..46143c0 100644
--- a/tests/test_shell_completion.py
+++ b/tests/test_shell_completion.py
@@ -125,6 +125,14 @@ def test_path_types(type, expect):
assert c.type == expect
+def test_absolute_path():
+ cli = Command("cli", params=[Option(["-f"], type=Path())])
+ out = _get_completions(cli, ["-f"], "/ab")
+ assert len(out) == 1
+ c = out[0]
+ assert c.value == "/ab"
+
+
def test_option_flag():
cli = Command(
"cli",