-
Notifications
You must be signed in to change notification settings - Fork 162
Add --cxml flag
#16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add --cxml flag
#16
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ venv | |
| .pytest_cache | ||
| *.egg-info | ||
| .DS_Store | ||
| build/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import os | ||
| import click | ||
| from fnmatch import fnmatch | ||
|
|
||
| import click | ||
|
|
||
|
|
||
| def should_ignore(path, gitignore_rules): | ||
| for rule in gitignore_rules: | ||
|
|
@@ -22,18 +23,39 @@ def read_gitignore(path): | |
| return [] | ||
|
|
||
|
|
||
| def print_path(path, content, xml): | ||
| if xml: | ||
| print_as_xml(path, content) | ||
| else: | ||
| print_default(path, content) | ||
|
|
||
|
|
||
| def print_default(path, content): | ||
| click.echo(path) | ||
| click.echo("---") | ||
| click.echo(content) | ||
| click.echo() | ||
| click.echo("---") | ||
|
|
||
|
|
||
| def print_as_xml(path, content): | ||
| click.echo(f'<document path="{path}">') | ||
| click.echo(content) | ||
| click.echo("</document>") | ||
|
|
||
|
|
||
| def process_path( | ||
| path, include_hidden, ignore_gitignore, gitignore_rules, ignore_patterns | ||
| path, | ||
| include_hidden, | ||
| ignore_gitignore, | ||
| gitignore_rules, | ||
| ignore_patterns, | ||
| xml, | ||
| ): | ||
| if os.path.isfile(path): | ||
| try: | ||
| with open(path, "r") as f: | ||
| file_contents = f.read() | ||
| click.echo(path) | ||
| click.echo("---") | ||
| click.echo(file_contents) | ||
| click.echo() | ||
| click.echo("---") | ||
| print_path(path, f.read(), xml) | ||
| except UnicodeDecodeError: | ||
| warning_message = f"Warning: Skipping file {path} due to UnicodeDecodeError" | ||
| click.echo(click.style(warning_message, fg="red"), err=True) | ||
|
|
@@ -63,17 +85,11 @@ def process_path( | |
| if not any(fnmatch(f, pattern) for pattern in ignore_patterns) | ||
| ] | ||
|
|
||
| for file in files: | ||
| for file in sorted(files): | ||
| file_path = os.path.join(root, file) | ||
| try: | ||
| with open(file_path, "r") as f: | ||
| file_contents = f.read() | ||
|
|
||
| click.echo(file_path) | ||
| click.echo("---") | ||
| click.echo(file_contents) | ||
| click.echo() | ||
| click.echo("---") | ||
| print_path(file_path, f.read(), xml) | ||
| except UnicodeDecodeError: | ||
| warning_message = ( | ||
| f"Warning: Skipping file {file_path} due to UnicodeDecodeError" | ||
|
|
@@ -100,8 +116,13 @@ def process_path( | |
| default=[], | ||
| help="List of patterns to ignore", | ||
| ) | ||
| @click.option( | ||
| "--xml", | ||
| is_flag=True, | ||
| help="Output in XML format suitable for Claude's long context window.", | ||
| ) | ||
| @click.version_option() | ||
| def cli(paths, include_hidden, ignore_gitignore, ignore_patterns): | ||
| def cli(paths, include_hidden, ignore_gitignore, ignore_patterns, xml): | ||
| """ | ||
| Takes one or more paths to files or directories and outputs every file, | ||
| recursively, each one preceded with its filename like this: | ||
|
|
@@ -114,13 +135,41 @@ def cli(paths, include_hidden, ignore_gitignore, ignore_patterns): | |
| path/to/file2.py | ||
| --- | ||
| ... | ||
|
|
||
| If the `--xml` flag is provided, the output will be structured as follows: | ||
|
|
||
| Here are some documents for you to reference for your task: | ||
|
|
||
| <documents> | ||
| <document path="path/to/file1.txt"> | ||
| Contents of file1.txt | ||
| </document> | ||
|
|
||
| <document path="path/to/file2.txt"> | ||
| Contents of file2.txt | ||
| </document> | ||
| ... | ||
| </documents> | ||
| """ | ||
| gitignore_rules = [] | ||
| for path in paths: | ||
| if not os.path.exists(path): | ||
| raise click.BadArgumentUsage(f"Path does not exist: {path}") | ||
| if not ignore_gitignore: | ||
| gitignore_rules.extend(read_gitignore(os.path.dirname(path))) | ||
| if xml and path == paths[0]: | ||
| click.echo("Here are some documents for you to reference for your task:") | ||
|
||
| click.echo() | ||
| click.echo("<documents>") | ||
|
|
||
| process_path( | ||
| path, include_hidden, ignore_gitignore, gitignore_rules, ignore_patterns | ||
| path, | ||
| include_hidden, | ||
| ignore_gitignore, | ||
| gitignore_rules, | ||
| ignore_patterns, | ||
| xml, | ||
| ) | ||
|
|
||
| if xml: | ||
| click.echo("</documents>") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.