ArgumentDiscussion.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2024 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. /// A structure that contains an extended description of the argument.
  12. ///
  13. /// For `EnumerableOptionValue` types, the `.enumerated` case encapsulates the
  14. /// necessary information to list each of the possible values and their
  15. /// descriptions. Optionally, users can add a discussion preamble that will be
  16. /// appended to the beginning of the value list section.
  17. ///
  18. /// For example, the following `EnumerableOptionValue` type defined in a command
  19. /// could contain an additional discussion block defined in its `ArgumentHelp`:
  20. ///
  21. /// ```swift
  22. /// enum Color: String, EnumerableOptionValue {
  23. /// case red
  24. /// case blue
  25. /// case yellow
  26. ///
  27. /// public var description: String {
  28. /// switch self {
  29. /// case .red:
  30. /// return "A red color."
  31. /// case. blue:
  32. /// return "A blue color."
  33. /// case .yellow:
  34. /// return "A yellow color."
  35. /// }
  36. /// }
  37. /// }
  38. ///
  39. /// struct Example: ParsableCommand {
  40. /// @Option(help: ArgumentHelp(discussion: "A set of available colors."))
  41. /// var color: Color
  42. /// }
  43. /// ```
  44. ///
  45. /// To which the printed usage would look like the following:
  46. ///
  47. /// ```
  48. /// USAGE: example --color <color>
  49. ///
  50. /// OPTIONS:
  51. /// --color <color>
  52. /// A set of available colors.
  53. /// Values:
  54. /// red - A red color.
  55. /// blue - A blue color.
  56. /// yellow - A yellow color.
  57. /// -h, --help Show help information
  58. /// ```
  59. ///
  60. /// Without the additional discussion text:
  61. ///
  62. /// ```swift
  63. /// @Option var color: Color
  64. /// ```
  65. ///
  66. /// The printed usage would look like the following:
  67. ///
  68. /// ```
  69. /// USAGE: example --color <color>
  70. ///
  71. /// OPTIONS:
  72. /// --color <color>
  73. /// red - A red color.
  74. /// blue - A blue color.
  75. /// yellow - A yellow color.
  76. /// -h, --help Show help information
  77. /// ```
  78. ///
  79. /// Arrays of enumerable types are also supported and will display the same
  80. /// enumerated format:
  81. ///
  82. /// ```swift
  83. /// @Option var colors: [Color] = [.red, .blue]
  84. /// ```
  85. ///
  86. /// The printed usage would look like the following:
  87. ///
  88. /// ```
  89. /// USAGE: example [--colors <colors> ...]
  90. ///
  91. /// OPTIONS:
  92. /// --colors <colors> (default: red, blue)
  93. /// red - A red color.
  94. /// blue - A blue color.
  95. /// yellow - A yellow color.
  96. /// -h, --help Show help information
  97. /// ```
  98. ///
  99. /// In any case where the argument type is not `EnumerableOptionValue` or an
  100. /// array of `EnumerableOptionValue`, the default implementation will use the
  101. /// `.staticText` case and will print a block of discussion text.
  102. enum ArgumentDiscussion {
  103. case staticText(String)
  104. case enumerated(preamble: String? = nil, any ExpressibleByArgument.Type)
  105. init?(
  106. _ text: String? = nil, _ options: (any ExpressibleByArgument.Type)? = nil
  107. ) {
  108. switch (text, options) {
  109. case (.some(let text), .some(let options)):
  110. guard !options.allValueDescriptions.isEmpty else {
  111. self = .staticText(text)
  112. return
  113. }
  114. self = .enumerated(preamble: text, options)
  115. case (.some(let text), .none):
  116. self = .staticText(text)
  117. case (.none, .some(let options)):
  118. guard !options.allValueDescriptions.isEmpty else {
  119. return nil
  120. }
  121. self = .enumerated(options)
  122. default:
  123. return nil
  124. }
  125. }
  126. var isEnumerated: Bool {
  127. if case .enumerated = self {
  128. return true
  129. }
  130. return false
  131. }
  132. }
  133. extension ArgumentDiscussion: Sendable {}
  134. extension ArgumentDiscussion: Hashable {
  135. static func == (lhs: ArgumentDiscussion, rhs: ArgumentDiscussion) -> Bool {
  136. switch (lhs, rhs) {
  137. case (.staticText(let lhsText), .staticText(let rhsText)):
  138. return lhsText == rhsText
  139. case (
  140. .enumerated(let lhsPreamble, let lhsOption),
  141. .enumerated(preamble: let rhsPreamble, let rhsOption)
  142. ):
  143. return (lhsPreamble == rhsPreamble) && (lhsOption == rhsOption)
  144. default:
  145. return false
  146. }
  147. }
  148. func hash(into hasher: inout Hasher) {
  149. switch self {
  150. case .staticText(let text):
  151. hasher.combine(text)
  152. case .enumerated(preamble: let text, let options):
  153. hasher.combine(text)
  154. hasher.combine(ObjectIdentifier(options))
  155. }
  156. }
  157. }