Skip to content

Commit 108b9e9

Browse files
committed
Improve parsing error message
1 parent 7b34b90 commit 108b9e9

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

library/src/scala/annotation/newMain.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,23 @@ final class newMain extends MainAnnotation[FromString, Any]:
314314
}
315315
end explain
316316

317-
private def convert[T](argName: String, arg: String, p: FromString[T]): () => T =
318-
p.fromStringOption(arg) match
319-
case Some(t) => () => t
320-
case None => error(s"invalid argument for $argName: $arg")
321-
322317
def argGetter[T](param: Parameter, arg: String, defaultArgument: Option[() => T])(using p: FromString[T]): () => T = {
323-
if arg.nonEmpty then convert(param.name, arg, p)
318+
if arg.nonEmpty then parse[T](param, arg)
324319
else defaultArgument match
325320
case Some(defaultGetter) => defaultGetter
326321
case None => error(s"missing argument for ${param.name}")
327322
}
328323

329324
def varargGetter[T](param: Parameter, args: Seq[String])(using p: FromString[T]): () => Seq[T] = {
330-
val getters = args.map(arg => convert(param.name, arg, p))
325+
val getters = args.map(arg => parse[T](param, arg))
331326
() => getters.map(_())
332327
}
333328

329+
private def parse[T](param: Parameter, arg: String)(using p: FromString[T]): () => T =
330+
p.fromStringOption(arg) match
331+
case Some(t) => () => t
332+
case None => error(s"could not parse argument for `${param.name}` of type ${param.typeName.split('.').last}: $arg")
333+
334334
def run(execProgram: () => Any): Unit = {
335335
if errors.nonEmpty then
336336
for msg <- errors do println(s"Error: $msg")

tests/run/main-annotation-help-override.check

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Usage: helpOverride1 [--notHelp] <Int>
44
A method that should let --help and -h display help.
55
Arguments:
66
--notHelp - Int
7-
Error: invalid argument for help: --help
7+
Error: could not parse argument for `help` of type Int: --help
88
Usage: helpOverride2 [--help] <Int>
99
Usage: helpOverride3 [-h] <Int>
1010

@@ -13,7 +13,7 @@ Arguments:
1313
-h - Int
1414
Error: missing argument for h
1515
Usage: helpOverride4 [--help] <Int> [-h] <Int>
16-
Error: invalid argument for notHelp: --help
16+
Error: could not parse argument for `notHelp` of type Int: --help
1717
Usage: helpOverride5 [--notHelp | --help] <Int>
1818
Usage: helpOverride6 [--notHelp | -h] <Int>
1919

@@ -22,7 +22,7 @@ Arguments:
2222
--notHelp (-h) - Int
2323
Error: missing argument for notH
2424
Usage: helpOverride7 [--notHelp | --help] <Int> [--notH | -h] <Int>
25-
Error: invalid argument for notHelp: --help
25+
Error: could not parse argument for `notHelp` of type Int: --help
2626
Usage: helpOverride8 [--notHelp | --help | -h] <Int>
2727
##### -h
2828
Usage: helpOverride1 [--notHelp] <Int>
@@ -35,7 +35,7 @@ Usage: helpOverride2 [--help] <Int>
3535
A method that should let -h display help, but not --help.
3636
Arguments:
3737
--help - Int
38-
Error: invalid argument for h: -h
38+
Error: could not parse argument for `h` of type Int: -h
3939
Usage: helpOverride3 [-h] <Int>
4040
Error: missing argument for h
4141
Usage: helpOverride4 [--help] <Int> [-h] <Int>
@@ -44,9 +44,9 @@ Usage: helpOverride5 [--notHelp | --help] <Int>
4444
A method that should let -h display help, but not --help.
4545
Arguments:
4646
--notHelp (--help) - Int
47-
Error: invalid argument for notHelp: -h
47+
Error: could not parse argument for `notHelp` of type Int: -h
4848
Usage: helpOverride6 [--notHelp | -h] <Int>
4949
Error: missing argument for notH
5050
Usage: helpOverride7 [--notHelp | --help] <Int> [--notH | -h] <Int>
51-
Error: invalid argument for notHelp: -h
51+
Error: could not parse argument for `notHelp` of type Int: -h
5252
Usage: helpOverride8 [--notHelp | --help | -h] <Int>
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
Error: invalid argument for inc: true
1+
Error: could not parse argument for `inc` of type Int: true
22
Usage: add [--num] <Int> [--inc] <Int>
3-
Error: invalid argument for num: 2.1
3+
Error: could not parse argument for `num` of type Int: 2.1
44
Usage: add [--num] <Int> [--inc] <Int>
5-
Error: invalid argument for inc: 3.1415921535
5+
Error: could not parse argument for `inc` of type Int: 3.1415921535
66
Usage: add [--num] <Int> [--inc] <Int>
7-
Error: invalid argument for num: 192.168.1.1
7+
Error: could not parse argument for `num` of type Int: 192.168.1.1
88
Usage: add [--num] <Int> [--inc] <Int>
9-
Error: invalid argument for num: false
10-
Error: invalid argument for inc: true
9+
Error: could not parse argument for `num` of type Int: false
10+
Error: could not parse argument for `inc` of type Int: true
1111
Usage: add [--num] <Int> [--inc] <Int>
12-
Error: invalid argument for num: Hello
13-
Error: invalid argument for inc: world!
12+
Error: could not parse argument for `num` of type Int: Hello
13+
Error: could not parse argument for `inc` of type Int: world!
1414
Usage: add [--num] <Int> [--inc] <Int>

0 commit comments

Comments
 (0)