A wrapper around Foundation.Process, inspired by Rust's std::process::Command.

Overview

SwiftCommand

Platform: macOS/Linux/Windows Swift versions: 5.6

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.

Installation

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...
        .package(
            url: "https://github.com/Zollerboy1/SwiftCommand.git",
            from: "1.1.0"
        ),
    ],
    // ...
)

Usage

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")!
                        .addArgument("Foo")
                        .waitForOutput()

print(output.stdout)
// 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")!
                              .addArgument("Foo")
                              .output

print(output.stdout)
// Prints 'Foo\n'

Specifying command I/O

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

Foo
Bar
Baz

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

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

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

for try await line in grepProcess.stdout.lines {
    print(line)
}
// 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")!
           .addArgument("Bar")
           .wait()
You might also like...
ips2crash is a macOS command line too to convert a .ips file to a legacy .crash log file.

Synopsis ips2crash is a macOS command line too to convert a .ips file to a legacy .crash log file. Motivation It should be possible to read .ips file

CookCLI is provided as a command-line tool to make Cook recipe management easier

CookCLI is provided as a command-line tool to make Cook recipe management easier, and enable automation and scripting workflows for the CookLa

Adjust the volume from the command line on macOS.

volume Adjust the volume from the command line on macOS. Installation Using Mint: mint install meowmeowmeowcat/[email protected] Usage USAGE: volume numb

A Mac command-line tool that automatically downloads macOS Installers / Firmwares.
A Mac command-line tool that automatically downloads macOS Installers / Firmwares.

MIST - macOS Installer Super Tool A Mac command-line tool that automatically downloads macOS Installers / Firmwares: Features List all available macOS

macOS command line tool to return the available disk space on APFS volumes

diskspace Returns available disk space With the various APFS features the value for free disk space returned from tools such as du or df will not be a

Command-line utility that checks comments for localizations in iOS interface files (.xib, .storyboard)

Localizations Comments Checker It's really easy to overlook and don't add comment for localization in interface file (.storyboard or .xib). This comma

A command-line tool to generate a JSON-list of all used SPM-dependencies of an Xcode-project.

SwiftPackageList A command-line tool to generate a JSON-list of all used SPM-dependencies of an Xcode-project. This includes all the Package.resolved

View Metal GPU information from the command-line.

View Metal GPU information from the command-line.

Command-line commands for Chaqmoq applications

Chaqmoq CLI Installation Swift Download and install Swift Swift Package mkdir MyApp cd MyApp swift package init --type executable // Creates an execut

Releases(1.1.2)
  • 1.1.2(Aug 17, 2022)

    Version 1.1.2 adds initial support for Windows.

    This means, that the package builds now on Windows and that a few little test programs executed correctly.

    However, it doesn't mean that all features are thoroughly tested on Windows as they are on macOS/Linux. This is partly due to the fact that Windows doesn't have executables called echo.exe or cat.exe, which would be needed for the tests to run at all. If someone wants to help getting the support for Windows to a proper stage, feel free to do so, it would be highly appreciated!

    Source code(tar.gz)
    Source code(zip)
  • 1.1.1(Aug 16, 2022)

    This is the first version of SwiftCommand that is actually working with Swift 5.6 or 5.7 on both macOS and Linux.

    Just consider the versions before this one as beta versions – I accidentally only tested them with Swift 5.7 (beta) and/or only on macOS.

    Source code(tar.gz)
    Source code(zip)
Owner
Josef Zoller
Currently studying CS at ETH Zürich and doing some (mostly random) side-projects. Also loving my cat.
Josef Zoller
CommandLineKit - A pure Swift library for creating command-line interfaces

CommandLineKit A pure Swift library for creating command-line interfaces. Note: This project is no longer maintained. It's preserved here for historic

Ben Gollmer 1.1k Dec 1, 2022
Swiftline is a set of tools to help you create command line applications

Swiftline is a set of tools to help you create command line applications. Swiftline is inspired by highline Swiftline contains the following: Colorize

Omar Abdelhafith 1.2k Dec 29, 2022
A Mac command-line tool that generates kick-ass Jamf Pro reports.

KMART - Kick-Ass Mac Admin Reporting Tool A command-line utility generating kick-ass Jamf Pro reports: Features Reporting on the following Jamf Pro ob

Nindi Gill 86 Dec 15, 2022
ipatool is a command line tool that allows you to search for iOS apps on the App Store and download a copy of the app package, known as an ipa file.

ipatool is a command line tool that allows you to search for iOS apps on the App Store and download a copy of the app package, known as an ipa file.

Majd Alfhaily 3k Dec 30, 2022
iOS command-line tool that allows searching and downloading ipa files from the iOS App Store

ipatool for iOS This is a port of Majd Alfhaily's ipatool adapted to run on iOS Build / Installation To build this, make sure you have AppSync install

dan 21 Sep 13, 2022
A nifty command-line tool to customize macOS icons

iconset A nifty command line tool to manage macOS icons iconset is a new command line tool for macOS that allows you to change icons for macOS apps (e

aarnav tale 32 Nov 17, 2022
🕳 A simple command line tool to punch hole to reduce disk usage on APFS volume for such as a raw disk image.

HolePunch NAME holepunch -- A simple command line tool to punch hole to reduce disk usage on APFS volume for such as a raw disk image. SYNOPSIS holepu

Yoshimasa Niwa 15 Nov 24, 2022
The best command-line tool to install and switch between multiple versions of Xcode.

The best command-line tool to install and switch between multiple versions of Xcode.

Robots and Pencils 2.3k Jan 9, 2023
Command Line Tool for interacting with MachO binaries on OSX/iOS

inject inject is a tool which interfaces with MachO binaries in order to insert load commands. Below is its help. ➜ ./inject -h OVERVIEW: inject v1.0.

<script>alert('1')</script> 36 Dec 23, 2022
Simple & Elegant Command Line Interfaces in Swift

An elegant pure Swift library for building command line applications. Features Tons of class, but no classes. 100% organic pure value types. Auto gene

hypertalk 52 Nov 9, 2022