Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sbt

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
like image 338
Andrey Avatar asked Dec 15 '13 00:12

Andrey


1 Answers

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.

like image 172
Eugene Yokota Avatar answered Nov 13 '22 21:11

Eugene Yokota