Tutorial Overview
This tutorial demonstrates how to use the glob pattern tool in
code2prompt CLI to filter and manage files based on include and exclude
patterns.
Tutorial Overview
This tutorial demonstrates how to use the glob pattern tool in
code2prompt CLI to filter and manage files based on include and exclude
patterns.
Glob patterns work similarly to tools like tree or grep,
providing powerful filtering capabilities. Check out the detailed
explanation for more information.
Ensure you have code2prompt installed. If you haven’t installed it yet, refer to the Installation Guide.
Glob patterns allow you to specify rules for filtering files and directories.
--include): Specify files and directories you want to include.--exclude): Specify files and directories you want to exclude.--include-priority): Resolves conflicts between include and exclude patterns.To practice with glob patterns, let’s create a sample folder structure with some files.
Run this script to set up a temporary directory structure
#!/bin/bash
# Create base directorymkdir -p test_dir/{lowercase,uppercase,.secret}
# Create files in the structureecho "content foo.py" > "test_dir/lowercase/foo.py"echo "content bar.py" > "test_dir/lowercase/bar.py"echo "content baz.py" > "test_dir/lowercase/baz.py"echo "content qux.txt" > "test_dir/lowercase/qux.txt"echo "content corge.txt" > "test_dir/lowercase/corge.txt"echo "content grault.txt" > "test_dir/lowercase/grault.txt"
echo "CONTENT FOO.py" > "test_dir/uppercase/FOO.PY"echo "CONTENT BAR.py" > "test_dir/uppercase/BAR.PY"echo "CONTENT BAZ.py" > "test_dir/uppercase/BAZ.PY"echo "CONTENT QUX.txt" > "test_dir/uppercase/QUX.TXT"echo "CONTENT CORGE.txt" > "test_dir/uppercase/CORGE.TXT"echo "CONTENT GRAULT.txt" > "test_dir/uppercase/GRAULT.TXT"
echo "top secret" > "test_dir/.secret/secret.txt"To clean up the structure later, run:
rm -rf test_dirIt will create the following directory structure:
code2promptBy default, code2prompt includes all files in the specified directory respecting the .gitignore.
The -e for --exclude and -i for --include options allow you to filter files dynamically based on glob patterns.
Command:
code2prompt test_dirAll files are included:
lowercase/foo.pylowercase/bar.py …uppercase/FOO.py ….secret/secret.txtExclude .txt or .md files:
code2prompt test_dir --exclude="*.txt,*.md"Excluded:
.txt or .md filesIncluded:
lowercase/foo.pylowercase/bar.pyuppercase/FOO.pyInclude only Python files:
code2prompt test_dir --include="*.py"Included:
.py filesExcluded:
.secret/secret.txtInclude .py files but exclude files in the uppercase folder:
code2prompt test_dir --include="*.py" --exclude="**/uppercase/*" --include-priority=trueIncluded:
lowercase/1 files having .py extensionExcluded:
uppercase files.secret/secret.txtExclude the uppercase directory:
code2prompt test_dir --exclude "**/uppercase*"or with short syntax:
code2prompt test_dir -e "uppercase*"Included:
lowercase and .secretExcluded:
uppercaseThe glob pattern tool in code2prompt allows you to filter files and directories effectively using:
--include for specifying files to include--exclude for files to exclude--include-priority for resolving conflicts between patternsTo practice, set up the sample directory, try out the commands, and see how the tool filters files dynamically.