Commander
Commander copied to clipboard
PromiseKit extension
Hey 👋
I'm currently using the following overload for command in order to be able to return a Promise from PromiseKit directly from my command. This, I think, makes it very easy to write asynchronous CLI apps (e.g. performing multiple tasks in parallel).
This example is only for the very specific "two argument descriptor and an executor" overload, but could easily be written for every already existing overload.
import Foundation
import Commander
import PromiseKit
public func command<A:ArgumentDescriptor, A1:ArgumentDescriptor>(_ descriptor: A, _ descriptor1:A1, _ closure:@escaping (A.ValueType, A1.ValueType) throws -> Promise<Void>) -> CommandType {
return command(descriptor, descriptor1) { (value0: A.ValueType, value1: A1.ValueType) throws -> () in
firstly {
try closure(value0, value1)
}.done { _ in
exit(0)
}.catch { err in
command({ throw err }).run()
}
CFRunLoopRun()
}
}
How would you feel about a pull request that would add these overloads? (probably as a new target, so that it's opt-in for consumers)
I would also like to improve the error handling (command({ throw err }).run()), how would you feel about extracting out the following lines to a separate function?
https://github.com/kylef/Commander/blob/314f8d7455e25e60e9e9b850040eebd0a87f2527/Sources/Commander/CommandRunner.swift#L25-L46
Thanks for a great library 🍻