ParsedValues.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /// The resulting values after parsing the command-line arguments.
  12. ///
  13. /// This is a flat key-value list of values.
  14. struct ParsedValues {
  15. struct Element {
  16. var key: InputKey
  17. var value: Any?
  18. /// Where in the input that this came from.
  19. var inputOrigin: InputOrigin
  20. fileprivate var shouldClearArrayIfParsed = true
  21. }
  22. /// These are the parsed key-value pairs.
  23. var elements: [InputKey: Element] = [:]
  24. /// This is the *original* array of arguments that this was parsed from.
  25. ///
  26. /// This is used for error output generation.
  27. var originalInput: [String]
  28. /// Any arguments that are captured into an `.allUnrecognized` argument.
  29. var capturedUnrecognizedArguments = SplitArguments(originalInput: [])
  30. }
  31. extension ParsedValues {
  32. mutating func set(_ new: Any?, forKey key: InputKey, inputOrigin: InputOrigin)
  33. {
  34. set(Element(key: key, value: new, inputOrigin: inputOrigin))
  35. }
  36. mutating func set(_ element: Element) {
  37. if let e = elements[element.key] {
  38. // Merge the source values. We need to keep track
  39. // of any previous source indexes we have used for
  40. // this key.
  41. var element = element
  42. element.inputOrigin.formUnion(e.inputOrigin)
  43. elements[element.key] = element
  44. } else {
  45. elements[element.key] = element
  46. }
  47. }
  48. func element(forKey key: InputKey) -> Element? {
  49. elements[key]
  50. }
  51. mutating func update<A>(
  52. forKey key: InputKey, inputOrigin: InputOrigin, initial: A,
  53. closure: (inout A) -> Void
  54. ) {
  55. var e =
  56. element(forKey: key)
  57. ?? Element(key: key, value: initial, inputOrigin: InputOrigin())
  58. var v = (e.value as? A) ?? initial
  59. closure(&v)
  60. e.value = v
  61. e.inputOrigin.formUnion(inputOrigin)
  62. set(e)
  63. }
  64. mutating func update<A>(
  65. forKey key: InputKey, inputOrigin: InputOrigin, initial: [A],
  66. closure: (inout [A]) -> Void
  67. ) {
  68. var e =
  69. element(forKey: key)
  70. ?? Element(key: key, value: initial, inputOrigin: InputOrigin())
  71. var v = (e.value as? [A]) ?? initial
  72. // The first time a value is parsed from command line, empty array of any default values.
  73. if e.shouldClearArrayIfParsed {
  74. v.removeAll()
  75. e.shouldClearArrayIfParsed = false
  76. }
  77. closure(&v)
  78. e.value = v
  79. e.inputOrigin.formUnion(inputOrigin)
  80. set(e)
  81. }
  82. }