swift-argument-parser icon indicating copy to clipboard operation
swift-argument-parser copied to clipboard

@Flag claims to support Bool? but doesn't?

Open ktoso opened this issue 1 month ago • 1 comments

The following should be supported but isn't:

    @Flag(help: "...")
    var writeEmptyFiles: Bool?

The error is

Referencing initializer 'init(name:help:)' on 'Flag' requires the types 'Bool?' and 'Int' be equivalent

It turns out the inversion parameter is required, so the following works:

    @Flag(inversion: .prefixedEnableDisable, help: "")
    var writeEmptyFiles: Bool?

this seems somewhat accidental? Are we just missing an initializer with defaulted inversion?

ArgumentParser version: 0.1.0 or the main branch, for example. Swift version: swift-driver version: 1.139 Apple Swift version 6.2 (swiftlang-6.3.0.101.15 clang-2100.0.101.15); arm64-apple-macosx26.0

Checklist

Steps to Reproduce

The snippets above are enough to repro

Expected behavior

We should support just @Flag() on optional boolean fields

Actual behavior

fails to compile

ktoso avatar Dec 09 '25 05:12 ktoso

Seems this is just because we don't provide a default, we should add one -- ill follow up on it

ktoso avatar Dec 09 '25 11:12 ktoso

This functionality is supported, but the overloads we have lead to a bad compiler response. I'm not sure if there's a way to improve it, but something that could point out instead that an initial value is required when the type is Bool? would be great!

natecook1000 avatar Dec 15 '25 14:12 natecook1000