Skip to content

Commit 6deff84

Browse files
feat(cli): add --pattern filter to env list
1 parent c2de727 commit 6deff84

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

hatch/cli/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def _setup_env_commands(subparsers):
107107

108108
# List environments command
109109
env_list_parser = env_subparsers.add_parser("list", help="List all available environments")
110+
env_list_parser.add_argument(
111+
"--pattern",
112+
help="Filter environments by name using regex pattern",
113+
)
110114
env_list_parser.add_argument(
111115
"--json", action="store_true", help="Output in JSON format"
112116
)

hatch/cli/cli_env.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,31 @@ def handle_env_list(args: Namespace) -> int:
143143
Args:
144144
args: Namespace with:
145145
- env_manager: HatchEnvironmentManager instance
146+
- pattern: Optional regex pattern to filter environments
146147
- json: Optional flag for JSON output
147148
148149
Returns:
149150
Exit code (0 for success)
151+
152+
Reference: R02 §2.1 (02-list_output_format_specification_v2.md)
150153
"""
151154
import json as json_module
155+
import re
152156

153157
env_manager: "HatchEnvironmentManager" = args.env_manager
154158
json_output: bool = getattr(args, 'json', False)
159+
pattern: str = getattr(args, 'pattern', None)
155160
environments = env_manager.list_environments()
156161

162+
# Apply pattern filter if specified
163+
if pattern:
164+
try:
165+
regex = re.compile(pattern)
166+
environments = [env for env in environments if regex.search(env.get("name", ""))]
167+
except re.error as e:
168+
print(f"[ERROR] Invalid regex pattern: {e}")
169+
return EXIT_ERROR
170+
157171
if json_output:
158172
# JSON output per R02 §8.1
159173
env_data = []

0 commit comments

Comments
 (0)