From c7ed67c8ec7cb1504507e346d378e703c323f75b Mon Sep 17 00:00:00 2001 From: Flo Eibeck Date: Thu, 2 Jul 2026 09:07:58 +0200 Subject: [PATCH] Support loading ANTHROPIC_API_KEY from a .env file python-dotenv was already a declared dependency but was never wired up. Call load_dotenv() at CLI startup so the key (and other vars) can live in a git-ignored .env file instead of requiring a shell export. Shell env still takes precedence. Documents the option in the README. Co-Authored-By: Claude Opus 4.8 --- README.md | 12 ++++++++++-- src/rpg_explainer/cli.py | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aab2930..372fb6d 100644 --- a/README.md +++ b/README.md @@ -54,11 +54,19 @@ See [examples.md](examples.md) for complete analysis examples. pip install -e . ``` -4. Set your Anthropic API key: +4. Set your Anthropic API key, either as a shell environment variable: ```bash export ANTHROPIC_API_KEY=your-api-key-here ``` + ...or in a `.env` file in the project root (loaded automatically on startup): + ```bash + echo "ANTHROPIC_API_KEY=your-api-key-here" > .env + ``` + + Variables already set in the shell environment take precedence over the + `.env` file. The `.env` file is git-ignored, so your key won't be committed. + ## Usage Analyze one or more RPG source files: @@ -160,7 +168,7 @@ pytest --cov=rpg_explainer | Variable | Description | Required | |----------|-------------|----------| -| `ANTHROPIC_API_KEY` | Your Anthropic API key | Yes | +| `ANTHROPIC_API_KEY` | Your Anthropic API key (via shell export or a `.env` file) | Yes | | `RPG_TREESITTER_LIB` | Custom path to compiled Tree-sitter library | No (defaults to `build/languages.so`) | ## How It Works diff --git a/src/rpg_explainer/cli.py b/src/rpg_explainer/cli.py index 17d4411..36b0d9b 100644 --- a/src/rpg_explainer/cli.py +++ b/src/rpg_explainer/cli.py @@ -6,6 +6,7 @@ from pathlib import Path import click +from dotenv import load_dotenv from . import __version__ @@ -78,6 +79,10 @@ def main( rpg-explain program.rpgle --json > index.json """ + # Load variables from a .env file (e.g. ANTHROPIC_API_KEY) if present. + # Values already set in the environment take precedence and are not overridden. + load_dotenv() + # Check if Tree-sitter library is built if not check_treesitter_library(): print_error("Tree-sitter RPG library not found.")