Swiftpack.co - Zollerboy1/SwiftCommand as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Zollerboy1.
Zollerboy1/SwiftCommand 1.2.0
A wrapper around Foundation.Process, inspired by Rust's std::process::Command.
⭐️ 50
🕓 2 weeks ago
.package(url: "https://github.com/Zollerboy1/SwiftCommand.git", from: "1.2.0")


Platform: macOS/Linux/Windows* Swift versions: 5.6, 5.7

*Windows support is only experimental for now.

A wrapper around Foundation.Process, inspired by Rust's std::process::Command. This package makes it easy to call command line programs and handle their I/O.


You can install this package using the Swift Package Manager, by including it in the dependencies of your package:

let package = Package(
    // ...
    dependencies: [
        // other dependencies...
            url: "https://github.com/Zollerboy1/SwiftCommand.git",
            from: "1.2.0"
    // ...


Using this package is very easy.

Before you start, make sure that you've imported the SwiftCommand module:

import SwiftCommand

Now it can be used like this:

let output = try Command.findInPath(withName: "echo")!

// Prints 'Foo\n'

This blocks the thread until the command terminates. You can use the async/await API instead, if you want to do other work while waiting for the command to terminate:

let output = try await Command.findInPath(withName: "echo")!

// Prints 'Foo\n'

Specifying command I/O

Suppose that you have a file called SomeFile.txt that looks like this:


You can then set stdin and stdout of commands like this:

let catProcess = try Command.findInPath(withName: "cat")!
                            .setStdin(.read(fromFile: "SomeFile.txt"))

let grepProcess = try Command.findInPath(withName: "grep")!
                             .setStdin(.pipe(from: catProcess.stdout))

for try await line in grepProcess.stdout.lines {
// Prints 'Bar' and 'Baz'

try catProcess.wait()
try grepProcess.wait()
// Ensure the processes are terminated before exiting the parent process

This is doing in Swift, what you would normally write in a terminal like this:

cat < SomeFile.txt | grep Ba

If you don't specify stdin, stdout, or stderr, and also don't capture the output (using e.g. waitForOutput()), then they will by default inherit the corresponding handle of the parent process. E.g. the stdout of the following program is Bar\n:

import SwiftCommand

try Command.findInPath(withName: "echo")!


Stars: 50
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

2 weeks ago

This release adds some API for accessing the stdout/stderr of a process as Data instead of as a String. It also makes it possible to access the signal that the process was terminated with on Linux and macOS.

Additionally, this release contains a simple fix by @doozMen, that allows the package to build correctly again on Linux.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics