A pipe for streaming text from the command line into Swift scripts.
My hope is for this framework to eventually become redundant and obsolete. The makers of Swift could come up with an elegant way to pump lines of text into scripts.
In September 2015, the engineers at Apple granted my wish with the addition
of readLine()
to Swift 2 (details here). Their implementation
is robust, simple to use and highly recommended. I plan to use it for my own
scripts.
This repository will be maintained but mostly as an academic exercise.
If you're looking for an arcane excuse to use FileInput
, obtaining the
file path of each line of text is a minor detail that can be
interesting in certain cases.
myScript.swift a.txt b.txt c.txt
However, processing multiple files is easily accomplished with readLine()
.
cat a.txt b.txt c.txt | myScript.swift
The interface is borrowed from python.
#!/usr/bin/swift -F <path-to-FileInput.framework>
import FileInput
for line in input() {
process(line)
}
More documentation here.
Requires Xcode 7.3 or later.
% git clone git@github.com:otto-schnurr/FileInput-swift.git
...
% cd FileInput-swift
% xcodebuild
...
Move ./build/Release/FileInput.framework
to a convenient path
for your Swift scripts to reference.
important: If you plan to ship this framework in a binary, Apple recommends recompiling Swift frameworks with the same version of Xcode as your binary.
MIT License, see http://opensource.org/licenses/MIT