Name: morione
Owner: Wire Swiss GmbH
Description: A Swift subprocess execution library intended to be used in Swift scripts, inspired by Python subprocess
Forked from: marcoconti83/morione
Created: 2016-09-12 12:28:34.0
Updated: 2017-10-07 16:31:51.0
Pushed: 2018-04-12 13:38:32.0
Size: 52
Language: Swift
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Morione is a Swift subprocess execution library intended to uses in Swift scripts, inspired by Python subprocess. It allows scripts to spawn subprocesses and capture the output and return status. While designed with Swift scripting in mind, it can also be used in other scenarios (e.g. OSX apps).
See also Targone, A Swift command line argument parser and automatic usage description generator inspired by Python Argparse.
Morione public API is designed keeping in mind ease of use within a script.
To achieve this, errors in executing subprocess are printed on screen instead of throwing errors, not to make the script too cumbersome with try
s.
The API is documented in the code and tests.
There is a full-fledged API version (returning Optionals) and a compact API version. The compact API will make a lot of assumption and just assert if any of these assumption is not fulfilled. This is intended to be used in scripts where it's desirable to abort the script with an automatically generated error message in case of error. We are aware that this makes it hard to write unit tests, but it contributes to create simple scripts that are concise and to the point. Compare Subprocess.output("/bin/df", "-h", "-l")
(compact API) with Subprocess("/bin/df", "-h", "-l").execute()!.output
.
A simple example Swift script integration can be found in the Example folder.
To print the last line of the output of /bin/df -h -l
in your script, use:
diskFree = Subprocess.output("/bin/df", "-h", "-l")
t lines.last
ecute a command, get the termination status:
ecute a command, get the output as an array of strings, one element per output line:
stdErr
and the termination status:d let result = Subprocess("do.sh").execute(true) else { ... } // will fail if "do.sh" does not exist
status = result.status
error = result.error
output = result.output
pipeline = Subprocess("/bin/ls","-l","folder") | Subprocess("/usr/bin/grep", "file-") | Subprocess("/usr/bin/sort","-r")
output = pipeline.output()
Just add `import Morione
` to your script
In order to be able to import it, you need to have the Morione.framework
in the Swift search path. You can achieve this by compiling it yourself or downloading a binary version from a release. You need to invoke Swift with the -F
argument, pointing to the folder where Morione is stored.
Morione can be downloaded locally with Carthage.
Just add
our `Cartfile`. After running
you would be able to run any swift file from the folder where you run carthage, with:
`-F` flag can also be included in the [shebang](https://en.wikipedia.org/wiki/Shebang_%28Unix%29) line of the script, so that you can just invoke the script directly (e.g. ```$> do.swift```). This is the approach used in the [examples](https://github.com/marcoconti83/morione/tree/master/Examples) included with this project.
Without Carthage
can download the framework binary from the GitHub [latest release](https://github.com/marcoconti83/morione/releases/latest)