Model: claude-3-opus-20240229
import click
@click.command()
@click.argument(
"path",
type=click.Path
)
@click.option(
"--include-hidden",
is_flag=True,
help="Include files and folders starting with .",
)
@click.version_option()
def cli(path, include_hidden):
"""
Takes a path to a folder and outputs every file in that folder,
recursively, each one preceeded with its filename like this:
path/to/file.py
----
Contents of file.py goes here
---
path/to/file2.py
---
...
"""
finish this code for me
Here's the completed code based on the provided code snippet:
import os
import click
@click.command()
@click.argument(
"path",
type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True),
)
@click.option(
"--include-hidden",
is_flag=True,
help="Include files and folders starting with .",
)
@click.version_option()
def cli(path, include_hidden):
"""
Takes a path to a folder and outputs every file in that folder,
recursively, each one preceded with its filename like this:
path/to/file.py
----
Contents of file.py goes here
---
path/to/file2.py
---
...
"""
for root, dirs, files in os.walk(path):
if not include_hidden:
dirs[:] = [d for d in dirs if not d.startswith(".")]
files = [f for f in files if not f.startswith(".")]
for file in files:
file_path = os.path.join(root, file)
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("---")
if __name__ == "__main__":
cli()
Here's what each part of the code does:
-
We import the
os
module to handle file and directory operations. -
The
@click.argument
decorator is updated to ensure that the providedpath
exists, is a directory, and is readable. Thefile_okay
parameter is set toFalse
to disallow file paths, anddir_okay
is set toTrue
to allow directory paths. -
Inside the
cli
function, we useos.walk
to traverse the directory tree starting from the providedpath
. It yields a tuple(root, dirs, files)
for each directory, whereroot
is the current directory,dirs
is a list of subdirectories, andfiles
is a list of files in the current directory. -
If
include_hidden
isFalse
(default), we filter out directories and files that start with a dot (.
) to exclude hidden files and directories. -
For each file in the current directory, we construct the file path using
os.path.join(root, file)
and open the file in read mode. -
We read the contents of the file using
f.read()
and store it in thefile_contents
variable. -
We use
click.echo
to print the file path, a separator (---
), the file contents, an empty line, and another separator to the console. -
Finally, we add the
if __name__ == "__main__":
block to ensure that thecli
function is executed only when the script is run directly (not when imported as a module).
You can now run this script from the command line by providing the path to a folder as an argument. The script will recursively traverse the folder and print the contents of each file, preceded by its filename. The --include-hidden
option can be used to include hidden files and directories in the output.