@@ -129,14 +129,17 @@ extension Flag where Value == Optional<Bool> {
129129 /// - name: A specification for what names are allowed for this flag.
130130 /// - inversion: The method for converting this flags name into an on/off
131131 /// pair.
132+ /// - exclusivity: The behavior to use when an on/off pair of flags is
133+ /// specified.
132134 /// - help: Information about how to use this flag.
133135 public init (
134136 name: NameSpecification = . long,
135137 inversion: FlagInversion ,
138+ exclusivity: FlagExclusivity = . chooseLast,
136139 help: ArgumentHelp ? = nil
137140 ) {
138141 self . init ( _parsedValue: . init { key in
139- . flag( key: key, name: name, default: nil , inversion: inversion, help: help)
142+ . flag( key: key, name: name, default: nil , inversion: inversion, exclusivity : exclusivity , help: help)
140143 } )
141144 }
142145}
@@ -188,15 +191,18 @@ extension Flag where Value == Bool {
188191 /// - initial: The default value for this flag.
189192 /// - inversion: The method for converting this flag's name into an on/off
190193 /// pair.
194+ /// - exclusivity: The behavior to use when an on/off pair of flags is
195+ /// specified.
191196 /// - help: Information about how to use this flag.
192197 public init (
193198 name: NameSpecification = . long,
194199 default initial: Bool ? = false ,
195200 inversion: FlagInversion ,
201+ exclusivity: FlagExclusivity = . chooseLast,
196202 help: ArgumentHelp ? = nil
197203 ) {
198204 self . init ( _parsedValue: . init { key in
199- . flag( key: key, name: name, default: initial, inversion: inversion, help: help)
205+ . flag( key: key, name: name, default: initial, inversion: inversion, exclusivity : exclusivity , help: help)
200206 } )
201207 }
202208}
@@ -248,17 +254,7 @@ extension Flag where Value: CaseIterable, Value: RawRepresentable, Value.RawValu
248254 return ArgumentDefinition . flag ( name: name, key: key, caseKey: caseKey, help: help, parsingStrategy: . nextAsValue, initialValue: initial, update: . nullary( { ( origin, name, values) in
249255 // TODO: We should catch duplicate flags that hit a single part of
250256 // an exclusive argument set in the value parsing, not here.
251- let previous = values. element ( forKey: key)
252- switch ( hasUpdated, previous, exclusivity) {
253- case ( true , let p? , . exclusive) :
254- // This value has already been set.
255- throw ParserError . duplicateExclusiveValues ( previous: p. inputOrigin, duplicate: origin, originalInput: values. originalInput)
256- case ( false , _, _) , ( _, _, . chooseLast) :
257- values. set ( value, forKey: key, inputOrigin: origin)
258- default :
259- break
260- }
261- hasUpdated = true
257+ hasUpdated = try ArgumentSet . updateFlag ( key: key, value: value, origin: origin, values: & values, hasUpdated: hasUpdated, exclusivity: exclusivity)
262258 } ) )
263259 }
264260 return exclusivity == . exclusive
@@ -293,13 +289,9 @@ extension Flag {
293289 let caseKey = InputKey ( rawValue: value. rawValue)
294290 let help = ArgumentDefinition . Help ( options: . isOptional, help: help, key: key)
295291 return ArgumentDefinition . flag ( name: name, key: key, caseKey: caseKey, help: help, parsingStrategy: . nextAsValue, initialValue: nil as Element ? , update: . nullary( { ( origin, name, values) in
296- if hasUpdated && exclusivity == . exclusive {
297- throw ParserError . unexpectedExtraValues ( [ ( origin, String ( describing: value) ) ] )
298- }
299- if !hasUpdated || exclusivity == . chooseLast {
300- values. set ( value, forKey: key, inputOrigin: origin)
301- }
302- hasUpdated = true
292+ // TODO: We should catch duplicate flags that hit a single part of
293+ // an exclusive argument set in the value parsing, not here.
294+ hasUpdated = try ArgumentSet . updateFlag ( key: key, value: value, origin: origin, values: & values, hasUpdated: hasUpdated, exclusivity: exclusivity)
303295 } ) )
304296 }
305297 return exclusivity == . exclusive
0 commit comments