0

I'd like to use Clikt to parse database search arguments from a TextField in a Compose Desktop app, but in the documentation all the examples seem to include the run function like this:

class Filter : CliktCommand() {    
    val firstName: String? by option()
    val timeframe: String? by option().choice("any", "today", "week")
    override fun run() = Unit
}

Is there a way to just parse a string and get the resulting options so that I can use them for other purposes without running the command itself? I'm thinking about something like this (pseudocode):

val filter = CliktEtc.parse(argv).as(Filter::class)
prabu naresh
  • 405
  • 1
  • 10
t3chb0t
  • 16,340
  • 13
  • 78
  • 118

1 Answers1

0

I've figured it out. There's a parse method that populates the properties:

val argv = Commandline.translateCommandline("--first-name bob --timeframe today")
val result = Filter().apply { parse(argv) }

Since I'm using a plain string for it I also need something to parse that string, but unfortunatelly neither Clikt nor picocli support plain strings (only arrays) so I added the org.apache.ant as a dependency. That's where the Commandline from this answer comes from.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118