https://github.com/qiuzhifei/swift-commands
Swift utilities for running commands.
https://github.com/qiuzhifei/swift-commands
commands macos spm subprocess swift swift-commands swift-package-manager
Last synced: 7 months ago
JSON representation
Swift utilities for running commands.
- Host: GitHub
- URL: https://github.com/qiuzhifei/swift-commands
- Owner: QiuZhiFei
- License: mit
- Created: 2021-08-03T10:17:58.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-02T08:51:19.000Z (over 1 year ago)
- Last Synced: 2024-07-08T09:37:10.007Z (over 1 year ago)
- Topics: commands, macos, spm, subprocess, swift, swift-commands, swift-package-manager
- Language: Swift
- Homepage:
- Size: 147 KB
- Stars: 53
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift Commands


[](https://github.com/qiuzhifei/swift-commands/actions)
[](https://github.com/qiuzhifei/swift-commands/blob/main/LICENSE)

Swift utilities for running commands.
The `Commands` module allows you to take a system command as a string and return the standard output.
[API documentation can be found here](https://qiuzhifei.github.io/swift-commands/).
## Usage
```
import Commands
```
### Bash
Execute shell commands.
```swift
let result = Commands.Task.run("bash -c ls")
```
Or
```swift
let result = Commands.Task.run(["bash", "-c", "ls"])
```
Or
```swift
let result = Commands.Bash.run("ls")
```
### Python
Execute python scripts.
```swift
let result = Commands.Task.run("python main.py")
```
Execute python commands.
```swift
let result = Commands.Task.run("python -c import base64; print(base64.b64encode(b'qiuzhifei').decode('ascii'))")
```
Or
```swift
let result = Commands.Python.run("import base64; print(base64.b64encode(b'qiuzhifei').decode('ascii'))")
```
### Ruby
Execute ruby scripts.
```swift
let result = Commands.Task.run("ruby main.rb")
```
Execute ruby commands.
```swift
let result = Commands.Task.run("ruby -e require 'base64'; puts Base64.encode64('qiuzhifei')")
```
Or
```swift
let result = Commands.Ruby.run("require 'base64'; puts Base64.encode64('qiuzhifei')")
```
### Alias
Create a shortcut name for a command.
```swift
let node = Commands.Alias("/usr/local/bin/node", dashc: "-e")
let result = node.run("console.log('qiuzhifei')")
```
### Setting global environment variables
```swift
Commands.ENV.global["http_proxy"] = "http://127.0.0.1:7890"
```
```swift
Commands.ENV.global.add(PATH: "/Users/zhifeiqiu/.rvm/bin")
```
### Making Commands
```swift
let request: Commands.Request = "ruby -v"
```
Or
```swift
let request: Commands.Request = ["ruby", "-v"]
```
Or
```swift
let request = Commands.Request(executableURL: "ruby", arguments: "-v")
```
Change environment variables
```swift
var request: Commands.Request = "ruby -v"
request.environment?.add(PATH: "/usr/local/bin")
request.environment?["http_proxy"] = "http://127.0.0.1:7890"
request.environment?["https_proxy"] = "http://127.0.0.1:7890"
request.environment?["all_proxy"] = "socks5://127.0.0.1:7890"
let result = Commands.Task.run(request)
```
### Result Handler
Returns the `Commands.Result` of running cmd in a subprocess.
```swift
let result = Commands.Task.run("ruby -v")
switch result {
case .Success(let request, let response):
debugPrint("command: \(request.absoluteCommand), success output: \(response.output)")
case .Failure(let request, let response):
debugPrint("command: \(request.absoluteCommand), failure output: \(response.errorOutput)")
}
```
## Adding Commands as a Dependency
To use the `Commands` library in a SwiftPM project,
add the following line to the dependencies in your `Package.swift` file:
```swift
let package = Package(
// name, platforms, products, etc.
dependencies: [
.package(url: "https://github.com/qiuzhifei/swift-commands", from: "0.6.0"),
// other dependencies
],
targets: [
.target(name: "", dependencies: [
.product(name: "Commands", package: "swift-commands"),
]),
// other targets
]
)
```
## CocoaPods (OS X 11.0+)
You can use [CocoaPods](http://cocoapods.org/) to install `Commands` by adding it to your `Podfile`:
```ruby
pod 'Commands', '~> 0.7.0'
```
## QuickStart
```shell
git clone https://github.com/QiuZhiFei/swift-commands
cd swift-commands && open Package.swift
```
## References
- [https://github.com/apple/swift-argument-parser](https://github.com/apple/swift-argument-parser)