Skip to content

Commit ae06f20

Browse files
committed
-e/--extension feature, closes #28
1 parent 3332a86 commit ae06f20

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ This will output the contents of every file, with each file preceded by its rela
2929

3030
### Options
3131

32+
- `-e/--extension <extension>`: Only include files with the specified extension. Can be used multiple times.
33+
34+
```bash
35+
files-to-prompt path/to/directory -e txt -e md
36+
```
37+
3238
- `--include-hidden`: Include files and folders starting with `.` (hidden files and directories).
3339

3440
```bash

files_to_prompt/cli.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def print_as_xml(writer, path, content):
5353

5454
def process_path(
5555
path,
56+
extensions,
5657
include_hidden,
5758
ignore_gitignore,
5859
gitignore_rules,
@@ -93,6 +94,9 @@ def process_path(
9394
if not any(fnmatch(f, pattern) for pattern in ignore_patterns)
9495
]
9596

97+
if extensions:
98+
files = [f for f in files if f.endswith(extensions)]
99+
96100
for file in sorted(files):
97101
file_path = os.path.join(root, file)
98102
try:
@@ -107,6 +111,7 @@ def process_path(
107111

108112
@click.command()
109113
@click.argument("paths", nargs=-1, type=click.Path(exists=True))
114+
@click.option("extensions", "-e", "--extension", multiple=True)
110115
@click.option(
111116
"--include-hidden",
112117
is_flag=True,
@@ -140,7 +145,13 @@ def process_path(
140145
)
141146
@click.version_option()
142147
def cli(
143-
paths, include_hidden, ignore_gitignore, ignore_patterns, output_file, claude_xml
148+
paths,
149+
extensions,
150+
include_hidden,
151+
ignore_gitignore,
152+
ignore_patterns,
153+
output_file,
154+
claude_xml,
144155
):
145156
"""
146157
Takes one or more paths to files or directories and outputs every file,
@@ -186,6 +197,7 @@ def cli(
186197
writer("<documents>")
187198
process_path(
188199
path,
200+
extensions,
189201
include_hidden,
190202
ignore_gitignore,
191203
gitignore_rules,

tests/test_files_to_prompt.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,31 @@ def test_ignore_patterns(tmpdir):
108108
assert "This file should be included" in result.output
109109

110110

111+
def test_specific_extensions(tmpdir):
112+
runner = CliRunner()
113+
with tmpdir.as_cwd():
114+
# Write one.txt one.py two/two.txt two/two.py three.md
115+
os.makedirs("test_dir/two")
116+
with open("test_dir/one.txt", "w") as f:
117+
f.write("This is one.txt")
118+
with open("test_dir/one.py", "w") as f:
119+
f.write("This is one.py")
120+
with open("test_dir/two/two.txt", "w") as f:
121+
f.write("This is two/two.txt")
122+
with open("test_dir/two/two.py", "w") as f:
123+
f.write("This is two/two.py")
124+
with open("test_dir/three.md", "w") as f:
125+
f.write("This is three.md")
126+
127+
# Try with -e py -e md
128+
result = runner.invoke(cli, ["test_dir", "-e", "py", "-e", "md"])
129+
assert result.exit_code == 0
130+
assert ".txt" not in result.output
131+
assert "test_dir/one.py" in result.output
132+
assert "test_dir/two/two.py" in result.output
133+
assert "test_dir/three.md" in result.output
134+
135+
111136
def test_mixed_paths_with_options(tmpdir):
112137
runner = CliRunner()
113138
with tmpdir.as_cwd():

0 commit comments

Comments
 (0)