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

Option not being parsed correctly #371

Open
philippzagar opened this issue Nov 8, 2021 · 0 comments
Open

Option not being parsed correctly #371

philippzagar opened this issue Nov 8, 2021 · 0 comments
Labels
bug Something isn't working

Comments

@philippzagar
Copy link

philippzagar commented Nov 8, 2021

The Argument Parser isn't able to parse an Option correctly in a very specific parameter setting (see example below). This may even result in wrongly parsed values.

ArgumentParser version: 1.0.1
Swift version: swift-driver version: 1.26.9 Apple Swift version 5.5.1 (swiftlang-1300.0.31.4 clang-1300.0.29.6) Target: x86_64-apple-macosx12.0

Steps to Reproduce

The Argument Parser isn't able to parse an Option correctly if it is stated between a "normal" Argument and an Argument with the unconditionalRemaining setting.

Here an example:

struct ExampleCommand: ParsableCommand {
    @Argument(help: "Works as expected")
    var firstArg: String
    
    @Option(help: "Not recognized argument")
    var secondArg: String
    
    @Argument(parsing: .unconditionalRemaining, help:"Failure causing argument, especially the `unconditionalRemaining` option")
    var thirdArg: [String]
}

swift run ArgumentParserExample --help prints the following (for all tested swift-argument-parser versions, so v0.4.4 onwards):

OVERVIEW: Example Command that showcases bug in the swift-argument-parser

USAGE: example-command <first-arg> --second-arg <second-arg> [<third-arg> ...]

ARGUMENTS:
  <first-arg>             Works as expected 
  <third-arg>             Failure causing argument, especially the `unconditionalRemaining` option 

OPTIONS:
  --second-arg <second-arg>
                          Not recognized argument 
  --version               Show the version.
  -h, --help              Show help information.

A related run Command could look like the following: swift run ArgumentParserExample test1 --second-arg=test2 test3 --test4 -t

Repository containing example code reproducing the behavior described below. Try the different swift-argument-parser versions in the Package.swift file.

Expected behavior

One would expect that all stated CLI parameters are parsed correctly and that they can be accessed containing the correct values.
Especially, the secondArg parameter ( --second-arg ) is detected and parsed correctly.
Occurs correctly on v0.4.4 of the swift-argument-parser.

However, the thirdArg actually contains the --second-arg=test2 from the run CMD mentioned above. This shouldn't be the case, as only the secondArg should contain the correct value (test2), which it also does in this scenario.

Actual behavior

Starting with the swift-argument-parser v0.5 and up until the current v1.0.1 version, the parsing fails with the error message that the secondArg is missing (again, using the run CMD mentioned above):

Error: Missing expected argument '--second-arg <second-arg>'
Help:  --second-arg <second-arg>  Not recognized argument
Usage: example-command <first-arg> --second-arg <second-arg> [<third-arg> ...]
  See 'example-command --help' for more information.

Setting the name of the Option explicitly to second-arg (so --second-arg) within the initialization of the Option property wrapper doesn't change the behavior.

As soon as the unconditionalRemaining option for the last Argument is deleted, the parsing is done correctly, under the condition that the passed parameters after the --second-arg are not Option's on their own (so no --test4 or -t anymore). Same behavior as soon as the first Argument (so firstArg) is deleted.
As described in the example above, the secondArg parameter ( --second-arg ) isn't being detected correctly.

It seems to me that the .unconditionalRemaining option to a certain extent is responsible for this incorrect behavior. However, as mentioned above, if the firstArg is deleted, the parsing is done correctly, even with the .unconditionalRemaining option (even with correct values of the parsed parameters!).

@natecook1000 natecook1000 added the bug Something isn't working label Mar 15, 2022
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

2 participants