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

前端 未结 1 1959
傲寒
傲寒 2021-01-21 01:17

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:



        
相关标签:
1条回答
  • 2021-01-21 01:38

    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)
    
    0 讨论(0)
提交回复
热议问题