Custom Exit Code

I have a need to specify exit codes based on the thrown error type. It seems there might be an API for that Add an API for converting an error to an exit code by natecook1000 · Pull Request #79 · apple/swift-argument-parser · GitHub.

But I am having a hard time figuring out how I am supposed to actually use this API to specify my own exit code.

Is an example available?

If you want to exit with a specific error code, the way to do that currently is to throw an ExitCode error or pass one to MyCommand.exit(with:). Neither of these will print anything, so you'd want to print any error message to stderr first.

There's an issue here for handling CustomNSErrors better, which will let you build an error with a custom description and a custom exit code: Throwing a `CustomNSError` should exit with the custom error code · Issue #243 · apple/swift-argument-parser · GitHub

1 Like

You created that issue 6 hours ago. I wrote that message yesterday before continuing to come up with a solution for the rest of the night. I swear I looked before writing the message. Clearly not before posting.

I'll see if I cannot implement that feature since it is marked as a starter.

Also, obviously coincidences and all that... but that's some crazy timing. :laughing: