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.94k stars 424 forks source link

Showing --help for commands should have a way of automatic include default values #2351

Closed davsclaus closed 1 week ago

davsclaus commented 2 weeks ago

We use picocli at the Apache Camel project for our CLI. We have about 50 commands, each with a mixed set of options. https://camel.apache.org/manual/camel-jbang.html

The --help output shows the options and their description. However what we struggle with is that the default values is not possible to see / know. To include default values you would have to update every description to include it via a special literal text (default: ${DEFAULT-VALUE}).

Before

$ camel get bean --help
Usage: camel get bean [-h] [--dsl] [--internal] [--nulls] [--properties]
                      [--filter=<filter>] [--sort=<sort>] [<name>]
List beans in a running Camel integration
      [<name>]            Name or pid of running Camel integration
  -h, --help              Display the help and sub-commands
      --sort=<sort>       Sort by name or type
      --filter=<filter>   Filter beans names (use all to include all beans)
      --properties        Show bean properties
      --nulls             Include null values
      --internal          Include internal Camel beans
      --dsl               Include only beans from YAML or XML DSL

After (with default values) where I have manually updated all the descriptions for evert option in this command.

$ camel get bean --help
Usage: camel get bean [-h] [--dsl] [--internal] [--nulls] [--properties]
                      [--filter=<filter>] [--sort=<sort>] [<name>]
List beans in a running Camel integration
      [<name>]            Name or pid of running Camel integration
  -h, --help              Display the help and sub-commands
      --sort=<sort>       Sort by name or type (default: name)
      --filter=<filter>   Filter beans names; use all to include all beans
                            (default: all)
      --properties        Show bean properties (default: true)
      --nulls             Include null values (default: true)
      --internal          Include internal Camel beans (default: false)
      --dsl               Include only beans from YAML or XML DSL (default:
                            false)

Now the default values are shown in the description.

1) I wonder if there can be added some way to automatic include this via some new option in the @Command

@Command(name = "bean", description = "List beans in a running Camel integration", sortOptions = false)

Or some other way to enable this globally for all commands.

2) I would also like to see the dumped --help to show the default values more prominently in a separate column instead of in the description. This makes it much quicker to see the defaults instead of scanning all the description texts.

davsclaus commented 2 weeks ago

Example how to manually add default values

     @CommandLine.Option(names = { "--sort" }, completionCandidates = NameTypeCompletionCandidates.class,
-                        description = "Sort by name or type", defaultValue = "name")
+                        description = "Sort by name or type (default: ${DEFAULT-VALUE})", defaultValue = "name")
     String sort;

     @CommandLine.Option(names = { "--filter" },
-                        description = "Filter beans names (use all to include all beans)", defaultValue = "all")
+                        description = "Filter beans names; use all to include all beans (default: ${DEFAULT-VALUE})", defaultValue = "all")
     String filter;

     @CommandLine.Option(names = { "--properties" },
-                        description = "Show bean properties", defaultValue = "true")
+                        description = "Show bean properties (default: ${DEFAULT-VALUE})", defaultValue = "true")
     boolean properties;

     @CommandLine.Option(names = { "--nulls" },
-                        description = "Include null values", defaultValue = "true")
+                        description = "Include null values (default: ${DEFAULT-VALUE})", defaultValue = "true")
     boolean nulls;

     @CommandLine.Option(names = { "--internal" },
-                        description = "Include internal Camel beans", defaultValue = "false")
+                        description = "Include internal Camel beans (default: ${DEFAULT-VALUE})", defaultValue = "false")
     boolean internal;

     @CommandLine.Option(names = { "--dsl" },
-                        description = "Include only beans from YAML or XML DSL", defaultValue = "false")
+                        description = "Include only beans from YAML or XML DSL (default: ${DEFAULT-VALUE})", defaultValue = "false")
     boolean dsl;
remkop commented 1 week ago

Hi @davsclaus picocli has that feature, see https://picocli.info/#_legacy_configuration_for_displaying_default_values

Use @Command(showDefaultValues = true) o append the default value of all non-null options and positional parameters to the description column.

To change the layout of the help, and add an extra column, please see the picocli-examples subproject on github.

davsclaus commented 1 week ago

@remkop thank you very much for the help. This is what I needed.