ArgumentVisibility.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// Visibility level of an argument's help.
  12. public struct ArgumentVisibility: Hashable {
  13. /// Internal implementation of `ArgumentVisibility` to allow for easier API
  14. /// evolution.
  15. internal enum Representation {
  16. case `default`
  17. case hidden
  18. case `private`
  19. }
  20. internal var base: Representation
  21. /// Show help for this argument whenever appropriate.
  22. public static let `default` = Self(base: .default)
  23. /// Only show help for this argument in the extended help screen.
  24. public static let hidden = Self(base: .hidden)
  25. /// Never show help for this argument.
  26. public static let `private` = Self(base: .private)
  27. }
  28. extension ArgumentVisibility: Sendable {}
  29. extension ArgumentVisibility.Representation {
  30. /// A raw Integer value that represents each visibility level.
  31. ///
  32. /// `_comparableLevel` can be used to test if a Visibility case is more or
  33. /// less visible than another, without committing this behavior to API.
  34. /// A lower `_comparableLevel` indicates that the case is less visible (more
  35. /// secret).
  36. internal var _comparableLevel: Int {
  37. switch self {
  38. case .default:
  39. return 2
  40. case .hidden:
  41. return 1
  42. case .private:
  43. return 0
  44. }
  45. }
  46. }
  47. extension ArgumentVisibility {
  48. /// - Returns: true if `self` is at least as visible as the supplied argument.
  49. internal func isAtLeastAsVisible(as other: Self) -> Bool {
  50. self.base._comparableLevel >= other.base._comparableLevel
  51. }
  52. /// Reduce the visibility to a specified level if it is more restricted than the current value.
  53. internal mutating func reduce(to: ArgumentVisibility) {
  54. switch to.base {
  55. case .default:
  56. break // No effect
  57. case .hidden:
  58. if case .default = self.base {
  59. self.base = .hidden
  60. }
  61. case .private:
  62. self.base = .private
  63. }
  64. }
  65. }
  66. extension ArgumentDefinition {
  67. internal func reducingHelpVisibility(to visibility: ArgumentVisibility)
  68. -> Self
  69. {
  70. var result = self
  71. result.help.visibility.reduce(to: visibility)
  72. return result
  73. }
  74. }