Nim: Typed environment config with validation in Nim

Nim
import std/[os, strutils]

proc getIntEnv(name: string; fallback: int; lo, hi: int): int =
  let raw = getEnv(name, $fallback).strip()
  try:
    result = parseInt(raw)
  except ValueError:
    raise newException(ValueError,
      name & " must be an integer, got '" & raw & "'")
  if result < lo or result > hi:
    raise newException(ValueError,
      name & " must be in " & $lo & ".." & $hi & ", got " & $result)

let port = getIntEnv("PORT", 8080, 1, 65535)
let workers = getIntEnv("WORKERS", 4, 1, 64)
echo "port=", port, " workers=", workers