Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

- Bug Fixes
- Fixed file path completion for names containing square brackets, including bracketed working
directories and home directories.

## 4.4.2 (November 11, 2026)

- Security
Expand Down
6 changes: 3 additions & 3 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ def path_complete(

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

# Start the search string
search_str = text + "*"
search_str = text

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

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

# Filter out results that don't belong
if path_filter is not None:
Expand Down
53 changes: 53 additions & 0 deletions tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,59 @@ def test_path_completion_user_path_expansion(cmd_app) -> None:
assert completions_tilde_slash == completions_home


@pytest.mark.parametrize("prefix", ["[ab]", "[!ab]"])
def test_path_completion_literal_brackets_in_filename(cmd_app, tmp_path, monkeypatch, prefix) -> None:
for name in ("[ab].txt", "[!ab].txt", "a.txt", "c.txt"):
(tmp_path / name).touch()
monkeypatch.chdir(tmp_path)
line = f"shell cat {prefix}"

completions = cmd_app.path_complete(prefix, line, len(line) - len(prefix), len(line))

assert completions.to_strings() == (prefix + ".txt",)


@pytest.mark.parametrize("mode", ["absolute", "relative", "empty_cwd", "cwd_prefix", "tilde"])
def test_path_completion_literal_brackets_in_directory(cmd_app, tmp_path, monkeypatch, mode) -> None:
directory = tmp_path / "project[2026]"
directory.mkdir()
(directory / "target.txt").touch()
monkeypatch.chdir(tmp_path)
if mode == "absolute":
text = str(directory / "tar")
expected = str(directory / "target.txt")
elif mode == "relative":
text = os.path.join(directory.name, "tar")
expected = os.path.join(directory.name, "target.txt")
elif mode == "tilde":
monkeypatch.setenv("USERPROFILE" if sys.platform == "win32" else "HOME", str(directory))
text = os.path.join("~", "tar")
expected = os.path.join("~", "target.txt")
else:
monkeypatch.chdir(directory)
text = "" if mode == "empty_cwd" else "tar"
expected = "target.txt"
line = f"shell cat {text}"

completions = cmd_app.path_complete(text, line, len(line) - len(text), len(line))

assert completions.to_strings() == (expected,)


def test_path_completion_literal_brackets_directory_filter(cmd_app, tmp_path, monkeypatch) -> None:
(tmp_path / "[ab]-dir").mkdir()
(tmp_path / "a-dir").mkdir()
(tmp_path / "[ab]-file.txt").touch()
monkeypatch.chdir(tmp_path)
text = "[ab]"
line = f"shell cat {text}"

completions = cmd_app.path_complete(text, line, len(line) - len(text), len(line), path_filter=os.path.isdir)

assert completions.to_strings() == ("[ab]-dir" + os.path.sep,)
assert not completions.allow_finalization


def test_basic_completion(cmd_app) -> None:
text = "P"
line = f"list_food -f {text}"
Expand Down
Loading