You can also do this with CommandSpec
, as shown in the Git example in the picocli-examples repository. Just add spec.commandLine().usage(System.err)
to the call()
/run()
method of your top-level command.
@Command(name = "cli", subcommands = {ShowUserCommand.class})public class CLI implements Runnable { @Option(names = {"-h", "--help"}, usageHelp = true, description = "Display this usage menu") private boolean help; @Spec CommandSpec spec; @Override public void run() { // print the help menu if no subcommand is passed spec.commandLine().usage(System.err); } public static void main(String[] args) { final int exitCode = new CommandLine(new CLI()).execute(args); System.exit(exitCode); }}@Command(name = "show-user", description = "Show the current user")public class ShowUserCommand implements Runnable { @Option(names = {"-h", "--help"}, usageHelp = true, description = "Display this usage menu") private boolean help; @Option(names = {"-u", "--uppercase"}, description = "Print the username as uppercase") private boolean uppercase; @Spec CommandSpec spec; @Override public void run() { String username = System.getProperty("user.name"); System.out.println("User: " + (uppercase ? username.toUpperCase() : username)); }}
Executing the command without any parameters will print the help menu:

In regards to your followup question, there doesn't seem to be a way to print the full help message (including options for the subcommands), it's required to pass the subcommand with -h
to see the help for that specific subcommand.