目录
目录README.md

Commands

Commands allows you to execute commands from within your application and capture their output and exit codes.

Install

As a Swift Package:

.package(url: "git@github.com:q231950/commands.git", from: "2.0.0")

Usage

You can build and run an example from the command line or by running the CommandsExample target in Xcode. The example utilizes Commands to print some date and time.

kim@mbp commands % swift run
Building for debugging...
Build complete! (0.15s)
output:
DATE: 2022-03-19
TIME: 19:44:28

exit code: 0

The DATE/TIME text is output of the command that you can find in main.swift of the CommandsExample target. It is described in more detail there, but here is the shortened essence of that code. This is also how you can execute commands and capture their output and exit codes from within your code:

let command = Command(launchPath: "/bin/date", arguments: ["+%nDATE: %Y-%m-%d%nTIME: %H:%M:%S"])

let commandExecutor = CommandExecutor()

commandExecutor.outputHandler = { text in
    print("output: \(text)")
}

commandExecutor.terminationHandler = { code in
    print("exit code: \(code)")
}

commandExecutor.execute(command)

Test

swift test


Have fun 🐼

关于
358.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号