You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for exiting without printing an error. (#51)
This adds an `ExitCode` error type that stores an exit code,
and updates the exit machinery to (1) use `ExitCode` as the source
for exit codes, and (2) silently exit when the provided error
is an `ExitCode` instance.
Copy file name to clipboardExpand all lines: Documentation/05 Validation and Errors.md
+21-1Lines changed: 21 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ hey
57
57
58
58
## Handling Post-Validation Errors
59
59
60
-
The `ValidationError` type is a special `ArgumentParser` error — a validation error's message is always accompanied by an appropriate usage string. You can throw other errors, from either the `validate()` or `run()` method to indicate that something has gone wrong that isn't validation-specific.
60
+
The `ValidationError` type is a special `ArgumentParser` error — a validation error's message is always accompanied by an appropriate usage string. You can throw other errors, from either the `validate()` or `run()` method to indicate that something has gone wrong that isn't validation-specific. Errors that conform to `CustomStringConvertible` or `LocalizedError` provide the best experience for users.
61
61
62
62
```swift
63
63
structLineCount: ParsableCommand {
@@ -80,3 +80,23 @@ The throwing `String(contentsOfFile:encoding:)` initializer fails when the user
80
80
Error: The file “non-existing-file.swift” couldn’t be opened because
81
81
there is no such file.
82
82
```
83
+
84
+
If you print your error output yourself, you still need to throw an error from `validate()` or `run()`, so that your command exits with the appropriate exit code. To avoid printing an extra error message, use the `ExitCode` error, which has static properties for success, failure, and validation errors, or lets you specify a specific exit code.
0 commit comments