mirror of
https://github.com/apple/swift-argument-parser.git
synced 2026-05-07 20:12:41 +00:00
Improve diagnostic for @Flag Bool? without inversion: (#892)
When a user declares `@Flag(...) var x: Bool?` without the required `inversion:` parameter, overload resolution falls through to the `Flag where Value == Int` initializer and the compiler reports "requires the types 'Bool?' and 'Int' be equivalent", which does not point the user at the real cause. Add an `@available(*, unavailable)` init overload on `Flag where Value == Bool?` with a message that names the missing parameter. Follows the same pattern used for the unavailable no-arg `Flag.init()` at the top of the file. Resolves #835.
This commit is contained in:
@@ -251,6 +251,24 @@ extension Flag where Value == Bool? {
|
||||
exclusivity: exclusivity,
|
||||
help: help)
|
||||
}
|
||||
|
||||
/// Surfaces a clearer diagnostic when a user declares a `Bool?` flag without
|
||||
/// the required `inversion:` parameter.
|
||||
///
|
||||
/// Without this overload, the compiler falls back to the
|
||||
/// `Flag where Value == Int` initializer and reports a confusing
|
||||
/// "types 'Bool?' and 'Int' be equivalent" error.
|
||||
@available(
|
||||
*, unavailable,
|
||||
message:
|
||||
"An optional 'Bool?' @Flag requires an 'inversion:' parameter, such as '.prefixedNo' or '.prefixedEnableDisable'."
|
||||
)
|
||||
public init(
|
||||
name _name: NameSpecification = .long,
|
||||
help: ArgumentHelp? = nil
|
||||
) {
|
||||
fatalError("unavailable")
|
||||
}
|
||||
}
|
||||
|
||||
extension Flag where Value == Bool {
|
||||
|
||||
Reference in New Issue
Block a user