Mixins¶
Feature mixins used internally by pyclif's group and context classes. Exposed publicly for advanced subclassing.
GlobalOptionsMixin¶
Propagates options marked is_global=True from a parent group to all child commands
and subgroups at invocation time.
pyclif.GlobalOptionsMixin
¶
Mixin that propagates global options to subcommands.
Source code in src/pyclif/core/mixins/cli.py
add_command(cmd, name=None, **kwargs)
¶
Register a subcommand and inject global options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
Command
|
The command to add. |
required |
name
|
str | None
|
The name to register the command with. |
None
|
**kwargs
|
Any
|
Additional arguments passed to the parent method. |
{}
|
Source code in src/pyclif/core/mixins/cli.py
HandleResponseMixin¶
Auto-wraps commands added via command() or add_command() with returns_response
when handle_response=True.
pyclif.HandleResponseMixin
¶
Mixin that adds automatic Response dispatch to Click groups.
When handle_response_by_default is True (set via @app_group(handle_response=True)), every command registered through @group.command() or group.add_command() automatically wraps its return value: if the function returns a Response, it is printed using the output format stored in ctx.meta['pyclif.output_format'].
The per-command handle_response kwarg always takes precedence over the group-level default.
Source code in src/pyclif/core/mixins/response.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
command(*args, handle_response=None, **kwargs)
¶
Override Click.Group.command to automatically handle Response objects.
Source code in src/pyclif/core/mixins/response.py
add_command(cmd, name=None)
¶
Override Click.Group.add_command to propagate response handling.
When handle_response_by_default is True: - Sub-groups: handle_response_by_default is propagated recursively so that all already-registered leaf commands are wrapped and future registrations on those groups are also covered. - Leaf commands: the callback is wrapped with returns_response directly.
Commands whose callback carries the _PYCLIF_RESPONSE_DECIDED flag (set by the @group.command() factory path before Click registers the command) are left untouched so that explicit handle_response=False overrides and already-wrapped functions are both respected.
Source code in src/pyclif/core/mixins/response.py
OutputFormatMixin¶
Dispatches Response objects to the appropriate formatter (JSON / YAML / Table / Rich / Raw)
based on --output-format stored in ctx.meta.
pyclif.OutputFormatMixin
¶
Provide methods for printing error messages and results based on a specified format.
This mixin expects the inheriting class to have 'console' and 'output_format' attributes. Every result must be a Response with a renderer attached.
Source code in src/pyclif/core/mixins/output.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
print_error_based_on_format(exception)
¶
Format and print an unhandled exception as a structured Response.
Creates a single-result Response from the exception and dispatches it through the normal renderer path using _ExceptionRenderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
Exception
|
The exception to display. |
required |
Source code in src/pyclif/core/mixins/output.py
print_result_based_on_format(result, options=None)
¶
Print a Response using its renderer.
Streaming responses (data["stream"] present) are handled via the Live context for rich output, or materialized first for all other formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Response
|
The Response to print. Must have a renderer attached. |
required |
options
|
dict | None
|
Optional dict with the filter_value key for --output-filter support. |
None
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When result.renderer is None — a programming error. |
Source code in src/pyclif/core/mixins/output.py
RichHelpersMixin¶
Rich console helpers available on the context: panels, rules, status spinners, prompts.
pyclif.RichHelpersMixin
¶
Provide rich console helper methods for CLI contexts.
This mixin expects the inheriting class to have a 'console' attribute instantiated with a rich.console.Console object.
Source code in src/pyclif/core/mixins/rich.py
rich_panel(text, title=None, border_style=None, padding=(0, 1), console_print=False, fit=False)
¶
Create a rich panel with the given formatting options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
Any
|
The text content or renderable of the panel. |
required |
title
|
str | None
|
The title of the panel. |
None
|
border_style
|
str | None
|
The border style of the panel. |
None
|
padding
|
tuple
|
The padding of the panel (top/bottom, left/right). |
(0, 1)
|
console_print
|
bool
|
If True, prints the panel directly to the console. |
False
|
fit
|
bool
|
If True, the panel fits its contents. |
False
|
Returns:
| Type | Description |
|---|---|
Panel
|
The rendered rich panel object. |
Source code in src/pyclif/core/mixins/rich.py
display_rule(title='', style='blue')
¶
Display a horizontal rule with an optional title.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The text to display in the middle of the rule. |
''
|
style
|
str
|
The color and style of the rule. |
'blue'
|
Source code in src/pyclif/core/mixins/rich.py
show_status(message='Processing...', spinner='dots')
¶
Return a rich status context manager for long-running tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The message to display next to the spinner. |
'Processing...'
|
spinner
|
str
|
The type of spinner animation to use. |
'dots'
|
Returns:
| Name | Type | Description |
|---|---|---|
Status |
Status
|
A context manager for the status. |
Source code in src/pyclif/core/mixins/rich.py
ask_user(question, default=None, choices=None, password=False)
¶
Prompt the user for input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The prompt text to display. |
required |
default
|
str | None
|
The default value if the user presses enter. |
None
|
choices
|
list[str] | None
|
A list of valid choices to restrict input. |
None
|
password
|
bool
|
If True, hides the user's input. |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
The user's input. |
Source code in src/pyclif/core/mixins/rich.py
ask_confirmation(question, default=False)
¶
Ask the user a yes or no confirmation question.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The prompt text to display. |
required |
default
|
bool
|
The default answer if the user presses enter. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the user confirmed, False otherwise. |