diff --git a/README.md b/README.md index 389f4cd..31d1bb1 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,12 @@ This will output the contents of every file, with each file preceded by its rela ### Options +- `-e/--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 diff --git a/files_to_prompt/cli.py b/files_to_prompt/cli.py index fe68ad4..4fb358b 100644 --- a/files_to_prompt/cli.py +++ b/files_to_prompt/cli.py @@ -53,6 +53,7 @@ def print_as_xml(writer, path, content): def process_path( path, + extensions, include_hidden, ignore_gitignore, gitignore_rules, @@ -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: @@ -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, @@ -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, @@ -186,6 +197,7 @@ def cli( writer("") process_path( path, + extensions, include_hidden, ignore_gitignore, gitignore_rules, diff --git a/tests/test_files_to_prompt.py b/tests/test_files_to_prompt.py index 648e9ab..207a515 100644 --- a/tests/test_files_to_prompt.py +++ b/tests/test_files_to_prompt.py @@ -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():