Skip to content

Context

Holds various state and I/O for commands.

Source code in nava/platform/cli/context.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@dataclass
class CliContext:
    """Holds various state and I/O for commands."""

    output_level: OutputLevel
    log: nava.platform.cli.logging.Logger
    console: nava.platform.cli.console.ConsoleWrapper
    fail_with_usage: Callable[[str], NoReturn]
    """Aborts the execution of the program with a specific error message and CLI help message."""
    exit: Callable[[int], NoReturn]
    """Exits the application with a given exit code."""
    exception_handler: Callable[["CliContext", BaseException], None]
    """Handle exceptions with this context object."""
    app_dirs: AppDirs = app_dirs

    def fail(self, message: str) -> NoReturn:
        """Aborts the execution of the program with a specific error message."""
        self.console.error.print(message)
        self.exit(1)

    @contextmanager
    def handle_exceptions(self) -> Generator[None, None, None]:
        try:
            yield
        except BaseException as e:
            self.exception_handler(self, e)

Handle exceptions with this context object.

Exits the application with a given exit code.

Aborts the execution of the program with a specific error message and CLI help message.

Aborts the execution of the program with a specific error message.

Source code in nava/platform/cli/context.py
28
29
30
31
def fail(self, message: str) -> NoReturn:
    """Aborts the execution of the program with a specific error message."""
    self.console.error.print(message)
    self.exit(1)