https://kotlinlang.org logo
#clikt
Title
# clikt
j

Jim

03/14/2023, 3:06 AM
Is there a way to allow an option to be shared by say 2/3 cases in an OptionGroup?
Copy code
class Pet(kind: String) : OptionGroup(kind) {
  val name by option()

class Cat : Pet("cat") {
  val declawed by option().flag
}

class Dog : Pet("dog") {
  val floppyEars by option().flag()
}
Gives an error like:
Duplicate option name --name
- this is trivial of course, but doesn't allow the full expression of an hierarchical data structure for larger program configs
and it would kinda suck to do something like:
--cat-name
p

Phil Richardson

03/14/2023, 10:24 AM
The problem with what you want is that you don't specify how you would resolve the issue of the duplication the error is indicating. Essentially what your asking for is that both your cat & dog have a single name, given you provide a
--name catName
, which will be consumed by both Pets assigned to your command. That or I suspect your Cat/Dog example may not do your scenario justice to explain that it may / may not be OK that name being the same value would be valid and mandatory (though I can't think of a sane short example to test that theory).
j

Jim

03/14/2023, 3:53 PM
Ah it was a little late I shoulda waited til morning to ask. I think the switch option group example gets me close, I made some edits to my sample and I'll come up with something better today
Copy code
sealed class LoadConfig(name: String): OptionGroup(name) {
    val outName by option()
}
class FromDisk : LoadConfig("Options for loading from disk") {
    val path by option().required()
    val followSymlinks by option().flag()
}

class FromNetwork: LoadConfig("Options for loading from network") {
    val url by option().required()
    val username by option().prompt()
    val password by option().prompt(hideInput = true)
}
Here's a much better and relatable example
Duplicate option name --out-name
The out name is what the file should be saved as and makes sense if it comes from network and from disk, and the
load
option is presumably how the runtime decides which case to use
p

Phil Richardson

03/14/2023, 5:43 PM
Yeah, that's a much more concrete example. File operation with source and target, when source is always of the same type / logic, but target is impl dependant I'm not sure there is a solution to that, though it does imply some problem exists, though more likely in the documented description
Options for the groups other than the selected one are ignored, ...
In this case, correct use of groupChoice/Select don't quite completely ignore the non-selected, as the requirement for unique option names is enforced even against non-selected
15 Views