Proc: clean subprocess

Asyncio subprocess wrapper featuring:

  • Capture + live logging of stdout/stderr

  • ANSI escape code cleaning for captured output: print colored output for humans, have clean output in a variable for processing, log, cache… and sending to LLMs!

  • Separate start/wait methods for process control

Example usage:

# pass shell command in a string for convenience
proc = cli2.Proc('foo bar')

# or as list, better when building commands
proc = await cli2.Proc('foo', 'bar')

# wait in async loop
await proc.wait()

Note

There are also start functions, sync and async, in case you want to start the proc and wait later.

class cli2.proc.Proc(cmd, *args, quiet=False, inherit=True, timeout=None, cwd=None, **env)[source]

Asynchronous subprocess manager with advanced IO handling.

args

Full command arguments list used to launch the process

env

Dict of environment variables

cwd

Working directory path to run the command in

rc

Return Code: process exit code (available after process completes)

out

Combined cleaned output with ANSI escape codes removed.

out_ansi

Combined stdout/stderr output with ANSI codes preserved.

stdout

Cleaned stdout output with ANSI escape codes removed.

stderr

Cleaned stdout output with ANSI escape codes removed.

stdout_ansi

Stdout output with ANSI escape codes preserved.

stderr_ansi

Stderr output with ANSI escape codes preserved.

clone()[source]

Create a new unstarted Proc instance with identical configuration.

Returns:

New Proc instance ready for execution

property cmd

Get/set the command as a shell-joinable string.

Getter:

Returns shell-escaped command string

Setter:

Parses and updates internal args list

Type:

str

async start()[source]

Launch the subprocess asynchronously.

Returns:

Self reference for method chaining

Raises:

RuntimeError – If process is already started

async wait()[source]

Wait for process completion with timeout handling.

Terminates process if timeout occurs. Gathers all output streams.

Returns:

Self reference for method chaining