Commands

2.0.0

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

What's New

v2.0.0 eco goose

2022-03-19T19:25:47Z

Commands

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

Install

As a Swift Package:

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 🐼

Description

  • Swift Tools 4.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Aug 31 2022 21:23:34 GMT-0500 (GMT-05:00)