remkop / picocli

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.
https://picocli.info
Apache License 2.0
4.93k stars 424 forks source link

[Question] Is it possible to create section headers in help text without using nested fields in the options class? #2315

Closed hq6 closed 4 months ago

hq6 commented 4 months ago

In the help text, I would like to group my options into sections with headers, so it's more clear to users which options affect which aspects of the program.

In the code, I prefer to have a flat class without nesting, because my program is a benchmarking framework and I do not want people writing tests inside my framework to have to understand the nesting structure that is purely useful for people running the program.

The documented way to enable option headers is to create groups, which (as far as I can tell) require defining classes containing the options in a group, and then declaring fields of those classes. This approach induces nesting to access fields inside a section.

Is there some mechanism to decouple these two, and get grouping of options in the help text without creating nesting in the options class?

For example, is there a way I can tag each option with a reference to a string that is intended to be header for that section, such that options tagged with the same header string are put in the same section?

remkop commented 4 months ago

There are no undocumented features like tagging options that would result in the option list being rendered differently. However, the way that the help is rendered can be customized by overriding methods in the picocli CommandLine.Help class.

Please see the examples, hopefully these are enough to get you started:

hq6 commented 4 months ago

Thanks for the pointers! I'll take a look and see if I can get the grouping effect I want.