1010//===----------------------------------------------------------------------===//
1111
1212fileprivate protocol ParsableArgumentsValidator {
13- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ?
13+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ?
1414}
1515
1616enum ValidatorErrorKind {
@@ -37,15 +37,15 @@ struct ParsableArgumentsValidationError: Error, CustomStringConvertible {
3737}
3838
3939extension ParsableArguments {
40- static func _validate( ) throws {
40+ static func _validate( parent : InputKey . Parent ) throws {
4141 let validators : [ ParsableArgumentsValidator . Type ] = [
4242 PositionalArgumentsValidator . self,
4343 ParsableArgumentsCodingKeyValidator . self,
4444 ParsableArgumentsUniqueNamesValidator . self,
4545 NonsenseFlagsValidator . self,
4646 ]
4747 let errors = validators. compactMap { validator in
48- validator. validate ( self )
48+ validator. validate ( self , parent : parent )
4949 }
5050 if errors. count > 0 {
5151 throw ParsableArgumentsValidationError ( parsableArgumentsType: self , underlayingErrors: errors)
@@ -68,7 +68,6 @@ fileprivate extension ArgumentSet {
6868/// in the argument list. Any other configuration leads to ambiguity in
6969/// parsing the arguments.
7070struct PositionalArgumentsValidator : ParsableArgumentsValidator {
71-
7271 struct Error : ParsableArgumentsValidatorError , CustomStringConvertible {
7372 let repeatedPositionalArgument : String
7473
@@ -81,19 +80,16 @@ struct PositionalArgumentsValidator: ParsableArgumentsValidator {
8180 var kind : ValidatorErrorKind { . failure }
8281 }
8382
84- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
83+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
8584 let sets : [ ArgumentSet ] = Mirror ( reflecting: type. init ( ) )
8685 . children
8786 . compactMap { child in
8887 guard
89- var codingKey = child. label,
88+ let codingKey = child. label,
9089 let parsed = child. value as? ArgumentSetProvider
9190 else { return nil }
9291
93- // Property wrappers have underscore-prefixed names
94- codingKey = String ( codingKey. first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey. dropFirst ( 0 ) )
95-
96- let key = InputKey ( rawValue: codingKey)
92+ let key = InputKey ( name: codingKey, parent: parent)
9793 return parsed. argumentSet ( for: key)
9894 }
9995
@@ -107,20 +103,20 @@ struct PositionalArgumentsValidator: ParsableArgumentsValidator {
107103 let firstRepeatedPositionalArgument : ArgumentDefinition = sets [ repeatedPositional] . firstRepeatedPositionalArgument!
108104 let positionalFollowingRepeatedArgument : ArgumentDefinition = positionalFollowingRepeated. firstPositionalArgument!
109105 return Error (
110- repeatedPositionalArgument: firstRepeatedPositionalArgument. help. keys. first!. rawValue ,
111- positionalArgumentFollowingRepeated: positionalFollowingRepeatedArgument. help. keys. first!. rawValue )
106+ repeatedPositionalArgument: firstRepeatedPositionalArgument. help. keys. first!. name ,
107+ positionalArgumentFollowingRepeated: positionalFollowingRepeatedArgument. help. keys. first!. name )
112108 }
113109}
114110
115111/// Ensure that all arguments have corresponding coding keys
116112struct ParsableArgumentsCodingKeyValidator : ParsableArgumentsValidator {
117113
118114 private struct Validator : Decoder {
119- let argumentKeys : [ String ]
115+ let argumentKeys : [ InputKey ]
120116
121117 enum ValidationResult : Swift . Error {
122118 case success
123- case missingCodingKeys( [ String ] )
119+ case missingCodingKeys( [ InputKey ] )
124120 }
125121
126122 let codingPath : [ CodingKey ] = [ ]
@@ -135,7 +131,7 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
135131 }
136132
137133 func container< Key> ( keyedBy type: Key . Type ) throws -> KeyedDecodingContainer < Key > where Key : CodingKey {
138- let missingKeys = argumentKeys. filter { Key ( stringValue: $0) == nil }
134+ let missingKeys = argumentKeys. filter { Key ( stringValue: $0. name ) == nil }
139135 if missingKeys. isEmpty {
140136 throw ValidationResult . success
141137 } else {
@@ -147,7 +143,7 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
147143 /// This error indicates that an option, a flag, or an argument of
148144 /// a `ParsableArguments` is defined without a corresponding `CodingKey`.
149145 struct MissingKeysError : ParsableArgumentsValidatorError , CustomStringConvertible {
150- let missingCodingKeys : [ String ]
146+ let missingCodingKeys : [ InputKey ]
151147
152148 var description : String {
153149 let resolution = """
@@ -194,8 +190,8 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
194190 }
195191 }
196192
197- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
198- let argumentKeys : [ String ] = Mirror ( reflecting: type. init ( ) )
193+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
194+ let argumentKeys : [ InputKey ] = Mirror ( reflecting: type. init ( ) )
199195 . children
200196 . compactMap { child in
201197 guard
@@ -204,7 +200,7 @@ struct ParsableArgumentsCodingKeyValidator: ParsableArgumentsValidator {
204200 else { return nil }
205201
206202 // Property wrappers have underscore-prefixed names
207- return String ( codingKey . first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey . dropFirst ( 0 ) )
203+ return InputKey ( name : codingKey, parent : parent )
208204 }
209205 guard argumentKeys. count > 0 else {
210206 return nil
@@ -239,19 +235,16 @@ struct ParsableArgumentsUniqueNamesValidator: ParsableArgumentsValidator {
239235 var kind : ValidatorErrorKind { . failure }
240236 }
241237
242- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
238+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
243239 let argSets : [ ArgumentSet ] = Mirror ( reflecting: type. init ( ) )
244240 . children
245241 . compactMap { child in
246242 guard
247- var codingKey = child. label,
243+ let codingKey = child. label,
248244 let parsed = child. value as? ArgumentSetProvider
249245 else { return nil }
250246
251- // Property wrappers have underscore-prefixed names
252- codingKey = String ( codingKey. first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey. dropFirst ( 0 ) )
253-
254- let key = InputKey ( rawValue: codingKey)
247+ let key = InputKey ( name: codingKey, parent: parent)
255248 return parsed. argumentSet ( for: key)
256249 }
257250
@@ -290,19 +283,16 @@ struct NonsenseFlagsValidator: ParsableArgumentsValidator {
290283 var kind : ValidatorErrorKind { . warning }
291284 }
292285
293- static func validate( _ type: ParsableArguments . Type ) -> ParsableArgumentsValidatorError ? {
286+ static func validate( _ type: ParsableArguments . Type , parent : InputKey . Parent ) -> ParsableArgumentsValidatorError ? {
294287 let argSets : [ ArgumentSet ] = Mirror ( reflecting: type. init ( ) )
295288 . children
296289 . compactMap { child in
297290 guard
298- var codingKey = child. label,
291+ let codingKey = child. label,
299292 let parsed = child. value as? ArgumentSetProvider
300293 else { return nil }
301294
302- // Property wrappers have underscore-prefixed names
303- codingKey = String ( codingKey. first == " _ " ? codingKey. dropFirst ( 1 ) : codingKey. dropFirst ( 0 ) )
304-
305- let key = InputKey ( rawValue: codingKey)
295+ let key = InputKey ( name: codingKey, parent: parent)
306296 return parsed. argumentSet ( for: key)
307297 }
308298
0 commit comments