Skip to content

Commit 1d04a6b

Browse files
authored
Make combine short args that fail to parse go through normal leftover-token code paths (#112)
Previously, we aggressively errored out, which prevented the normal `def complete` logic from happening: reporting missing arguments, passing remaining tokens to `Leftover`, etc. This change makes it so that we just go through the normal code paths when combined short args fail to parse, making the error reporting more consistent with the rest of MainArgs and allowing tokens such as `-foo` to be passed to `Leftover` when possible Covered by additional unit tests
1 parent ab790e1 commit 1d04a6b

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

mainargs/src/TokenGrouping.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object TokenGrouping {
5757
var rest2 = rest
5858
var i = 0
5959
var currentMap = current
60-
var failure: Result.Failure = null
60+
var failure = false
6161

6262
while (i < chars.length) {
6363
val c = chars(i)
@@ -79,7 +79,7 @@ object TokenGrouping {
7979
rest2 match {
8080
case Nil =>
8181
// If there is no next token, it is an error
82-
failure = Result.Failure.MismatchedArguments(incomplete = Some(a))
82+
failure = true
8383
case next :: remaining =>
8484
currentMap = Util.appendMap(currentMap, a, next)
8585
rest2 = remaining
@@ -88,15 +88,14 @@ object TokenGrouping {
8888
case None =>
8989
// If we encounter a character that is neither a short flag or a
9090
// short argument, it is an error
91-
failure = Result.Failure.MismatchedArguments(unknown = Seq("-" + c.toString))
91+
failure = true
9292
}
9393
i = chars.length
9494
}
9595

9696
}
9797

98-
if (failure != null) Left(failure)
99-
else Right((rest2, currentMap))
98+
if (failure) None else Some((rest2, currentMap))
10099
}
101100

102101
def lookupArgMap(k: String, m: Map[String, ArgSig]): Option[(ArgSig, mainargs.TokensReader[_])] = {
@@ -112,8 +111,8 @@ object TokenGrouping {
112111
// special handling for combined short args of the style "-xvf" or "-j10"
113112
if (head.startsWith("-") && head.lift(1).exists(c => c != '-')){
114113
parseCombinedShortTokens(current, head, rest) match{
115-
case Left(failure) => failure
116-
case Right((rest2, currentMap)) => rec(rest2, currentMap)
114+
case None => complete(remaining, current)
115+
case Some((rest2, currentMap)) => rec(rest2, currentMap)
117116
}
118117

119118
} else if (head.startsWith("-") && head.exists(_ != '-')) {

mainargs/test/src/FlagTests.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ object FlagTests extends TestSuite {
112112
test - check(
113113
List("bool", "-ab"),
114114
Result.Failure.MismatchedArguments(
115+
Vector(new ArgSig(None, Some('b'), None, None, TokensReader.BooleanRead, false, false)),
116+
List("-ab"),
115117
Nil,
116-
Nil,
117-
Nil,
118-
Some(
119-
new ArgSig(None, Some('b'), None, None, TokensReader.BooleanRead, false, false)
120-
)
118+
None
121119
)
122120
)
123121

@@ -128,7 +126,7 @@ object FlagTests extends TestSuite {
128126

129127
test - check(
130128
List("str", "-bvalue", "-akey=value"),
131-
Result.Failure.MismatchedArguments(Nil, List("-k"), Nil, None)
129+
Result.Failure.MismatchedArguments(Nil, List("-akey=value"), Nil, None)
132130
)
133131
}
134132

mainargs/test/src/PositionalTests.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,31 @@ object PositionalTests extends TestSuite {
4444
)
4545
test - check(
4646
List("-x", "true", "-y", "false", "-z", "false"),
47-
Result.Failure.MismatchedArguments(List(), List("-y"), List(), None)
47+
Result.Failure.MismatchedArguments(
48+
List(
49+
ArgSig(
50+
None,
51+
Some('y'),
52+
None,
53+
None,
54+
TokensReader.BooleanRead,
55+
positional = true,
56+
hidden = false
57+
),
58+
ArgSig(
59+
None,
60+
Some('z'),
61+
None,
62+
None,
63+
TokensReader.BooleanRead,
64+
positional = false,
65+
hidden = false
66+
)
67+
),
68+
List("-y", "false", "-z", "false"),
69+
List(),
70+
None
71+
)
4872
)
4973
}
5074
}

mainargs/test/src/VarargsBaseTests.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,20 @@ trait VarargsBaseTests extends TestSuite {
113113
) =>
114114
}
115115
}
116+
117+
test("failedCombinedShortArgsGoToLeftover"){
118+
test - check(
119+
List("mixedVariadic", "-f", "123", "abc", "xyz"),
120+
Result.Success("123abcxyz")
121+
)
122+
test - check(
123+
List("mixedVariadic", "-f123", "456", "abc", "xyz"),
124+
Result.Success("123456abcxyz")
125+
)
126+
test - check(
127+
List("mixedVariadic", "-f123", "-unknown", "456", "abc", "xyz"),
128+
Result.Success("123-unknown456abcxyz")
129+
)
130+
}
116131
}
117132
}

0 commit comments

Comments
 (0)