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

Leftover args not parsed as expected #60

Closed
zhranklin opened this issue Apr 11, 2023 · 0 comments
Closed

Leftover args not parsed as expected #60

zhranklin opened this issue Apr 11, 2023 · 0 comments
Milestone

Comments

@zhranklin
Copy link

I'm using mainargs in ammonite, but the result is not correct.

code

run in ammonite 2.5.8/scala 3.2.1/java 17

// filename: bug.sc
import mainargs.Leftover
@main
def mycmd(@arg(name = "the-flag") f: mainargs.Flag = mainargs.Flag(false), @arg str: String = "s", args: Leftover[String]) = {
  println(f)
  println(str)
  println(args)
}

case 1

amm bug.sc --str str a b c d

expected:

Flag(false)
str
Leftover(List(a, b, c, d))

actual:

Flag(true)
str
Leftover(List(b, c, d))

case 2

amm bug.sc a b c d

expected:

 Flag(false)
 s
 Leftover(List(a, b, c, d))

actual:

 Flag(true)
 b
 Leftover(List(c, d))
lihaoyi added a commit that referenced this issue Apr 29, 2023
…#66)

Should fix #58 and
#60

Previously, we allowed any arg to take positional arguments if
`allowPositional = true` (which is the case for Ammonite and Mill
user-defined entrypoints.), even `mainargs.Flag`s. for which being
positional doesn't make sense.

```scala
    val positionalArgSigs = argSigs
      .filter {
        case x: ArgSig.Simple[_, _] if x.reader.noTokens => false
        case x: ArgSig.Simple[_, _] if x.positional => true
        case x => allowPositional
      }
```

The relevant code path was rewritten in
#62, but the buggy behavior
was preserved before and after that change. This wasn't caught in other
uses of `mainargs.Flag`, e.g. for Ammonite/Mill's own flags, because
those did not set `allowPositional=true`

This PR tweaks `TokenGrouping.groupArgs` to be more discerning about how
it selects positional, keyword, and missing arguments:

1. Now, only `TokenReader.Simple[_]`s with `.positional` or
`allowPositional` can be positional; `Flag`s, `Leftover`, etc. cannot

2. Keyword arguments are limited only to `Flag`s and `Simple` with
`!a.positional`

Added `mainargs.IssueTests.issue60` as a regression test, that fails on
main and passes on this PR. Existing tests all pass
@lihaoyi lihaoyi closed this as completed Apr 29, 2023
@lefou lefou added this to the after 0.4.0 milestone Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants