Skip to content

Instantly share code, notes, and snippets.

@simonw
Created April 8, 2024 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonw/3492bc9a6ff639ff7fbaa081fa508131 to your computer and use it in GitHub Desktop.
Save simonw/3492bc9a6ff639ff7fbaa081fa508131 to your computer and use it in GitHub Desktop.

2024-03-22T15:33:06 conversation: 01hskcqpq8btyptz4xe0r87m6g

Model: claude-3-opus-20240229

Prompt:

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
    ---
    ...
    """

System:

finish this code for me

Response:

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:

  1. We import the os module to handle file and directory operations.

  2. The @click.argument decorator is updated to ensure that the provided path exists, is a directory, and is readable. The file_okay parameter is set to False to disallow file paths, and dir_okay is set to True to allow directory paths.

  3. Inside the cli function, we use os.walk to traverse the directory tree starting from the provided path. It yields a tuple (root, dirs, files) for each directory, where root is the current directory, dirs is a list of subdirectories, and files is a list of files in the current directory.

  4. If include_hidden is False (default), we filter out directories and files that start with a dot (.) to exclude hidden files and directories.

  5. 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.

  6. We read the contents of the file using f.read() and store it in the file_contents variable.

  7. We use click.echo to print the file path, a separator (---), the file contents, an empty line, and another separator to the console.

  8. Finally, we add the if __name__ == "__main__": block to ensure that the cli 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment