Skip to main content

cli.exit_code()

The exit code for one of this package’s exceptions, or None for any other.

Usage

Source

cli.exit_code(exc)

This is part of the host contract with spice_kernel_db.cli.app and logging_to_stderr. A program with its own Typer CLI mounts skd’s command tree as one of its groups with app.add_typer(spice_kernel_db.cli.app, name=...). This is what planetarypy does as plp spice. The host catches what escapes its Typer app and asks here, so both front doors exit the same way. main() uses it too (#45, #49; planetarypy #51). These three names change only with a major version. The rest of spice_kernel_db.cli is internal.

The mapping:

Parameters

exc: BaseException
The exception that escaped the app.

Returns

int | None
The exit code, or None when the exception has no mapping.

Examples

>>> import sys
>>> from spice_kernel_db.cli import exit_code, logging_to_stderr
>>> try:
...     with logging_to_stderr():
...         app(standalone_mode=False)  # the host's Typer app
... except Exception as exc:
...     code = exit_code(exc)
...     if code is None:
...         raise
...     print(exc, file=sys.stderr)
...     sys.exit(code)