Environment variables¶
A KliamkaArg can name an environment variable used when the command line does not supply that field.
from kliamka import KliamkaArg, KliamkaArgClass
class Args(KliamkaArgClass):
endpoint: str = KliamkaArg(
"--endpoint",
"Service endpoint",
default="https://api.example.com",
env="APP_ENDPOINT",
)
timeout: int = KliamkaArg(
"--timeout",
"Request timeout in seconds",
default=30,
env="APP_TIMEOUT",
)
Generated help includes an [env: NAME] hint.
Resolution order¶
Kliamka resolves every field independently:
- CLI — an explicitly supplied option or positional always wins;
- environment — used only when the CLI omitted the field;
- default — the descriptor default, then the type-specific fallback.
APP_TIMEOUT=60 app # timeout is 60
APP_TIMEOUT=60 app --timeout 5 # timeout is 5
app # timeout is 30
A CLI value still wins when it happens to equal the declared default.
Empty values¶
An environment variable counts as present even when its value is empty. For a string, APP_ENDPOINT= therefore produces "" rather than falling back to the descriptor default. Use Pydantic validation if an empty value is invalid for your application.
Boolean values¶
Environment booleans are stripped, case-insensitive, and intentionally strict.
| True | False |
|---|---|
true |
false |
1 |
0 |
yes |
no |
on |
off |
Other spellings fail with a KliamkaError that names the environment variable. CLI booleans remain presence flags and do not accept these string values.
List values¶
Environment lists use comma-separated items:
class Args(KliamkaArgClass):
ports: list[int] = KliamkaArg(
"--ports", "Ports to bind", env="APP_PORTS"
)
Whitespace around each item is removed. An empty environment value produces an empty list. Comma escaping and CSV quoting are not supported.
Enums and custom types¶
Environment values use the same enum and converter resolution as command-line tokens:
from pathlib import Path
class Args(KliamkaArgClass):
config: Path = KliamkaArg(
"--config",
"Configuration file",
env="APP_CONFIG",
converter=Path,
)
For list fields, conversion is applied to each comma-separated item. See Converters and validation for precedence details.
Errors¶
Conversion errors are raised from from_args() as KliamkaError and identify the source variable:
When parsing through @kliamka_cli or @kliamka_subcommands, that message is rendered as a standard CLI error without an application traceback.
Operational guidance¶
- Treat environment names as part of your CLI's public configuration contract.
- Use application-specific prefixes to avoid collisions.
- Do not place secret values in help text, logs, defaults, or error messages.
- Validate URLs, ranges, paths, and required non-empty strings with Pydantic.
- In tests, isolate environment changes with fixtures such as pytest's
monkeypatch.