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
) -> 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:
|
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
)
Initialize the command context.
PARAMETER | DESCRIPTION |
---|---|
command |
TYPE:
|
cwd |
TYPE:
|
check |
TYPE:
|
abort_on_error |
TYPE:
|
Source code in capsula/_context/_command.py
Configuration example
Via capsula.toml
[pre-run]
contexts = [
{ type = "CommandContext", command = "poetry check --lock", 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("poetry check --lock", cwd=PROJECT_ROOT), mode="pre")
def func(): ...
Output example
The following is an example of the output of the CommandContext
, reported by the JsonDumpReporter
: