Skip to content

Unable to set default value of String-typed option with help and completion #381

@stevapple

Description

@stevapple

Let's say, it's possible to configure a String-typed option:

  • with help text;
  • with completion;
  • with default value.

In fact, we can do either two of these, but combining three of these will cause compilation failure.

ArgumentParser version: 1.0.2, main
Swift version: swift-driver version: 1.26.21 Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)

Checklist

  • If possible, I've reproduced the issue using the main branch of this package
  • I've searched for existing GitHub issues

Steps to Reproduce

Minimal code to reproduce:

import ArgumentParser

struct Run: ParsableCommand {
    @Option(help: "", completion: .file())
    var string: String = ""

    func run() {}
}

Similar bug with String?:

@Option(completion: .file())
var string: String? = ""

Expected behavior

Compile successfully.

Actual behavior

main.swift:5:26: error: cannot convert value of type 'String' to expected argument type '[String]'
    var string: String = ""
                         ^
main.swift:5:9: error: property type 'String' does not match 'wrappedValue' type 'Array<String>'
    var string: String = ""
        ^~~~~~
main.swift:5:9: note: cannot automatically synthesize 'Decodable' because '<<error type>>' does not conform to 'Decodable'
    var string: String = ""
        ^
main.swift:3:8: error: type 'Run' does not conform to protocol 'Decodable'
struct Run: ParsableCommand {
       ^
Swift.Decodable:2:5: note: protocol requires initializer 'init(from:)' with type 'Decodable'
    init(from decoder: Decoder) throws
    ^

Workaround

For anyone who encountered such error, you can workaround with a non-op transform:

// ✅ It works!
@Option(help: "", completion: .file(), transform: {String($0)})
var string: String = ""

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions