Skip to content

Commit d84fc8f

Browse files
committed
fix: complete literal bracketed file paths
1 parent 593580e commit d84fc8f

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
- Bug Fixes
4+
- Fixed file path completion for names containing square brackets, including bracketed working
5+
directories and home directories.
6+
17
## 4.4.2 (November 11, 2026)
28

39
- Security

‎cmd2/cmd2.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ def path_complete(
23742374

23752375
# If the search text is blank, then search in the CWD for *
23762376
if not text:
2377-
search_str = os.path.join(os.getcwd(), "*")
2377+
search_str = os.path.join(os.getcwd(), "")
23782378
cwd_added = True
23792379
else:
23802380
# Purposely don't match any path containing wildcards
@@ -2384,7 +2384,7 @@ def path_complete(
23842384
return Completions()
23852385

23862386
# Start the search string
2387-
search_str = text + "*"
2387+
search_str = text
23882388

23892389
# Handle tilde expansion and completion
23902390
if text.startswith("~"):
@@ -2407,7 +2407,7 @@ def path_complete(
24072407
cwd_added = True
24082408

24092409
# Find all matching path completions
2410-
matches = glob.glob(search_str)
2410+
matches = glob.glob(glob.escape(search_str) + "*")
24112411

24122412
# Filter out results that don't belong
24132413
if path_filter is not None:

‎tests/test_completion.py‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,59 @@ def test_path_completion_user_path_expansion(cmd_app) -> None:
615615
assert completions_tilde_slash == completions_home
616616

617617

618+
@pytest.mark.parametrize("prefix", ["[ab]", "[!ab]"])
619+
def test_path_completion_literal_brackets_in_filename(cmd_app, tmp_path, monkeypatch, prefix) -> None:
620+
for name in ("[ab].txt", "[!ab].txt", "a.txt", "c.txt"):
621+
(tmp_path / name).touch()
622+
monkeypatch.chdir(tmp_path)
623+
line = f"shell cat {prefix}"
624+
625+
completions = cmd_app.path_complete(prefix, line, len(line) - len(prefix), len(line))
626+
627+
assert completions.to_strings() == (prefix + ".txt",)
628+
629+
630+
@pytest.mark.parametrize("mode", ["absolute", "relative", "empty_cwd", "cwd_prefix", "tilde"])
631+
def test_path_completion_literal_brackets_in_directory(cmd_app, tmp_path, monkeypatch, mode) -> None:
632+
directory = tmp_path / "project[2026]"
633+
directory.mkdir()
634+
(directory / "target.txt").touch()
635+
monkeypatch.chdir(tmp_path)
636+
if mode == "absolute":
637+
text = str(directory / "tar")
638+
expected = str(directory / "target.txt")
639+
elif mode == "relative":
640+
text = os.path.join(directory.name, "tar")
641+
expected = os.path.join(directory.name, "target.txt")
642+
elif mode == "tilde":
643+
monkeypatch.setenv("USERPROFILE" if sys.platform == "win32" else "HOME", str(directory))
644+
text = os.path.join("~", "tar")
645+
expected = os.path.join("~", "target.txt")
646+
else:
647+
monkeypatch.chdir(directory)
648+
text = "" if mode == "empty_cwd" else "tar"
649+
expected = "target.txt"
650+
line = f"shell cat {text}"
651+
652+
completions = cmd_app.path_complete(text, line, len(line) - len(text), len(line))
653+
654+
assert completions.to_strings() == (expected,)
655+
656+
657+
def test_path_completion_literal_brackets_directory_filter(cmd_app, tmp_path, monkeypatch) -> None:
658+
(tmp_path / "[ab]-dir").mkdir()
659+
(tmp_path / "a-dir").mkdir()
660+
(tmp_path / "[ab]-file.txt").touch()
661+
monkeypatch.chdir(tmp_path)
662+
text = "[ab]"
663+
line = f"shell cat {text}"
664+
665+
completions = cmd_app.path_complete(text, line, len(line) - len(text), len(line), path_filter=os.path.isdir)
666+
667+
assert completions.to_strings() == ("[ab]-dir" + os.path.sep,)
668+
assert not completions.allow_finalization
669+
670+
618671
def test_basic_completion(cmd_app) -> None:
619672
text = "P"
620673
line = f"list_food -f {text}"

0 commit comments

Comments
 (0)