kareman / SwiftShell

A Swift framework for shell scripting.
https://kareman.github.io/SwiftShell
MIT License
1.03k stars 87 forks source link

Make run and throwable #96

Open Blackjacx opened 4 years ago

Blackjacx commented 4 years ago

I already mentioned this request in #92. I think it is a very common use case to make all run commands throwable.

What I would like to avoid is the extra check for the error and manually print the error like this:

        let out = run("mint", args)

        if let error = out.error {
            print(out.stderror)
            throw error
        }

The following would be much cleaner imho and the failure reason could be part of the error or an underlying error maybe:

        let successfulOut = try run("mint", args)

Originally posted by @stherold in https://github.com/kareman/SwiftShell/issues/92#issuecomment-698853537

kareman commented 4 years ago

You're right, it is a bit cumbersome the way it is now. The problem with your suggestion is that it makes it more cumbersome to get the result of a failing command. And it is a breaking change, so it would have to be in SwiftShell 6.0. But your use case is probably more common.

I've been wanting to improve the API for a long time now, but I just haven't been able to come up with a good solution that handles all aspects well, like error handling, synchronicity, shell interpreter, stdin, joined or separate stdout/stderror, printing output, piping output to another command, and probably even more that I haven't thought of yet.