1 2 3 4 5 6 7 8 9 10 11 12 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 39 40 41 42 43 44 45 46 47 48 49
|
"""Utility functions for working with CLI commands."""
from __future__ import annotations
from typing import TYPE_CHECKING
import typer
import typer.core
from zabbix_cli.exceptions import ZabbixCLIError
if TYPE_CHECKING:
import click
def get_parent_ctx(
ctx: typer.Context | click.core.Context,
) -> typer.Context | click.core.Context:
"""Get the top-level parent context of a context."""
if ctx.parent is None:
return ctx
return get_parent_ctx(ctx.parent)
def get_command_help(command: typer.models.CommandInfo) -> str:
"""Get the help text of a command."""
if command.help:
return command.help
if command.callback and command.callback.__doc__:
lines = command.callback.__doc__.strip().splitlines()
if lines:
return lines[0]
if command.short_help:
return command.short_help
return ""
def get_command_by_name(ctx: typer.Context, name: str) -> click.core.Command:
"""Get a CLI command given its name."""
if not isinstance(ctx.command, typer.core.TyperGroup):
# NOTE: ideally we shouldn't leak this error to the user, but
# can this even happen? Isn't it always a command group?
raise ZabbixCLIError(f"Command {ctx.command.name} is not a command group.")
if not ctx.command.commands:
raise ZabbixCLIError(f"Command group {ctx.command.name} has no commands.")
command = ctx.command.commands.get(name)
if not command:
raise ZabbixCLIError(f"Command {name} not found.")
return command
|