LinuxCommandLibrary

cookiecutter

Generate projects from templates

SYNOPSIS

cookiecutter [OPTIONS] <template_source>

<template_source>: A URL (e.g., Git repository) or local path to a Cookiecutter template.

PARAMETERS

--no-input
    Do not prompt for input; use default values or values from replay.

--checkout <branch|tag|commit>
    The branch, tag, or commit to checkout after cloning the template.

--output-dir <directory>
    Where to output the generated project.

--config-file <path>
    Specify a custom user configuration file instead of the default.

--default-config
    Do not load any user configuration file; use only default settings.

--overwrite-if-exists
    Overwrite the contents of the output directory if it already exists.

--skip-if-file-exists
    Skip over existing files, do not overwrite them during generation.

--replay
    Do not prompt for input; use the last-used input from the replay file.

--skip-replay
    Do not use the last-used input from the replay file; prompt for new input.

--verbose
    Show verbose debug logging output.

--directory <dirname>
    Relative path to the template directory within the repository.

DESCRIPTION

cookiecutter is a powerful command-line utility designed to automate the scaffolding of new projects from pre-defined templates. It allows developers to quickly create consistent project structures, boilerplate code, and configuration files, significantly accelerating initial project setup. Users provide a template's Git repository URL or local file path.

Upon invocation, cookiecutter reads the cookiecutter.json file located within the template, which specifies a set of variables (e.g., project name, author, license). It then interactively prompts the user for values for these variables. Once all necessary input is collected, cookiecutter leverages the Jinja2 templating engine to render the template files, replacing variable placeholders with the user-provided data. The final output is a new project directory structured according to the template, containing all the generated files. This tool promotes uniformity across projects, minimizes manual setup errors, and is widely adopted for maintaining best practices across diverse programming ecosystems, particularly within the Python community.

CAVEATS

Template quality and maintenance can vary widely, potentially leading to outdated or poorly structured projects.
Updates to the original template do not automatically propagate to projects already generated from it.
Requires git to be installed and accessible for cloning templates from remote repositories.
Creating sophisticated templates with complex hooks and conditional logic can be challenging.

TEMPLATE STRUCTURE

A cookiecutter template is essentially a directory containing project files and a mandatory cookiecutter.json file. This JSON file defines the variables that cookiecutter will prompt the user for, along with their default values. Within the template's files, Jinja2 templating syntax (e.g., {{cookiecutter.project_slug}}) is used to embed these variables, which are replaced during project generation.

HOOKS

cookiecutter supports 'hooks' – Python scripts that can be executed before or after the project generation. Files named pre_gen_project.py and post_gen_project.py (located in the template's hooks directory) are automatically run at the respective stages, enabling custom logic like validating inputs, initializing Git repositories, installing dependencies, or printing custom messages.

HISTORY

cookiecutter was created by Audrey Roy Greenfeld and Daniel Greenfeld, with its initial release dating back to 2013. It rapidly gained traction, especially within the Python development community, as a de-facto standard for standardizing project layouts and boilerplate code generation. Its ease of use and flexibility contributed significantly to its widespread adoption, helping developers maintain consistency and accelerate new project creation workflows across various programming ecosystems.

SEE ALSO

git(1), mkdir(1)

Copied to clipboard