Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PromiseKit extension #68

Open
LinusU opened this issue Mar 23, 2018 · 0 comments
Open

PromiseKit extension #68

LinusU opened this issue Mar 23, 2018 · 0 comments

Comments

@LinusU
Copy link
Contributor

LinusU commented Mar 23, 2018

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?

} 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 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant