Decorators¶
The four main decorators are the public surface of pyclif. They wrap Click objects with framework features: automatic configuration, global option propagation, Rich logging, and standardized response handling.
app_group¶
Entry point decorator. Creates the root CLI group with all framework features enabled.
pyclif.app_group(**kwargs)
¶
Decorator for the main CLI application entry point.
Enables all automatic features (config, logging, version, etc.) by default. Options like --verbosity will be propagated to all subcommands.
All keyword arguments map to GroupConfig fields or are forwarded to Click.
Notable options:
handle_response(bool): intercept and printResponseobjects automatically.timer(bool): inject--time/--no-time. Prints elapsed time in rich/table/raw; injectsexecution_timeandexecution_time_strintoResponse.datain json/yaml.output_format_default(str): default for--output-format(json, yaml, table, rich, raw, text).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
GroupConfig fields or Click group arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., Any]], Group]
|
A decorator that wraps the function as a pyclif CLI group. |
Source code in src/pyclif/core/decorators.py
group¶
Creates a subgroup that inherits global options from its parent.
pyclif.group(**kwargs)
¶
Decorator for CLI subgroups.
Creates a standard group without global application options by default.
Source code in src/pyclif/core/decorators.py
command¶
Creates a CLI command. Use inside a group or app_group.
pyclif.command(name=None, handle_response=False, **kwargs)
¶
Create a Click command with optional automatic response handling.
When handle_response=True, any Response returned by the command function is automatically printed using the output format resolved from ctx.meta (--output-format option). This is equivalent to manually applying the returns_response decorator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Name of the command. |
None
|
handle_response
|
bool
|
If True, wrap the function with returns_response. |
False
|
**kwargs
|
Any
|
Additional arguments passed to click_extra.command(). |
{}
|
Returns:
| Type | Description |
|---|---|
Callable[[_F], Command]
|
Decorated function as a Click command. |
Source code in src/pyclif/core/decorators.py
option¶
Extends Click options with environment variable binding and optional global propagation.
pyclif.option(*param_decls, is_global=False, show_envvar=True, store_in_meta=False, **kwargs)
¶
Create a Click option with global propagation support.
Ensures a consistent environment variable display and allows options to be marked as global to be available on all subcommands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*param_decls
|
str
|
Parameter declarations for the option. |
()
|
is_global
|
bool
|
If True, the option is propagated to all subcommands. |
False
|
show_envvar
|
bool
|
Show environment variables in the help output. |
True
|
store_in_meta
|
bool
|
If True, stores the option value in ctx.meta automatically. |
False
|
**kwargs
|
Any
|
Additional arguments passed to click_extra.option(). |
{}
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable], Callable]
|
Option decorator function. |
Source code in src/pyclif/core/decorators.py
output_filter_option¶
Adds --output-format to a command (JSON, YAML, Table, Rich, Raw).
pyclif.output_filter_option(*param_decls, show_envvar=True, **kwargs)
¶
Add an output filter option to a command.
When combined with --output-format raw, json, or yaml, this option lets users extract a value from the Response data using a dotted key path.
The selected path is stored in ctx.meta['pyclif.output_filter'] and is automatically picked up by returns_response.
Numeric path segments are treated as list indices. Resolution order: data["data"] first, then top-level response fields.
Example:
@app.command()
@output_filter_option()
@returns_response
@click.pass_context
def articles(ctx):
return Response(
success=True,
message="2 articles",
data={"results": [{"id": 1, "title": "Hello"}, {"id": 2}]},
)
# myapp articles -o raw -f results.0.title -> Hello
# myapp articles -o json -f results.0 -> {"id": 1, "title": "Hello"}
# myapp articles -o raw -f message -> 2 articles
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*param_decls
|
str
|
Parameter declarations (default: --output-filter, -f). |
()
|
show_envvar
|
bool
|
Show environment variables in the help output. |
True
|
**kwargs
|
Any
|
Additional arguments passed to the option decorator. |
{}
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable], Callable]
|
The decorated function. |
Source code in src/pyclif/core/decorators.py
returns_response¶
Decorator that intercepts a Response return value and dispatches it to the formatter.
Applied automatically when handle_response=True on @app_group.
pyclif.returns_response(f)
¶
Decorator that intercepts a Response return value and prints it automatically.
When the decorated command function returns a Response instance, this decorator reads the output format stored in ctx.meta['pyclif.output_format'] (set by the --output-format option) and dispatches printing via BaseContext. Non-Response return values are left untouched.
Example:
@app.command()
@returns_response
@option("--name", default="world")
@click.pass_context
def hello(ctx, name):
return Response(success=True, message=f"Hello {name}", data={"name": name})
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
The command function to wrap. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
The wrapped function. |
Source code in src/pyclif/core/decorators.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |