aeat.core.click_context module

Shared click context helpers safe to import from any layer.

The helpers walk the active click.Context chain to surface root-level CLI flags (e.g. --json) without forcing callers to thread the context object explicitly. They live in core so domain code can call json_output_requested(), current_cli_flag(), or context_chain_requests_json() without inverting the dependency direction onto entrypoints.cli. Terminal error handlers that have no live context can fall back to argv_requests_json().

This module answers output-mode questions only. It is not an active-profile, bucket-session, or command-dispatch state source; those boundaries remain in the CLI bootstrap and storage/session helpers that own them.

current_cli_flag(name)[source]

Return the boolean value of name from any ancestor context’s obj dict.

Walks the click.Context parent chain looking for the first ctx.obj mapping that carries name; the truthiness of the associated value is returned. Returns False when no context is active or no ancestor carries the flag. This helper intentionally reads ctx.obj only; option values stored in ctx.params are handled by json_output_requested().

Parameters:

name (str) – Key to look up inside each ancestor’s ctx.obj dict.

Return type:

bool

Returns:

True when the nearest ancestor binding for name is truthy; False otherwise.

json_output_requested()[source]

Return True when the active context requests JSON output.

Checks every recognised JSON flag spelling — --json, --as-json, --json-out, --json-output — across the Click context chain. It also recognises the root AEAT --format json option, including Typer’s Python-facing format_ parameter name. The variant tolerance lets callers register the flag under whichever Python parameter name fits their command without breaking the output-mode probe. For terminal handlers without a live current context, use context_chain_requests_json() when an exception carries a context or argv_requests_json() as the last-resort argv parser.

Return type:

bool

context_chain_requests_json(ctx)[source]

Return True when ctx or any ancestor requests JSON output.

Companion to json_output_requested() for call sites where the context stack is already unwound (e.g. the root group’s terminal exception handler, where a click.UsageError carries its parse-time context on exc.ctx but no context is “current”). Accepts either Click runtime’s context object structurally.

Parameters:

ctx (object) – Upstream or Typer-vendored Click context, or any object exposing compatible params, obj, and parent attributes.

Return type:

bool

argv_requests_json(argv)[source]

Return True when raw argv tokens request JSON output.

Last-resort probe for terminal handlers that have neither an active context stack nor an exception-carried context (e.g. a crash before or outside command dispatch). Recognises --format json, --format=json, and the --json flag family. It is syntactic only: command parsing and value validation still belong to Click/Typer.

Return type:

bool

Parameters:

argv (Iterable[str])