import PackageDescription
let package = Package(
name: "YourAwesomeSoftware",
dependencies: [
.package(url: "https://github.com/Kitura/ShellToolKit.git", from: "0.1.0")
],
targets: [
.target(
name: "MyApp",
dependencies: ["ShellToolKit"]
)
]
)
#!/usr/bin/swift sh
import ShellToolKit // @Kitura
Currently the only class supported is SystemAction
which allows command-line tools to more easily support variations of verbose/dry-run. It uses the Rainbow library for colorized output and SwiftShell for executing commands.
A typical swift-argument-parser based program may look something like this:
import ArgumentParser // https://github.com/apple/swift-argument-parser.git
struct BuildCommand: ParsableCommand {
@Flag(name: .shortAndLong, help: "Enable verbose mode")
var verbose: Bool = false
@Flag(name: [.customLong("dry-run"), .customShort("n")], help: "Dry-run (print but do not execute commands)")
var enableDryRun: Bool = false
mutating func run() throws {
let actions: SystemAction
if enableDryRun {
actions = CompositeAction([SystemActionPrint()])
} else if verbose {
actions = CompositeAction([SystemActionPrint(), SystemActionReal()])
} else {
actions = CompositeAction([SystemActionReal()])
}
try actions.runAndPrint(command: "echo", "Hello", "World!")
}
}
- How to run an external program using Process
- StackOverflow: Make pipe fd return true for is_atty() in child process
- Streams of Cocoa: Why It's Still Worth Knowing NSStream
- The very basics of a terminal emulator
- StackOverflow: How to redirect stdout to stdin using fork and pipe and execvp in c?
- Foundation: NSStream.getBoundStreamsWithBufferSize:inputStream:outputStream:
- Streams of Cocoa: Why It's Still Worth Knowing NSStream
- PtyKit
- SwiftyPOSIX