colinc86 / CommandInterface

A Swift package for interacting with command line interfaces.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CommandInterface

A Swift package for interacting with command line interfaces.

Creating an Interface

Create an interface with

  1. the URL to the executable,
  2. an, optional, URL to the executable's working directory,
  3. and an, optional, dictionary of environment variables.
let interface = Interface(executableURL: ...)

Creating Commands

Create a command by implementing the Command and CommandResponse protocols.

struct VersionCommand: Command {
  typealias Response = VersionCommandResponse
  
  var arguments: [String] {
    return ["--version"]
  }
}

public struct VersionCommandResponse: CommandResponse {
  public let version: String
  
  public init?(response: String?) {
    guard let response = response else { return nil }
    version = response
  }
}

Sending Commands

Send commands to the executable through the interface.

do {
  try interface.send(VersionCommand()) { response in
    print("Version: \(response.version ?? "N/A")")
  }
}
catch {
  print("Error sending command: \(error)")
}

About

A Swift package for interacting with command line interfaces.


Languages

Language:Swift 100.0%