Hi friends! Apologies for the newbie question. :sw...
# clikt
p
Hi friends! Apologies for the newbie question. 😅 The following code works, but I've been wondering if this would be the correct way... I'd like
input
to be influenced by a flag, so
Copy code
private val caps by option("--caps").flag()
private val input by argument(name = "input").multiple(required = true).transformAll {
  if (caps) it.map { i -> i.uppercase() } else it
}
I may be mistaken, but I vaguely remember seeing this sort of cross-dependency mentioned somewhere (was it in the documentation?). Any suggestions are welcome! Thanks! Update: check 🧵 .
I knew I saw it mentioned! 😁 https://ajalt.github.io/clikt/parameters/#validate
The lambdas you pass to
validate
are called after the values for all options and arguments have been set, so (unlike in transforms) you can reference other parameters
(emphasis mine) So I gather my code works by accident, and this practice should be avoided? Thanks!
More findings! If I use
Copy code
private val caps by option("--caps").flag()
private val input by argument(name = "input").convert {
  if (caps) it.uppercase() else it
}.multiple(required = true)
I get
Copy code
Cannot read from option delegate before parsing command line
which was what I was expecting to get. 🙂 Then I found out about
eager
, so I could change
caps
to
Copy code
private val caps by option("--caps", eager = true).flag()
and then the code works! 🎉 Is this the correct way then? Thanks!