EECOLOR
EECOLOR

Reputation: 11244

Setting value of setting on command line when no default value defined in build?

I am writing a plugin that requires a specific setting: configUrl

If I specify that setting in my build.conf it would look like this:

MyPlugin.configUrl := "http://..../..."

I can then use the command line to do this:

sbt 'set MyPlugin.configUrl := "http://..../..."' performAction

Is there a better way that allows me to not have that setting in build?

If I start sbt without that setting I get the following error:

[error] Reference to undefined setting: 
[error] 
[error]   *:config-url from *:application-configuration

I searched google but could not find a way to provide the setting on the command line, something like this:

sbt config-url="http://..../..."

Upvotes: 3

Views: 3873

Answers (1)

Mark Harrah
Mark Harrah

Reputation: 7019

If the setting has a default value, that default should be set in the plugin. If it does not, the type should probably be Option[...] with a default of None. Typically, a build should not require a parameter to be passed from the command line just to be loaded.

Finally, if it is primarily the set syntax you dislike, you can use system properties and read the system property from your setting. However, this is restricted to Strings and so you lose type safety.

System properties can be set by -Dkey=value on the command line (either directly or in your startup script):

sbt -Dconfig.url=http://...

(quoting as needed by your shell). To pull this value into the setting:

MyPlugin.configUrl := 
  url(System.getProperty("config.url", "<default>"))

where "<default>" is the value to use if the system property is not set. If you take the Option approach,

MyPlugin.configUrl := 
  for(u <- Option(System.getProperty("config.url"))) yield
    url(u)

Upvotes: 9

Related Questions