Skip to content

Commit

Permalink
-e/--extension feature, closes simonw#28
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 16, 2024
1 parent 3332a86 commit ae06f20
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ This will output the contents of every file, with each file preceded by its rela

### Options

- `-e/--extension <extension>`: Only include files with the specified extension. Can be used multiple times.

```bash
files-to-prompt path/to/directory -e txt -e md
```

- `--include-hidden`: Include files and folders starting with `.` (hidden files and directories).

```bash
Expand Down
14 changes: 13 additions & 1 deletion files_to_prompt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def print_as_xml(writer, path, content):

def process_path(
path,
extensions,
include_hidden,
ignore_gitignore,
gitignore_rules,
Expand Down Expand Up @@ -93,6 +94,9 @@ def process_path(
if not any(fnmatch(f, pattern) for pattern in ignore_patterns)
]

if extensions:
files = [f for f in files if f.endswith(extensions)]

for file in sorted(files):
file_path = os.path.join(root, file)
try:
Expand All @@ -107,6 +111,7 @@ def process_path(

@click.command()
@click.argument("paths", nargs=-1, type=click.Path(exists=True))
@click.option("extensions", "-e", "--extension", multiple=True)
@click.option(
"--include-hidden",
is_flag=True,
Expand Down Expand Up @@ -140,7 +145,13 @@ def process_path(
)
@click.version_option()
def cli(
paths, include_hidden, ignore_gitignore, ignore_patterns, output_file, claude_xml
paths,
extensions,
include_hidden,
ignore_gitignore,
ignore_patterns,
output_file,
claude_xml,
):
"""
Takes one or more paths to files or directories and outputs every file,
Expand Down Expand Up @@ -186,6 +197,7 @@ def cli(
writer("<documents>")
process_path(
path,
extensions,
include_hidden,
ignore_gitignore,
gitignore_rules,
Expand Down
25 changes: 25 additions & 0 deletions tests/test_files_to_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ def test_ignore_patterns(tmpdir):
assert "This file should be included" in result.output


def test_specific_extensions(tmpdir):
runner = CliRunner()
with tmpdir.as_cwd():
# Write one.txt one.py two/two.txt two/two.py three.md
os.makedirs("test_dir/two")
with open("test_dir/one.txt", "w") as f:
f.write("This is one.txt")
with open("test_dir/one.py", "w") as f:
f.write("This is one.py")
with open("test_dir/two/two.txt", "w") as f:
f.write("This is two/two.txt")
with open("test_dir/two/two.py", "w") as f:
f.write("This is two/two.py")
with open("test_dir/three.md", "w") as f:
f.write("This is three.md")

# Try with -e py -e md
result = runner.invoke(cli, ["test_dir", "-e", "py", "-e", "md"])
assert result.exit_code == 0
assert ".txt" not in result.output
assert "test_dir/one.py" in result.output
assert "test_dir/two/two.py" in result.output
assert "test_dir/three.md" in result.output


def test_mixed_paths_with_options(tmpdir):
runner = CliRunner()
with tmpdir.as_cwd():
Expand Down

0 comments on commit ae06f20

Please sign in to comment.