Quantcast
Channel: User blacktide - Stack Overflow
Viewing all articles
Browse latest Browse all 49

Answer by blacktide for picocli: show help if no parameter given

$
0
0

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.


Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>