A command-line utility that creates projects from project templates, e.g. creating a Python package project from a Python package project template.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for generating projects from templates, including the primary cookiecutter() function and CLI interface. This module provides both programmatic and command-line access to cookiecutter's project generation capabilities.
The main cookiecutter() function that generates projects from templates with comprehensive configuration options.
def cookiecutter(
template,
checkout=None,
no_input=False,
extra_context=None,
replay=None,
overwrite_if_exists=False,
output_dir='.',
config_file=None,
default_config=False,
password=None,
directory=None,
skip_if_file_exists=False,
accept_hooks=True,
keep_project_on_failure=False
):
"""
Run Cookiecutter just as if using it from the command line.
Parameters:
- template: str - A directory containing a project template directory, or a URL to a git repository
- checkout: str, optional - The branch, tag or commit ID to checkout after clone
- no_input: bool - Do not prompt for user input. Use default values for template parameters
- extra_context: dict, optional - A dictionary of context that overrides default and user configuration
- replay: bool or str, optional - Do not prompt for input, instead read from saved json
- overwrite_if_exists: bool - Overwrite the contents of the output directory if it exists
- output_dir: str - Where to output the generated project dir into
- config_file: str, optional - User configuration file path
- default_config: bool - Use default values rather than a config file
- password: str, optional - The password to use when extracting the repository
- directory: str, optional - Relative path to a cookiecutter template in a repository
- skip_if_file_exists: bool - Skip the files in the corresponding directories if they already exist
- accept_hooks: bool - Accept pre and post hooks if set to True
- keep_project_on_failure: bool - If True keep generated project directory even when generation fails
Returns:
str - Path to the generated project directory
"""Main CLI entry point with comprehensive click options for all cookiecutter functionality.
def main(
template,
extra_context,
no_input,
checkout,
verbose,
replay,
overwrite_if_exists,
output_dir,
config_file,
default_config,
debug_file,
directory,
skip_if_file_exists,
accept_hooks,
replay_file,
list_installed,
keep_project_on_failure
):
"""
Create a project from a Cookiecutter project template (TEMPLATE).
Main CLI function that processes command-line arguments and calls the cookiecutter() function.
"""Helper functions for CLI functionality.
def version_msg():
"""
Return the Cookiecutter version, location and Python powering it.
Returns:
str - Formatted version information string
"""
def validate_extra_context(ctx, param, value):
"""
Validate extra context from command line arguments.
Parameters:
- ctx: click.Context - Click context object
- param: click.Parameter - Click parameter object
- value: tuple - Extra context values from command line
Returns:
dict - Validated extra context dictionary
"""
def list_installed_templates(default_config, passed_config_file):
"""
List installed (locally cloned) templates.
Parameters:
- default_config: bool - Whether to use default config
- passed_config_file: str - Path to config file
"""from cookiecutter.main import cookiecutter
# Generate from local template
result = cookiecutter('./my-template/')
# Generate from GitHub repository
result = cookiecutter('gh:audreyfeldroy/cookiecutter-pypackage')
# Generate with no user input
result = cookiecutter(
'template-path',
no_input=True,
extra_context={'project_name': 'my-project'}
)# Generate with custom output directory and overwrite existing
result = cookiecutter(
'gh:user/template-repo',
checkout='v2.0.0',
output_dir='/path/to/projects',
overwrite_if_exists=True,
extra_context={
'project_name': 'advanced-project',
'author_name': 'Jane Developer'
}
)
# Generate from specific directory within repository
result = cookiecutter(
'gh:multi-template/repo',
directory='python-package',
no_input=True
)# Use replay functionality
result = cookiecutter(
'template-path',
replay=True # Uses saved answers from previous run
)
# Use custom config file
result = cookiecutter(
'template-path',
config_file='/path/to/custom-config.yaml',
accept_hooks=False # Disable pre/post hooks
)from cookiecutter.main import cookiecutter
from cookiecutter.exceptions import (
CookiecutterException,
OutputDirExistsException,
RepositoryNotFound
)
try:
result = cookiecutter('template-path')
except OutputDirExistsException:
# Handle existing output directory
result = cookiecutter('template-path', overwrite_if_exists=True)
except RepositoryNotFound:
print("Template repository not found")
except CookiecutterException as e:
print(f"Cookiecutter error: {e}")# Basic usage
cookiecutter template-path
# With extra context
cookiecutter template-path --no-input project_name='My Project' author='Jane Doe'
# Advanced options
cookiecutter gh:user/template \
--checkout v2.0 \
--output-dir ./projects \
--overwrite-if-exists \
--accept-hooks no
# List installed templates
cookiecutter --list-installed
# Replay previous session
cookiecutter template-path --replayInstall with Tessl CLI
npx tessl i tessl/pypi-cookiecutter