-
Notifications
You must be signed in to change notification settings - Fork 85
Description
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?
Commander/Sources/Commander/CommandRunner.swift
Lines 25 to 46 in 314f8d7
| } catch let error as Help { | |
| let help = error.reraise("$ \(executableName)") | |
| help.print() | |
| exit(1) | |
| } catch GroupError.noCommand(let path, let group) { | |
| var usage = "$ \(executableName)" | |
| if let path = path { | |
| usage += " \(path)" | |
| } | |
| let help = Help([], command: usage, group: group) | |
| help.print() | |
| exit(1) | |
| } catch let error as ANSIConvertible { | |
| error.print() | |
| exit(1) | |
| } catch let error as CustomStringConvertible { | |
| ANSI.red.print(error.description, to: stderr) | |
| exit(1) | |
| } catch { | |
| ANSI.red.print("Unknown error occurred.", to: stderr) | |
| exit(1) | |
| } |
Thanks for a great library π»