Skip to content

Commit 1f3f27b

Browse files
committed
docs(cmd): Controlling invocation
1 parent aeee2e3 commit 1f3f27b

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

0 commit comments

Comments
 (0)