2

Given the following "Main" app:

package net

import com.typesafe.config.ConfigFactory

object Main extends App {
    override def main(args: Array[String]) {
        val x = ConfigFactory.load().getString("app.bar")
        println(s"x: $x")
    }
}

and application.conf:

app {
    bar = ${?BAR}
}

I could pass in the command-line argument via:

$sbt test:run -DBAR=bippy
[info] Set current project to typesafe_config_env_var 
    (in build file:.../typesafe_config_env_var/)
[info] Running net.Main 
x: bippy

I wrote this simple example to try to reproduce a problem resolving a command-line argument. However, as the output above shows, this test worked successfully.

Is this the standard way of passing command-line arguments in sbt?

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • possible duplicate of [SBT: Start a command line 'run' of the main class of a non-default project](http://stackoverflow.com/questions/7674615/sbt-start-a-command-line-run-of-the-main-class-of-a-non-default-project) – Chris Martin Jul 20 '15 at 21:26
  • But I don't know how to set `app.bar` to a value based via a `-Dkey=value` command-line argument. Is it possible with that approach, @ChrisMartin? – Kevin Meredith Jul 20 '15 at 21:28

1 Answers1

2

Depends on what you mean with standard. AFAIK, this is the "standard" way of passing arguments:

$ sbt "run --bippy"

Your example is basically using a feature from the typesafe config that binds environment variables to application.conf definitions. Here's a tutorial for that.

Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
  • Can I use this approach to set `app.bar` per my `application.conf`? If so, can you please show me? – Kevin Meredith Jul 20 '15 at 21:29
  • Kevin, your code is explicitly reading from the `application.conf`. If you want to test for the presence of `--bippy`, then you'll have to peek `args`, like, `if (args.exists(_ == "--blippy")) doStuff`. – Hugo Sereno Ferreira Jul 20 '15 at 21:36
  • But, isn't my above example showing that, although I'm reading from the typesafe config, `app.bar` is being set based on a command-line argument, namely `$sbt test:run -DBAR=bippy` assigning `app.bar` to `bippy`? – Kevin Meredith Jul 20 '15 at 21:39
  • 1
    `-DBAR=bippy` is setting the environment variable `BAR` to `bippy`, nothing else. Then, in `application.conf`, the line `bar = ${?BAR}` sets the `app.bar` to whatever value the environment variable `BAR` is set to. It might seem that you're passing arguments when in fact you're just doing the equivalent to `export BAR=bippy && sbt test:run`. – Hugo Sereno Ferreira Jul 20 '15 at 21:41
  • Can you think of any reason why an environment variable might be ignored from the command line? – Kevin Meredith Jul 20 '15 at 21:50
  • I appreciate the tutorial link and your help. So, if I'm trying to override `http { port = ${?HTTP_PORT} }` within the `sbt test:run` command. I tried `sbt test:run -Dhttp.port=8081`, but I got a `ConfigException$Missing`. – Kevin Meredith Jul 21 '15 at 00:12
  • You should do `sbt test:run -DHTTP_PORT=8081`. Be aware that the environment variable is not `http.port` (this is the configuration parameter path), but the one given by `${?HTTP_PORT}`. If this is helping, I would be most appreciated if you could accept the answer :-) – Hugo Sereno Ferreira Jul 21 '15 at 00:14
  • Thanks, that fixed it for me. Lastly, if I have another value, let's say `http.foo= ${?FOO}`. I tried `sbt test:run -DHTTP_PORT=8081 -DFOO=FOO`, but got a `ConfigException$UnresolvedSubstitution` exception when looking for `FOO`'s value. – Kevin Meredith Jul 21 '15 at 00:19