From c59e47b21fdf1b4f3739640ccef84a122e8e245e Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Wed, 7 Feb 2024 16:14:10 +0800 Subject: [PATCH] . --- mainargs/src/TokenGrouping.scala | 13 ++++++------ mainargs/test/src/FlagTests.scala | 10 ++++----- mainargs/test/src/PositionalTests.scala | 26 +++++++++++++++++++++++- mainargs/test/src/VarargsBaseTests.scala | 15 ++++++++++++++ 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/mainargs/src/TokenGrouping.scala b/mainargs/src/TokenGrouping.scala index dd3feda..8ff345a 100644 --- a/mainargs/src/TokenGrouping.scala +++ b/mainargs/src/TokenGrouping.scala @@ -57,7 +57,7 @@ object TokenGrouping { var rest2 = rest var i = 0 var currentMap = current - var failure: Result.Failure = null + var failure = false while (i < chars.length) { val c = chars(i) @@ -79,7 +79,7 @@ object TokenGrouping { rest2 match { case Nil => // If there is no next token, it is an error - failure = Result.Failure.MismatchedArguments(incomplete = Some(a)) + failure = true case next :: remaining => currentMap = Util.appendMap(currentMap, a, next) rest2 = remaining @@ -88,15 +88,14 @@ object TokenGrouping { case None => // If we encounter a character that is neither a short flag or a // short argument, it is an error - failure = Result.Failure.MismatchedArguments(unknown = Seq("-" + c.toString)) + failure = true } i = chars.length } } - if (failure != null) Left(failure) - else Right((rest2, currentMap)) + if (failure) None else Some((rest2, currentMap)) } def lookupArgMap(k: String, m: Map[String, ArgSig]): Option[(ArgSig, mainargs.TokensReader[_])] = { @@ -112,8 +111,8 @@ object TokenGrouping { // special handling for combined short args of the style "-xvf" or "-j10" if (head.startsWith("-") && head.lift(1).exists(c => c != '-')){ parseCombinedShortTokens(current, head, rest) match{ - case Left(failure) => failure - case Right((rest2, currentMap)) => rec(rest2, currentMap) + case None => complete(remaining, current) + case Some((rest2, currentMap)) => rec(rest2, currentMap) } } else if (head.startsWith("-") && head.exists(_ != '-')) { diff --git a/mainargs/test/src/FlagTests.scala b/mainargs/test/src/FlagTests.scala index d99dff0..8a6bef2 100644 --- a/mainargs/test/src/FlagTests.scala +++ b/mainargs/test/src/FlagTests.scala @@ -112,12 +112,10 @@ object FlagTests extends TestSuite { test - check( List("bool", "-ab"), Result.Failure.MismatchedArguments( + Vector(new ArgSig(None, Some('b'), None, None, TokensReader.BooleanRead, false, false)), + List("-ab"), Nil, - Nil, - Nil, - Some( - new ArgSig(None, Some('b'), None, None, TokensReader.BooleanRead, false, false) - ) + None ) ) @@ -128,7 +126,7 @@ object FlagTests extends TestSuite { test - check( List("str", "-bvalue", "-akey=value"), - Result.Failure.MismatchedArguments(Nil, List("-k"), Nil, None) + Result.Failure.MismatchedArguments(Nil, List("-akey=value"), Nil, None) ) } diff --git a/mainargs/test/src/PositionalTests.scala b/mainargs/test/src/PositionalTests.scala index 683e5d4..c68cf11 100644 --- a/mainargs/test/src/PositionalTests.scala +++ b/mainargs/test/src/PositionalTests.scala @@ -44,7 +44,31 @@ object PositionalTests extends TestSuite { ) test - check( List("-x", "true", "-y", "false", "-z", "false"), - Result.Failure.MismatchedArguments(List(), List("-y"), List(), None) + Result.Failure.MismatchedArguments( + List( + ArgSig( + None, + Some('y'), + None, + None, + TokensReader.BooleanRead, + positional = true, + hidden = false + ), + ArgSig( + None, + Some('z'), + None, + None, + TokensReader.BooleanRead, + positional = false, + hidden = false + ) + ), + List("-y", "false", "-z", "false"), + List(), + None + ) ) } } diff --git a/mainargs/test/src/VarargsBaseTests.scala b/mainargs/test/src/VarargsBaseTests.scala index 5b67bab..a8566ba 100644 --- a/mainargs/test/src/VarargsBaseTests.scala +++ b/mainargs/test/src/VarargsBaseTests.scala @@ -113,5 +113,20 @@ trait VarargsBaseTests extends TestSuite { ) => } } + + test("failedCombinedShortArgsGoToLeftover"){ + test - check( + List("mixedVariadic", "-f", "123", "abc", "xyz"), + Result.Success("123abcxyz") + ) + test - check( + List("mixedVariadic", "-f123", "456", "abc", "xyz"), + Result.Success("123456abcxyz") + ) + test - check( + List("mixedVariadic", "-f123", "-unknown", "456", "abc", "xyz"), + Result.Success("123-unknown456abcxyz") + ) + } } }