Errors.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2020 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. /// An error type that is presented to the user as an error with parsing their
  12. /// command-line input.
  13. public struct ValidationError: Error, CustomStringConvertible {
  14. /// The error message represented by this instance, this string is presented to
  15. /// the user when a `ValidationError` is thrown from either; `run()`,
  16. /// `validate()` or a transform closure.
  17. public internal(set) var message: String
  18. /// Creates a new validation error with the given message.
  19. public init(_ message: String) {
  20. self.message = message
  21. }
  22. public var description: String {
  23. message
  24. }
  25. }
  26. /// An error type that only includes an exit code.
  27. ///
  28. /// If you're printing custom error messages yourself, you can throw this error
  29. /// to specify the exit code without adding any additional output to standard
  30. /// out or standard error.
  31. public struct ExitCode: Error, RawRepresentable, Hashable {
  32. /// The exit code represented by this instance.
  33. public var rawValue: Int32
  34. /// Creates a new `ExitCode` with the given code.
  35. public init(_ code: Int32) {
  36. self.rawValue = code
  37. }
  38. public init(rawValue: Int32) {
  39. self.init(rawValue)
  40. }
  41. /// An exit code that indicates successful completion of a command.
  42. public static let success = ExitCode(Platform.exitCodeSuccess)
  43. /// An exit code that indicates that the command failed.
  44. public static let failure = ExitCode(Platform.exitCodeFailure)
  45. /// An exit code that indicates that the user provided invalid input.
  46. public static let validationFailure = ExitCode(
  47. Platform.exitCodeValidationFailure)
  48. /// A Boolean value indicating whether this exit code represents the
  49. /// successful completion of a command.
  50. public var isSuccess: Bool {
  51. self == Self.success
  52. }
  53. }
  54. /// An error type that represents a clean (non-error state) exit of the
  55. /// utility.
  56. ///
  57. /// Throwing a `CleanExit` instance from a `validate` or `run` method, or
  58. /// passing it to `exit(with:)`, exits the program with exit code `0`.
  59. public struct CleanExit: Error, CustomStringConvertible {
  60. internal enum Representation {
  61. case helpRequest(ParsableCommand.Type? = nil)
  62. case message(String)
  63. case dumpRequest(ParsableCommand.Type? = nil)
  64. }
  65. internal var base: Representation
  66. /// Treat this error as a help request and display the full help message.
  67. ///
  68. /// You can use this case to simulate the user specifying one of the help
  69. /// flags or subcommands.
  70. ///
  71. /// - Parameter command: The command type to offer help for, if different
  72. /// from the root command.
  73. ///
  74. /// - Returns: A throwable CleanExit error.
  75. public static func helpRequest(
  76. _ command: ParsableCommand.Type? = nil
  77. ) -> CleanExit {
  78. self.init(base: .helpRequest(command))
  79. }
  80. /// Treat this error as a clean exit with the given message.
  81. public static func message(_ text: String) -> CleanExit {
  82. self.init(base: .message(text))
  83. }
  84. /// Treat this error as a help request and display the full help message.
  85. ///
  86. /// You can use this case to simulate the user specifying one of the help
  87. /// flags or subcommands.
  88. ///
  89. /// - Parameter command: A command to offer help for, if different from
  90. /// the root command.
  91. ///
  92. /// - Returns: A throwable CleanExit error.
  93. public static func helpRequest(_ command: ParsableCommand) -> CleanExit {
  94. .helpRequest(type(of: command))
  95. }
  96. public var description: String {
  97. switch self.base {
  98. case .helpRequest: return "--help"
  99. case .message(let message): return message
  100. case .dumpRequest: return "--experimental-dump-help"
  101. }
  102. }
  103. }