Skip to content

rich_click.rich_context

Classes

NameDescription
RichContextThe context of a rich-click command: a click Context that formats help and errors with Rich.
RichContextMixinRich help formatting for a click Context, as a mixin with no click base class.

Functions

NameDescription
get_current_contextReturn the context of the command that runs now.

RichContextclass#

class RichContext(RichContextMixin, click.Context)

Bases: RichContextMixin, click.Context

The context of a rich-click command: a click Context that formats help and errors with Rich.

Inherited from rich_click.rich_context.RichContextMixin

consoleattributeclass attributeinstance attribute#

console: Console | None = None

errors_in_output_formatattributeclass attributeinstance attribute#

errors_in_output_format: bool = False

export_console_asattributeclass attributeinstance attribute#

export_console_as: Literal['html', 'svg', 'text'] | None = None

formatter_classattributeclass attributeinstance attribute#

formatter_class: type[RichHelpFormatter] = RichHelpFormatter

help_configattributeinstance attribute#

help_config = parent.help_config

help_to_stderrattributeclass attributeinstance attribute#

help_to_stderr: bool = False

make_formattermethod#

def make_formatter(error_mode: bool = False) -> RichHelpFormatter

Create the Rich Help Formatter.

RichContextMixinclass#

class RichContextMixin(*args: Any, rich_console: Console | None = None, rich_help_config: Mapping[str, Any] | RichHelpConfiguration | None = None, export_console_as: Literal['html', 'svg', 'text'] | None = None, errors_in_output_format: bool | None = None, help_to_stderr: bool | None = None, **kwargs: Any)

Bases: _ContextBase

Rich help formatting for a click Context, as a mixin with no click base class.

RichContext combines this mixin with click.Context. A click fork combines it with its own Context class, such as asyncclick.Context. Because the mixin has no click base class, it does not add click’s synchronous methods to the fork.

Create a context. An argument that is not set takes its value from the parent context.

Parameters

NameTypeDefaultDescription
*argsAny()Positional arguments for the click Context.
rich_consoleConsole | NoneNoneRich console to print help and errors with.
rich_help_configMapping[str, Any] | RichHelpConfiguration | NoneNoneHow to format help and errors, as a RichHelpConfiguration or a dict of its fields. A dict changes only the fields it names.
export_console_asLiteral['html', 'svg', 'text'] | NoneNoneExport help as "html", "svg" or "text", not as terminal output.
errors_in_output_formatbool | NoneNoneExport errors in the export_console_as format too.
help_to_stderrbool | NoneNonePrint help to stderr, not stdout.
**kwargsAny{}Keyword arguments for the click Context.

consoleattributeclass attributeinstance attribute#

console: Console | None = None

errors_in_output_formatattributeclass attributeinstance attribute#

errors_in_output_format: bool = False

export_console_asattributeclass attributeinstance attribute#

export_console_as: Literal['html', 'svg', 'text'] | None = None

formatter_classattributeclass attributeinstance attribute#

formatter_class: type[RichHelpFormatter] = RichHelpFormatter

help_configattributeinstance attribute#

help_config = parent.help_config

help_to_stderrattributeclass attributeinstance attribute#

help_to_stderr: bool = False

make_formattermethod#

def make_formatter(error_mode: bool = False) -> RichHelpFormatter

Create the Rich Help Formatter.

get_current_contextfunction#

def get_current_context(silent: bool = False) -> RichContext | None

Return the context of the command that runs now.

Use it in helper functions that need the context but do not get it from the pass_context decorator.

Parameters

NameTypeDefaultDescription
silentboolFalseIf true, return None when no command runs. If false, raise a RuntimeError.

Returns

RichContext | None
The context, or None if no command runs and silent is true.