How to pass command line args to program in SBT 0.13.1?

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

I just switched to SBT 0.13.1 and either arg handling changed or there's a bug. Here's what I'm testing with (named sbt-test.scala):

#!/bin/sh SBT_0_13_0="/path/to/sbt-launch-0.13.0.jar" SBT_0_13_1="/path/to/sbt-launch-0.13.1.jar" SBT="$SBT_0_13_1" exec java $JAVA_OPTS -Dsbt.main.class=sbt.ScriptMain -jar "$SBT" $0 "$@" !#  /*** scalaVersion := "2.10.3"  libraryDependencies ++= Seq(   "com.typesafe" % "config" % "1.0.0" ) */  import com.typesafe.config.{ConfigFactory, Config}  println(s"Args: ${args mkString ", "}")  val cfg = ConfigFactory.parseString(   """     |credentials {     |  user = someone     |  pass = s3cr3t     |}   """.stripMargin)  println(cfg getString "credentials.user")

When I run ./sbt-test.scala --arg=val I get the following error:

[error] Expected ID character [error] Not a valid command: arg [error] arg=val

Running the same script with the same argument with SBT 0.13.0 yields the expected:

Args: --arg=val someone

Similarly, if I turn that executable script into an actual SBT project and attempt to run sbt run --flag I get

[error] Not a valid command: flag (similar: iflast, last, alias) [error] flag

回答1:

See the documentation on Batch mode:

Batch mode

You can also run sbt in batch mode, specifying a space-separated list of sbt commands as arguments. For sbt commands that take arguments, pass the command and arguments as one argument to sbt by enclosing them in quotes. For example,

$ sbt clean compile "testOnly TestA TestB"

In this example, testOnly has arguments, TestA and TestB. The commands will be run in sequence (clean, compile, then testOnly).

So if you don't want --flag to be interpreted as a command, you have to quote it with run:

$ sbt "run --flag"

sbt script runner

The scripting is described in Scripts, REPL, and Dependencies, which seems to say you don't need extra quotes other than the bash quotes:

java -Dsbt.main.class=sbt.ScriptMain -Dsbt.boot.directory=/home/user/.sbt/boot -jar sbt-launch.jar "$@"

In reality, to make your script work, you'd need another sets of escaped quotes around $@ like this:

exec java $JAVA_OPTS -Dsbt.main.class=sbt.ScriptMain -jar "$SBT" $0 "\"$@\""

If this is a regression that started in 0.13.1, you should report to sbt.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!