Skip to content

Commit 2b352c0

Browse files
authored
Deprecate Option initializer and add a new one with parameters in order (#391)
1 parent 4cdcc17 commit 2b352c0

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Sources/ArgumentParser/Parsable Properties/Option.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ extension Option where Value: ExpressibleByArgument {
126126
)
127127
}
128128

129+
/// Creates a property with a default value provided by standard Swift default value syntax.
130+
@available(*, deprecated, message: "Swap the order of your 'help' and 'completion' arguments.")
131+
public init(
132+
wrappedValue: Value,
133+
name: NameSpecification = .long,
134+
parsing parsingStrategy: SingleValueParsingStrategy = .next,
135+
completion: CompletionKind?,
136+
help: ArgumentHelp?
137+
) {
138+
self.init(
139+
name: name,
140+
initial: wrappedValue,
141+
parsingStrategy: parsingStrategy,
142+
help: help,
143+
completion: completion)
144+
}
145+
129146
/// Creates a property with a default value provided by standard Swift default value syntax.
130147
///
131148
/// This method is called to initialize an `Option` with a default value such as:
@@ -138,12 +155,13 @@ extension Option where Value: ExpressibleByArgument {
138155
/// - name: A specification for what names are allowed for this flag.
139156
/// - parsingStrategy: The behavior to use when looking for this option's value.
140157
/// - help: Information about how to use this option.
158+
/// - completion: Kind of completion provided to the user for this option.
141159
public init(
142160
wrappedValue: Value,
143161
name: NameSpecification = .long,
144162
parsing parsingStrategy: SingleValueParsingStrategy = .next,
145-
completion: CompletionKind? = nil,
146-
help: ArgumentHelp? = nil
163+
help: ArgumentHelp? = nil,
164+
completion: CompletionKind? = nil
147165
) {
148166
self.init(
149167
name: name,

Tests/ArgumentParserEndToEndTests/DefaultsEndToEndTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,19 @@ extension DefaultsEndToEndTests {
777777
}
778778
}
779779
}
780+
781+
@available(*, deprecated)
782+
fileprivate struct OptionPropertyDeprecatedInit_NoDefault: ParsableArguments {
783+
@Option(completion: .file(), help: "")
784+
var data: String = "test"
785+
}
786+
787+
extension DefaultsEndToEndTests {
788+
/// Tests that instances created using deprecated initializer with completion and help arguments swapped are constructed and parsed correctly.
789+
@available(*, deprecated)
790+
func testParsing_OptionPropertyDeprecatedInit_NoDefault() {
791+
AssertParse(OptionPropertyDeprecatedInit_NoDefault.self, []) { arguments in
792+
XCTAssertEqual(arguments.data, "test")
793+
}
794+
}
795+
}

0 commit comments

Comments
 (0)