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

Make combine short args that fail to parse go through normal leftover-token code paths #112

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions mainargs/src/TokenGrouping.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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[_])] = {
Expand All @@ -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(_ != '-')) {
Expand Down
10 changes: 4 additions & 6 deletions mainargs/test/src/FlagTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)

Expand All @@ -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)
)
}

Expand Down
26 changes: 25 additions & 1 deletion mainargs/test/src/PositionalTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
}
15 changes: 15 additions & 0 deletions mainargs/test/src/VarargsBaseTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
}
}
}
Loading