Is there a way to get a value source file as an op...
# clikt
m
Is there a way to get a value source file as an option rather than hardcoding a path? Id like users to be able to give a path to a config file when calling a command then use that config file (if provided) as a value source for the other options. I thought setting the config option to eager would make it available in init where i could set the value as a value source but that didnt work for me, i also tried setting the value source on the context of the parent command but im guessing thats too late in execution since its not pulling values from the config Here's my first attempt i thought would work because of eager value
Copy code
class Pickle : SuspendingCliktCommand() {
  val config by option(eager = true)
    .file()

  init {
    context {
      config?.let {
        valueSources(PropertiesValueSource.from(it))
      }
    }
  }
  val test by option()

  override suspend fun run() {
    echo(test)
  }
}
I found a work around using transformAll, replacing default. Its a little clunky looking but i can pull it into a helper function i suppose.
.defaultLazy
seems to almost do what i need with a slightly better api than transformAll but it doesnt allow returning null as a default which ill need in some cases. Basically what i want is for the option to get its value in priority order of: command arg -> config file -> some static default (maybe null).
Copy code
val config by option()
    .file()

  val test by option(valueSourceKey = "test")
    .transformAll {
      it.lastOrNull() ?: config?.let {
        PropertiesValueSource.from(it).getValues(this.context, this.option).first().values.first()
      }
      ?: "default"
    }
a
Yeah, that's basically how I would do it.
thank you color 1