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

Fix argument parsing of flags in the presence of allowPositional=true #66

Merged
merged 2 commits into from
Apr 29, 2023

Conversation

lihaoyi
Copy link
Member

@lihaoyi lihaoyi commented Apr 29, 2023

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.Flags. for which being positional doesn't make sense.

    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; Flags, Leftover, etc. cannot

  2. Keyword arguments are limited only to Flags 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 merged commit a9e4c5e into main Apr 29, 2023
@lihaoyi lihaoyi deleted the dev branch April 29, 2023 01:21
@lefou lefou added this to the 0.5.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

Successfully merging this pull request may close these issues.

Possible argument parsing error
2 participants