Runner

2.1.2

Swift helper for launching subprocesses and capturing their output.
elegantchaos/Runner

What's New

2.1.2

2024-09-10T13:29:21Z

Slightly cleaner API.

Runner

Support for executing subprocesses, using Foundation.Process, and capturing their output asynchronously. Swift 6 ready.

Usage examples:

Run And Capture Stdout

let url = /* url to the executable */
let runner = Runner(for: url)

// execute with some arguments
let session = runner.run(["some", "arguments"])

// process the output asynchronously
for await l in result.stdout.lines {
  print(l)
}

Run In A Different Working Directory

// run in a different working directory
runner.cwd = /* url to the directory */
let _ = runner.run(["blah"])

// transfer execution to the subprocess
runner.exec(url)

Lookup Executable In Path

let runner = Runner(command: "git") /// we'll find git in $PATH if it's there
let session = runner.run("status")
print(await session.stdout.string)

Run And Wait For Termination

let url = /* url to the executable */
let runner = Runner(for: url)

// execute with some arguments
let session = runner.run(["some", "arguments"])

// wait for termination and read state
if await session.waitUntilExit() == .succeeded {
  print("all good")
}

Run Passing Stdout/Stderr Through

let url = /* url to the executable */
let runner = Runner(for: url)
let session = runner.run(stdoutMode: .forward, stderrMode: .forward)
let _ = session.waitUntilExit()

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

Last updated: Tue May 13 2025 16:27:38 GMT-0900 (Hawaii-Aleutian Daylight Time)