diff options
author | michelou <michelou@epfl.ch> | 2010-12-02 12:15:12 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2010-12-02 12:15:12 +0000 |
commit | a69c1afd4ba602bd4ccc9f9aced9bfc0f6f3c5e7 (patch) | |
tree | 4bb49b6a5ed11a4c721c311f3c6fc1541166c5f5 | |
parent | 2b689f169e9c2112dd243cac186038eeb952eead (diff) | |
download | scala-a69c1afd4ba602bd4ccc9f9aced9bfc0f6f3c5e7.tar.gz scala-a69c1afd4ba602bd4ccc9f9aced9bfc0f6f3c5e7.tar.bz2 scala-a69c1afd4ba602bd4ccc9f9aced9bfc0f6f3c5e7.zip |
added -jar option to scala interpreter
3 files changed, 64 insertions, 15 deletions
diff --git a/src/compiler/scala/tools/nsc/GenericRunnerCommand.scala b/src/compiler/scala/tools/nsc/GenericRunnerCommand.scala index c088bb9303..7f32fb3653 100644 --- a/src/compiler/scala/tools/nsc/GenericRunnerCommand.scala +++ b/src/compiler/scala/tools/nsc/GenericRunnerCommand.scala @@ -33,13 +33,19 @@ extends CompilerCommand(args, settings) { case hd :: tl => (Some(hd), tl) } - override def usageMsg = """ -%s [ <option> ]... [<torun> <arguments>] + override def usageMsg =""" +Usage: %s <options> [<torun> <arguments>] + or %s <options> [-jar <jarfile> <arguments>] All options to %s are allowed. See %s -help. <torun>, if present, is an object or script file to run. -If no <torun> is present, run an interactive shell. + +-jar <jarfile>, if present, uses the 'Main-Class' attribute +in the manifest file to determine the object to run. + +If neither <torun> nor -jar <jarfile> are present, run an +interactive shell. Option -howtorun allows explicitly specifying how to run <torun>: script: it is a script file @@ -57,5 +63,5 @@ for future use. Option -nocompdaemon requests that the fsc offline compiler not be used. Option -Dproperty=value sets a Java system property. -""".format(cmdName, compCmdName, compCmdName) +""".format(cmdName, cmdName, compCmdName, compCmdName) } diff --git a/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala b/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala index 5d272b06ae..65fdb82bd2 100644 --- a/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala +++ b/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala @@ -8,6 +8,13 @@ package scala.tools.nsc class GenericRunnerSettings(error: String => Unit) extends Settings(error) { + val jarfile = + StringSetting( + "-jar", + "jar", + "Specify the jarfile in which to look for the main class", + "") + val howtorun = ChoiceSetting( "-howtorun", @@ -17,24 +24,24 @@ extends Settings(error) { val loadfiles = MultiStringSetting( - "-i", - "file", - "load a file (assumes the code is given interactively)") + "-i", + "file", + "load a file (assumes the code is given interactively)") val execute = StringSetting( - "-e", - "string", - "execute a single command", - "") + "-e", + "string", + "execute a single command", + "") val savecompiled = BooleanSetting( - "-savecompiled", - "save the compiled script (assumes the code is a script)") + "-savecompiled", + "save the compiled script (assumes the code is a script)") val nocompdaemon = BooleanSetting( - "-nocompdaemon", - "do not use the fsc compilation daemon") + "-nocompdaemon", + "do not use the fsc compilation daemon") } diff --git a/src/compiler/scala/tools/nsc/settings/MutableSettings.scala b/src/compiler/scala/tools/nsc/settings/MutableSettings.scala index 9b7b838da4..04af436ea3 100644 --- a/src/compiler/scala/tools/nsc/settings/MutableSettings.scala +++ b/src/compiler/scala/tools/nsc/settings/MutableSettings.scala @@ -122,6 +122,32 @@ class MutableSettings(val errorFn: String => Unit) extends AbsSettings with Scal def parseNormalArg(p: String, args: List[String]): Option[List[String]] = tryToSetIfExists(p, args, (s: Setting) => s.tryToSet _) + def getMainClass(jarName: String): Option[String] = { + import java.io._, java.util.jar._ + try { + val in = new JarInputStream(new FileInputStream(jarName)) + val mf = in.getManifest + val res = if (mf != null) { + val name = mf.getMainAttributes getValue Attributes.Name.MAIN_CLASS + if (name != null) Some(name) + else { + errorFn("Unable to get attribute 'Main-Class' from jarfile "+jarName) + None + } + } else { + errorFn("Unable to find manifest in jarfile "+jarName) + None + } + in.close() + res + } catch { + case e: FileNotFoundException => + errorFn("Unable to access jarfile "+jarName); None + case e: IOException => + errorFn(e.getMessage); None + } + } + def doArgs(args: List[String]): List[String] = { if (args.isEmpty) return Nil val arg :: rest = args @@ -137,6 +163,16 @@ class MutableSettings(val errorFn: String => Unit) extends AbsSettings with Scal errorFn("'-' is not a valid argument.") args } + else if (arg == "-jar") { + parseNormalArg("-cp", rest) match { + case Some(xs) => + getMainClass(rest.head) match { + case Some(mainClass) => mainClass :: xs + case None => args + } + case None => args + } + } else // we dispatch differently based on the appearance of p: // 1) If it has a : it is presumed to be -Xfoo:bar,baz |