CommandContext
The CommandContext
captures the output of shell commands.
It can be created using the capsula.CommandContext.builder
method or the capsula.CommandContext.__init__
method.
capsula.CommandContext.builder
classmethod
builder(
command: str,
*,
cwd: Path | str | None = None,
check: bool = True,
abort_on_error: bool = True,
cwd_relative_to_project_root: bool = False,
shell: bool = True
) -> Callable[[CapsuleParams], CommandContext]
PARAMETER | DESCRIPTION |
---|---|
command |
Command to run
TYPE:
|
cwd |
Working directory for the command, passed to the
TYPE:
|
check |
Whether to raise an exception if the command returns a non-zero exit code, passed to the
TYPE:
|
abort_on_error |
Whether to abort the encapsulation if the command returns a non-zero exit code
TYPE:
|
cwd_relative_to_project_root |
Whether
TYPE:
|
shell |
Whether to run the command using the shell. If True, the command will be run using the shell. If False, the command will be run directly. For more information, see the
TYPE:
|
Source code in capsula/_context/_command.py
capsula.CommandContext.__init__
__init__(
command: str,
*,
cwd: Path | None = None,
check: bool = True,
abort_on_error: bool = True,
shell: bool = True
)
Initialize the command context.
PARAMETER | DESCRIPTION |
---|---|
command |
TYPE:
|
cwd |
TYPE:
|
check |
TYPE:
|
abort_on_error |
TYPE:
|
shell |
TYPE:
|
Source code in capsula/_context/_command.py
Configuration example
Via capsula.toml
[pre-run]
contexts = [
{ type = "CommandContext", command = "uv lock --locked", cwd = ".", cwd_relative_to_project_root = true },
]
Via @capsula.context
decorator
import capsula
PROJECT_ROOT = capsula.search_for_project_root(__file__)
@capsula.run()
@capsula.context(capsula.CommandContext("uv lock --locked", cwd=PROJECT_ROOT), mode="pre")
def func(): ...
Output example
The following is an example of the output of the CommandContext
, reported by the JsonDumpReporter
: