Skip to content

Commit 55ebf8b

Browse files
committed
Exit on main arg parse error
1 parent 7976598 commit 55ebf8b

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

compiler/src/dotty/tools/dotc/ast/MainProxies.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import core.*
55
import Symbols.*, Types.*, Contexts.*, Decorators.*, util.Spans.*, Flags.*, Constants.*
66
import StdNames.{nme, tpnme}
77
import ast.Trees.*
8-
import Names.Name
8+
import Names.{Name, termName}
99
import Comments.Comment
1010
import NameKinds.DefaultGetterName
1111
import Annotations.Annotation
@@ -94,7 +94,12 @@ object MainProxies {
9494
val handler = CaseDef(
9595
Typed(errVar, TypeTree(defn.CLP_ParseError.typeRef)),
9696
EmptyTree,
97-
Apply(ref(defn.CLP_showError.termRef), errVar :: Nil))
97+
Block(
98+
List(Apply(ref(defn.CLP_showError.termRef), errVar :: Nil)),
99+
Apply(Select(Select(Select(Ident(termName("java")), termName("lang")), termName("System")), termName("exit")),
100+
List(Literal(Constant(1))))
101+
)
102+
)
98103
val body = Try(call, handler :: Nil, EmptyTree)
99104
val mainArg = ValDef(nme.args, TypeTree(defn.ArrayType.appliedTo(defn.StringType)), EmptyTree)
100105
.withFlags(Param)

library/src/scala/util/CommandLineParser.scala

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package scala.util
22

3+
import scala.util.control.ControlThrowable
4+
35
/** A utility object to support command line parsing for @main methods */
46
object CommandLineParser {
57

68
/** An exception raised for an illegal command line
7-
* @param idx The index of the argument that's faulty (starting from 0)
8-
* @param msg The error message
9-
*/
10-
class ParseError(val idx: Int, val msg: String) extends Exception
9+
* @param idx The index of the argument that's faulty (starting from 0)
10+
* @param msg The error message
11+
*/
12+
class ParseError(val idx: Int, val msg: String) extends ControlThrowable(msg)
1113

1214
/** Parse command line argument `s`, which has index `n`, as a value of type `T`
1315
* @throws ParseError if argument cannot be converted to type `T`.
1416
*/
15-
def parseString[T](str: String, n: Int)(using fs: FromString[T]): T = {
17+
def parseString[T](str: String, n: Int)(using fs: FromString[T]): T =
1618
try fs.fromString(str)
17-
catch {
18-
case ex: IllegalArgumentException => throw ParseError(n, ex.toString)
19-
}
20-
}
19+
catch case ex: IllegalArgumentException => throw ParseError(n, ex.toString)
2120

2221
/** Parse `n`'th argument in `args` (counting from 0) as a value of type `T`
2322
* @throws ParseError if argument does not exist or cannot be converted to type `T`.

0 commit comments

Comments
 (0)