-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix argument parsing of flags in the presence of allowPositional=true (…
…#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
- Loading branch information
Showing
6 changed files
with
57 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mainargs | ||
import utest._ | ||
|
||
object IssueTests extends TestSuite { | ||
|
||
object Main { | ||
@main | ||
def mycmd(@arg(name = "the-flag") f: mainargs.Flag = mainargs.Flag(false), | ||
@arg str: String = "s", | ||
args: Leftover[String]) = { | ||
(f.value, str, args.value) | ||
} | ||
} | ||
|
||
val tests = Tests { | ||
test("issue60") { | ||
test { | ||
val parsed = ParserForMethods(Main) | ||
.runEither(Seq("--str", "str", "a", "b", "c", "d"), allowPositional = true) | ||
|
||
assert(parsed == Right((false, "str", List("a", "b", "c", "d")))) | ||
} | ||
test { | ||
val parsed = ParserForMethods(Main) | ||
.runEither(Seq("a", "b", "c", "d"), allowPositional = true) | ||
|
||
assert(parsed == Right((false, "a", List("b", "c", "d")))) | ||
} | ||
} | ||
} | ||
} |