Run a shell command with optional output capture and verbosity. Args: cmd (str | list): The command to run. cwd (str): The current working directory. verbose (bool): If True, print the command being run. **kwargs: Extra arguments passed to subprocess.run().
(cmd, verbose=False, **kwargs)
| 27 | |
| 28 | |
| 29 | def run(cmd, verbose=False, **kwargs): |
| 30 | """ |
| 31 | Run a shell command with optional output capture and verbosity. |
| 32 | |
| 33 | Args: |
| 34 | cmd (str | list): The command to run. |
| 35 | cwd (str): The current working directory. |
| 36 | verbose (bool): If True, print the command being run. |
| 37 | **kwargs: Extra arguments passed to subprocess.run(). |
| 38 | |
| 39 | Returns: |
| 40 | str | None: If capture is True, returns the command's stdout as a string; otherwise None. |
| 41 | """ |
| 42 | if verbose: |
| 43 | print(cmd if isinstance(cmd, str) else shlex.join(cmd)) |
| 44 | |
| 45 | result = subprocess.run(cmd, |
| 46 | shell=isinstance(cmd, str), |
| 47 | check=True, |
| 48 | capture_output=True, |
| 49 | text=True, |
| 50 | **kwargs).stdout.strip() |
| 51 | |
| 52 | if verbose: |
| 53 | print(result) |
| 54 | |
| 55 | return result |
| 56 | |
| 57 | |
| 58 | def get_top(): |
no test coverage detected