@@ -81,6 +81,9 @@ case class Settings(
8181 def withSave : Settings =
8282 this .copy(save = true )
8383
84+ def noSave : Settings =
85+ this .copy(save = false )
86+
8487 def withModeShouldBePossibleRun : Settings =
8588 this .copy(modeShouldBePossibleRun = true )
8689
@@ -144,6 +147,13 @@ object MainGenericRunner {
144147 process(remainingArgs, settings)
145148 case (o @ colorOption(_* )) :: tail =>
146149 process(tail, settings.withScalaArgs(o))
150+ case " -e" :: expression :: tail =>
151+ val tempScript = writeFile(s " @main def main(args: String *): Unit = \n ${expression}" )
152+ settings
153+ .withExecuteMode(ExecuteMode .Script )
154+ .withTargetScript(tempScript)
155+ .withScriptArgs(tail* )
156+ .noSave // useless
147157 case arg :: tail =>
148158 val line = Try (Source .fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
149159 lazy val hasScalaHashbang = { val s = line.getOrElse(" " ) ; s.startsWith(" #!" ) && s.contains(" scala" ) }
@@ -156,6 +166,7 @@ object MainGenericRunner {
156166 val newSettings = if arg.startsWith(" -" ) then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
157167 process(tail, newSettings.withResidualArgs(arg))
158168
169+
159170 def main (args : Array [String ]): Unit =
160171 val scalaOpts = envOrNone(" SCALA_OPTS" ).toArray.flatMap(_.split(" " )).filter(_.nonEmpty)
161172 val allArgs = scalaOpts ++ args
@@ -244,4 +255,11 @@ object MainGenericRunner {
244255 e.foreach(_.printStackTrace())
245256 ! isFailure
246257 }
258+ def writeFile (contents : String ): String = {
259+ val file = Files .createTempFile(" exec" , " .scala" )
260+ file.toFile.deleteOnExit()
261+ Files .write(file, contents.getBytes)
262+ file.toString.replace('\\ ' , '/' )
263+ }
264+
247265}
0 commit comments