Skip to content

Execute commands from within your application and capture their output and exit codes

License

Notifications You must be signed in to change notification settings

q231950/commands

Repository files navigation

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 🐼

About

Execute commands from within your application and capture their output and exit codes

Topics

Resources

License

Stars

Watchers

Forks