简体   繁体   中英

Scala.js - pass command line arguments from SBT run

When running the app using the sbt run while developing normal JVM app, I can pass command line arguments using run <args> . When I try the same with Scala.js, I get an error "No valid parser available". When trying runMain variant like runMain Main.main arg , the error is "Expected non-whitespace character", with arrow pointing just behind Main.main.

Is there some way how to pass arguments to the Scala.js / Node.js application when running it from SBT?

(I am using Scala.js 0.6.15).

No, there isn't, because JavaScript does not have a notion of command-line arguments. Node.js does, but only if started from the command-line, and that use case is not supported by the sbt plugin, I'm afraid.

Feel free to file a feature request. I'm not sure it can be accommodated, but we can look into it eventually.

One can define a custom task calling node.js , and parse arguments using SBT parsers. Add this into build.sbt:

import complete.DefaultParsers._

lazy val runa = inputKey[Unit]("Run app with arguments")

runa := {
  (fastOptJS in Compile).value // build it first
  val args: Seq[String] = spaceDelimited("<arg>").parsed
  val npmRun = "node index.js" + args.map("\"" + _ + "\"").mkString(" "," ","")
  npmRun.!
}

You also need to create a file index.js in your project root, containing something like this:

require("./target/scala-2.12/xxxx-jsdeps.js");

require("./target/scala-2.12/xxxx-fastopt.js");

在随后的几年里,出现了一个图书馆来解决这个问题: https ://ben.kirw.in/decline/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM