Reusable Click decorators for MADSci CLI commands.
Provides composable decorators that add common options (output format, service URL, timeout) and error-handling wrappers so individual commands don’t need to duplicate boilerplate.
Functions¶
output_options(f: F) ‑> ~FAdd
--json,--yaml, and-q/--quietoutput-format flags.These options are stored in
ctx.objso that :func:~madsci.client.cli.utils.output.determine_output_formatcan read them. They intentionally mirror the flags on the top-levelmadscigroup, but can be applied to individual sub-commands that need standalone format selection.Usage::
@cli.command() @output_options @click.pass_context def my_command(ctx): fmt = determine_output_format(ctx)resolve_service_url(ctx: click.Context, explicit_url: str | None, context_attr: str, default_port: int) ‑> strResolve a service URL from explicit option, context, or default.
Priority order:
explicit_url(from a CLI option) — highest precedence.The named attribute on the
MadsciContextstored inctx.obj["context"].http://localhost:<default_port>/— fallback default.
Args: ctx: Click context. explicit_url: URL provided via CLI option (takes precedence). context_attr: Attribute name on MadsciContext (e.g.
"workcell_server_url"). default_port: Default localhost port number.Returns: Resolved URL string with trailing slash.
service_url_option(service_name: str, envvar: str, default_port: int) ‑> Callable[[~F], ~F]Create a
--<service>-urlClick option with env-var fallback.Args: service_name: Hyphenated service name (e.g.
"event-manager"). envvar: Environment variable name for the URL (e.g."EVENT_SERVER_URL"). default_port: Default localhost port number.Returns: Click decorator that injects the URL as a keyword argument named
<service_name_underscored>_url.Usage::
@cli.command() @service_url_option("event-manager", "EVENT_SERVER_URL", 8001) def my_command(event_manager_url: str): ...timeout_option(default: float = 10.0) ‑> Callable[[~F], ~F]Add a
--timeoutoption (in seconds).Args: default: Default timeout value.
Returns: Click decorator that injects
timeoutas a keyword argument.with_service_error_handling(f: F) ‑> ~FCatch service and HTTP exceptions, print a friendly CLI message.
Translates :class:
MadsciServiceErrorand rawhttpxtransport exceptions into user-friendly error output and exits with code 1.Usage::
@cli.command() @with_service_error_handling def my_command(): ...