clikt icon indicating copy to clipboard operation
clikt copied to clipboard

OptionGroup options `protected` instead of `internal`

Open untra opened this issue 2 years ago • 1 comments

Hi there! Big fan of clikt, this project is awesome. We have made a practice of extending certain clikt classes including CliktCommand to get additional or supportive functionality we've desired.

One capability we desire is to extend OptionGroup so our CLI can debug log the assigned values passed to all options. Our CLI has a DEBUG flag, and an increasing number of options. It would help greatly with debugging and maintainability to report all assigned settings passed into the OptionGroup.

https://github.com/ajalt/clikt/blob/master/clikt/src/commonMain/kotlin/com/github/ajalt/clikt/parameters/groups/ParameterGroup.kt#L76

We don't have a slick way of doing this currently, because access to OptionGroup.options is marked as internal. Any reason this field cannot be made protected instead, so advanced users can access all options in a group dynamically? This is a small change, but it would be a huge help.

untra avatar Aug 10 '22 19:08 untra

You can get the info you're looking for by getting all a command's options, then getting the groupName from each option's parameterHelp. If you need that actual OptionGroup instance, you can get it from the parameterGroup for any of the options that are GroupableOption. There are other types of groups that don't have an options property, so I'd rather not expose that property.

ajalt avatar Aug 15 '22 04:08 ajalt

You can get the info you're looking for by getting all a command's options, then getting the groupName from each option's parameterHelp.

Bonus question: Is there a way to filter the resulting options to file options only (and their actual Filevalues)? When trying to use as NullableOption<File, File> I'm getting Cannot check for instance of erased type.

sschuberth avatar Dec 06 '22 09:12 sschuberth

Unfortunetely, since types are erased on the JVM, there's no way to filter on a specific delegate type. But you could filter for options with completionCandidates of type Path. It will require an unsafe cast though, and you'll need to know if your options use File or Path.

Alternately, you could cast them to OptionDelegate<*> and, for the ones where the cast succeeds, check the type of their value (o as? OptionDelegate<*>)?.value as? File. That's probably the way I would do it.

ajalt avatar Dec 06 '22 17:12 ajalt