Skip to content

feat: generate template from .env file#630

Open
amahuli03 wants to merge 4 commits intotheskumar:mainfrom
amahuli03:618/generate-template-from-env-file
Open

feat: generate template from .env file#630
amahuli03 wants to merge 4 commits intotheskumar:mainfrom
amahuli03:618/generate-template-from-env-file

Conversation

@amahuli03
Copy link

@amahuli03 amahuli03 commented Mar 12, 2026

Summary

Adds a dotenv template CLI command and generate_template() Python API to generate environment template files from existing .env files. Closes #618.

  • Values are replaced with key names (e.g., SECRET_KEY=abc123SECRET_KEY=SECRET_KEY)
  • Comments, blank lines, and export prefixes are preserved
  • Supports inline directives:
    • # ::dotenv-template-preserve — keeps the line's value as-is (useful for non-sensitive defaults)
    • # ::dotenv-template-exclude — omits the line from the template
  • Directives are stripped from output by default; --keep-directives flag retains them
  • Output goes to stdout by default, or to a file via positional argument

Usage

Given a .env file:

# Print template to stdout
dotenv -f .env template

# Generate template file called `.env.template`, replacing and overwriting the file if it already exists
dotenv -f .env template .env.template

# Keep directive annotations in output
dotenv -f .env template --keep-directives

Python API

from dotenv import generate_template

template = generate_template(dotenv_path=".env")

Test plan

  • Run python -m pytest tests/test_main.py tests/test_cli.py -k template -v — 14 tests covering value replacement, directives, keep_directives flag, file output, empty input, and error handling
  • Manual test: create a .env with mixed directives and verify output matches expected template

Add core function for generating .env template files from
existing .env files. Values are replaced with key names by
default. Supports inline directives ::dotenv-template-preserve
and ::dotenv-template-exclude. Directives are stripped from
output by default, controllable via keep_directives parameter.
Add `dotenv template` subcommand to generate .env template
files. Supports optional output path argument and
--keep-directives flag. Fix _strip_directives to clean up
bare comment markers left after stripping.
Cover core value replacement, directives, keep_directives flag, file path input, and CLI subcommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: generate a template from an env file

1 participant