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