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

Incorrect handling of arguments with the same property name #322

Closed
2 tasks done
rauhul opened this issue Jun 3, 2021 · 3 comments
Closed
2 tasks done

Incorrect handling of arguments with the same property name #322

rauhul opened this issue Jun 3, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@rauhul
Copy link
Contributor

rauhul commented Jun 3, 2021

Swift argument parser doesn't handle properties with the same name under different option groups gracefully.

The following snippet throws an error when run with no arguments, but I would expect no required arguments:

import ArgumentParser

struct Foo: ParsableCommand {
    @Option(name: .long)
    var `bar`: String = ""

    @OptionGroup
    var group: Group
    
    func run() throws { }
}

struct Group: ParsableArguments {
    @Option(name: .customLong("baz"))
    var `bar`: String?
}

Foo.main()

Output:

Error: Missing one of: '--bar <bar>', '--baz <baz>'

USAGE: foo [--bar <bar>] [--baz <baz>]

OPTIONS:
  --bar <bar>
  --baz <baz>
  -h, --help              Show help information.

ArgumentParser version: 0.4.3
Swift version:

swift-driver version: 1.20.1 Apple Swift version 5.5 (swiftlang-1300.0.13.7 clang-1300.0.15.1)
Target: arm64-apple-macosx11.0

Checklist

  • If possible, I've reproduced the issue using the main branch of this package
  • I've searched for existing GitHub issues
@rauhul rauhul added the bug Something isn't working label Jun 3, 2021
@bscothern
Copy link

Just ran into this issue between a few OptionGroups I have defining common values between commands. The hack I used to get around the issue for now was to namespace the actual parsed value like this:

public struct ExampleOption: ParsableArguments {
    @Option(name: [.customLong("foo")])
    var _valueExampleOption: Int // Add a prefix _ and a suffix of the type name
    public var value: Int { _valueExampleOption }

    public init() {}
}

This let me preserve the usage of value while making it so things could properly parse and function.

@randomeizer
Copy link
Contributor

Yeah, I've had the same issue as @bscothern with regards to having two ParsableArguments instances with the same internal field (eg. var enabled: Bool, in my case) and different .customLong(...) values, and they both overwrite each other. In my case, it's a @Flag.

If anyone has any pointers on where to start and/or the appropriate section to add a test case, I'm happy to investigate.

@randomeizer
Copy link
Contributor

I've tracked down the source of the bug, and have a PR at #495. If you want to test out my solution, my fix is in the following branch on my fork of the project:

https://github.com/randomeizer/swift-argument-parser/tree/randomeizer/322-duplicate-fields-in-OptionGroup

natecook1000 pushed a commit that referenced this issue Sep 22, 2022
…name+type as the ParseCommand (Issue #322) (#495)

* Contains fixes and test cases for #322
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants