Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate Option initializer and add a new one with parameters in order #391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions Sources/ArgumentParser/Parsable Properties/Option.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ extension Option where Value: ExpressibleByArgument {
)
}

/// Creates a property with a default value provided by standard Swift default value syntax.
@available(*, deprecated, message: "Swap the order of your 'help' and 'completion' arguments.")
public init(
wrappedValue: Value,
name: NameSpecification = .long,
parsing parsingStrategy: SingleValueParsingStrategy = .next,
completion: CompletionKind?,
help: ArgumentHelp?
) {
self.init(
name: name,
initial: wrappedValue,
parsingStrategy: parsingStrategy,
help: help,
completion: completion)
}

/// Creates a property with a default value provided by standard Swift default value syntax.
///
/// This method is called to initialize an `Option` with a default value such as:
Expand All @@ -138,12 +155,13 @@ extension Option where Value: ExpressibleByArgument {
/// - name: A specification for what names are allowed for this flag.
/// - parsingStrategy: The behavior to use when looking for this option's value.
/// - help: Information about how to use this option.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a description of the completion parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Contributor Author

@McNight McNight Jan 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I noticed that a lot of completionKind parameters from other initializers are not documented. What should we do about those ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth doing in a separate pull request (if you're up to it)

/// - completion: Kind of completion provided to the user for this option.
public init(
wrappedValue: Value,
name: NameSpecification = .long,
parsing parsingStrategy: SingleValueParsingStrategy = .next,
completion: CompletionKind? = nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't ABI stable if I recall correctly, but im not sure if that matters for this library. I wonder if you could leave types as Optional with a default of nil and use @_disfavoredOverload to prefer using your new initializer.

@natecook1000 to comment on if this is necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about @_disfavoredOverload. Indeed, it seems to do the trick (when re-establishing optionals).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to worry about ABI stability, since this isn't shipping as a binary library, but for source stability we need the parameter types to stay the same. The default values can go, however, which should be enough to prevent ambiguity between this deprecated version and the new one.

help: ArgumentHelp? = nil
help: ArgumentHelp? = nil,
completion: CompletionKind? = nil
) {
self.init(
name: name,
Expand Down
16 changes: 16 additions & 0 deletions Tests/ArgumentParserEndToEndTests/DefaultsEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -777,3 +777,19 @@ extension DefaultsEndToEndTests {
}
}
}

@available(*, deprecated)
fileprivate struct OptionPropertyDeprecatedInit_NoDefault: ParsableArguments {
@Option(completion: .file(), help: "")
var data: String = "test"
}

extension DefaultsEndToEndTests {
/// Tests that instances created using deprecated initializer with completion and help arguments swapped are constructed and parsed correctly.
@available(*, deprecated)
func testParsing_OptionPropertyDeprecatedInit_NoDefault() {
AssertParse(OptionPropertyDeprecatedInit_NoDefault.self, []) { arguments in
XCTAssertEqual(arguments.data, "test")
}
}
}