Skip to content

Commit 7dd0b76

Browse files
committed
docs(cmd): Controlling invocation
1 parent b34606c commit 7dd0b76

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/cmd/index.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,58 @@ hg
2121
svn
2222
core
2323
```
24+
25+
## Controlling commands
26+
27+
### Override `run()`
28+
29+
You want to control `stdout`, `stderr`, terminal output, tee'ing or logging, introspect and modify
30+
the commands themselves. libvcs is designed to make this trivial to control.
31+
32+
- Git -> `Git.<command>` -> `Git.run` -> `run`
33+
34+
You override `Git.run` method, and all `Git` commands can be intercepted.
35+
36+
```python
37+
38+
class MyGit(Git):
39+
def run(self, *args, **kwargs):
40+
return ...
41+
```
42+
43+
You can also pass-through using `super()`
44+
45+
```python
46+
47+
class MyGit(Git):
48+
def run(self, *args, **kwargs):
49+
return super().run(*args, **kwargs)
50+
```
51+
52+
Two possibilities:
53+
54+
1. Modify args / kwargs before running them
55+
2. Replace `run()` with a different subprocess runner
56+
57+
### `LazySubprocessMixin`
58+
59+
```python
60+
61+
class MyGit(Git, LazySubprocessMixin):
62+
def run(self, *args, **kwargs):
63+
return ...
64+
```
65+
66+
You can introspect it here.
67+
68+
Instead of `git.run(...)` you'd do `git.run(...).run()`.
69+
70+
Also, you can introspect and modify the output before execution
71+
72+
```python
73+
>>> mycmd = git.run(...)
74+
>>> mycmd.flags
75+
...
76+
>>> mycmd.flags = '--help'
77+
>>> mycmd.run()
78+
```

0 commit comments

Comments
 (0)