Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ options:
--verbose Display debug information for each request
```

Set `OPENCAGE_GEOCODING_API_KEY` to avoid passing your API key on the command
line. A supplied `--api-key` takes precedence over the environment variable.

<img src="batch-progress.gif"/>

See [`examples/addresses.csv`](examples/addresses.csv) for sample input.
Expand Down
8 changes: 7 additions & 1 deletion opencage_cli/command_line.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import sys
import io
import os
from pathlib import Path
import re
import csv
Expand Down Expand Up @@ -52,7 +53,12 @@ def parse_args(args):
'reverse', help="Reverse geocode a file (input is coordinates, add full address)")

for subparser in [subparser_forward, subparser_reverse]:
subparser.add_argument("--api-key", required=True, type=api_key_type, help="Your OpenCage API key")
subparser.add_argument(
"--api-key",
default=os.environ.get("OPENCAGE_GEOCODING_API_KEY"),
required="OPENCAGE_GEOCODING_API_KEY" not in os.environ,
type=api_key_type,
help="Your OpenCage API key (or OPENCAGE_GEOCODING_API_KEY environment variable)")
subparser.add_argument(
"--input",
required=True,
Expand Down
25 changes: 25 additions & 0 deletions test/cli/test_cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ def test_invalid_api_key(capfd):
)


def test_api_key_from_environment(monkeypatch):
monkeypatch.setenv("OPENCAGE_GEOCODING_API_KEY", "oc_gc_12345678901234567890123456789012")

args = parse_args([
"forward",
"--input", "test/fixtures/input.txt",
"--output", "test/fixtures/output.csv"
])

assert args.api_key == "oc_gc_12345678901234567890123456789012"


def test_command_line_api_key_overrides_environment(monkeypatch):
monkeypatch.setenv("OPENCAGE_GEOCODING_API_KEY", "oc_gc_12345678901234567890123456789012")

args = parse_args([
"forward",
"--api-key", "12345678901234567890123456789012",
"--input", "test/fixtures/input.txt",
"--output", "test/fixtures/output.csv"
])

assert args.api_key == "12345678901234567890123456789012"


def test_existing_output_file(capfd):
assert_parse_args_error(
[
Expand Down