harbor_cli.utils.commands
Attributes
PREFIX_ID = 'id:'
module-attribute
OPTION_QUERY = typer.Option(None, '--query', help='Query parameters to filter the results.')
module-attribute
OPTION_SORT = typer.Option(None, '--sort', help="Sorting order of the results. Example: [green]'name,-id'[/] to sort by name ascending and id descending.")
module-attribute
OPTION_PAGE_SIZE = typer.Option(10, '--page-size', help='(Advanced) Results to fetch per API call.')
module-attribute
OPTION_PAGE = typer.Option(1, '--page', help='(Advanced) Page to begin fetching from.')
module-attribute
OPTION_LIMIT = typer.Option(None, '--limit', help='Maximum number of results to fetch.')
module-attribute
OPTION_PROJECT_NAME_OR_ID = typer.Option(None, '--project', help=f'Project name or ID. {_USE_ID_HELP}')
module-attribute
OPTION_FORCE = typer.Option(False, '--force', help='Force deletion without confirmation.')
module-attribute
ARG_PROJECT_NAME = typer.Argument(None, help='Name of the project to use.')
module-attribute
ARG_PROJECT_NAME_OR_ID = _arg_project_name_or_id()
module-attribute
ARG_PROJECT_NAME_OR_ID_OPTIONAL = _arg_project_name_or_id(None)
module-attribute
ARG_REPO_NAME = typer.Argument(help='Name of the repository to use.')
module-attribute
ARG_USERNAME_OR_ID = typer.Argument(help=f'Username or ID of the user to use. {_USE_ID_HELP}')
module-attribute
ARG_LDAP_GROUP_DN_OR_ID = typer.Argument(help=f'LDAP Group DN or ID of the group to use. {_USE_ID_HELP}')
module-attribute
Classes
Functions
get_parent_ctx(ctx: typer.Context | click.core.Context) -> typer.Context | click.core.Context
Get the top-level parent context of a context.
get_command_help(command: typer.models.CommandInfo) -> str
Get the help text of a command.
Source code in harbor_cli/utils/commands.py
get_app_commands(app: typer.Typer) -> list[CommandSummary]
cached
get_app_callback_options(app: typer.Typer) -> list[typer.models.OptionInfo]
Get the options of the main callback of a Typer app.
Source code in harbor_cli/utils/commands.py
inject_help(model: Type[BaseModel], strict: bool = False, remove: Optional[List[str]] = None, **field_additions: str) -> Any
Injects a Pydantic model's field descriptions into the help attributes of Typer.Option() function parameters whose names match the field names.
Examples:
class MyModel(BaseModel):
my_field: str = Field(..., description="Description of my_field")
@app.command(name="my-command")
@inject_help(MyModel)
def my_command(my_field: str = typer.Option(...)):
...
# `my-app my-command --help`
# my_field's help text will be "Description of my_field"
NOTE
Does not modify the help text of options with existing help text!
Use the **field_additions
parameter to add additional help text to a field
in addition to the field's description. This text is appended to the
help text, separated by a space.
e.g. @inject_help(MyModel, my_field="Additional help text that is appended to the field's description.")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[BaseModel]
|
The pydantic model to use for help injection. |
required |
strict |
bool
|
If True, fail if a field in the model does not correspond to a function parameter of the same name with a typer.OptionInfo as a default value. |
False
|
remove |
Optional[List[str]]
|
List of strings to remove from descriptions before injecting them. |
None
|
**field_additions |
str
|
Additional help text to add to the help attribute of a field. The parameter name should be the name of the field, and the value should be the additional help text to add. This is useful when the field's description is not sufficient, and you want to add additional help text to supplement the existing description. |
{}
|
Source code in harbor_cli/utils/commands.py
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 |
|
inject_resource_options(f: Any = None, *, strict: bool = False, use_defaults: bool = True) -> Any
Decorator that calls inject_query, inject_sort, inject_page_size, inject_page and inject_limit to inject typer.Option() defaults for common options used when querying multiple resources.
NOTE: needs to be specified BEFORE @app.command() in order to work!
Not strict by default, so that it can be used on functions that only have a subset of the parameters (e.g. only query and sort).
The decorated function should always declare the parameters in the following order
if the parameters don't have defaults:
query
, sort
, page
, page_size
, limit
Examples:
@app.command()
@inject_resource_options()
def my_command(query: str, sort: str, page: int, page_size: int, limit: Optional[int]):
...
# OK
@app.command()
@inject_resource_options()
def my_command(query: str, sort: str):
...
# NOT OK (missing all required parameters)
@app.command()
@inject_resource_options(strict=True)
def my_command(query: str, sort: str):
...
# OK (inherits defaults)
@app.command()
@inject_resource_options()
def my_command(query: str, sort: str, page: int = typer.Option(1)):
...
# NOT OK (syntax error [non-default param after param with default])
# Use ellipsis to specify unset defaults
@app.command()
@inject_resource_options()
def my_command(query: str = typer.Option("tag=latest"), sort: str, page: int):
# OK (inherit default query, but override others)
# Use ellipsis to specify unset defaults
@app.command()
@inject_resource_options()
def my_command(query: str = typer.Option("my-query"), sort: str = ..., page: int = ...):
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Any
|
The function to decorate, by default None |
None
|
strict |
bool
|
If True, fail if function is missing any of the injected parameters, by default False
E.g. all of |
False
|
use_defaults |
bool
|
If True, use the default value specified by a parameter's typer.Option() field as the default value for the parameter, by default True. |
True
|
Returns:
Type | Description |
---|---|
Any
|
The decorated function |
Examples:
If use_defaults is True, the default value of page_size will be 20, instead of 10, which is the value inject_page_size() would use by default.Warning
inject_resource_options()
only accepts parameter defaults specified with typer.Option() and typer.Argument()!
@inject_resource_options(use_default=True)
my_func(page_size: int = 20) -> None: ... # will fail (for now)
Source code in harbor_cli/utils/commands.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 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 |
|