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

Allow parsing of -Dkey=value style options #515

Open
Zollerboy1 opened this issue Nov 1, 2022 · 0 comments
Open

Allow parsing of -Dkey=value style options #515

Zollerboy1 opened this issue Nov 1, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@Zollerboy1
Copy link

If I haven't overlooked something, it's currently not possible to define options in the style of make/gcc/clang definitions.

In theory it should be possible to define it like this:

@main
struct Foo: ParsableCommand {
    @Option(name: .customShort("D", allowingJoined: true), parsing: .singleValue)
    var defines: [String] = []

    public func run() throws {
        print(defines)
    }
}

This works fine for calling the executable e.g. like so:

$ foo -Dbar -Dbaz
["bar", "baz"]

However, when the value of the option contains an =, this doesn't work anymore:

$ foo -Dbar=baz
Error: Unknown option '-Dbar'

I'd guess that after seeing an equals sign the single "D" isn't considered anymore. It would be great, if that behaviour could be changed.

Of course, it would be possible to go an extra step and fully parse such key-value pairs into a dictionary. I could imagine a syntax like this:

@main
struct Foo: ParsableCommand {
    @Option(name: [.customShort("D", allowingJoined: true), .customLong("define")])
    var defines: [String: String] = [:]

    public func run() throws {
        print(defines)
    }
}

Where Option gets a new specialized init for Value == [String: Element] that then would work like that:

$ foo -Dbar=1 -D baz=2 --define qux=3
["bar": 1, "baz": 2, "qux": 3]
@natecook1000 natecook1000 added the enhancement New feature or request label Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants