How do I specify default subcommand?

1.2k views Asked by At

Is there a way to print help message when user didn't provide subcommand ?

Following doesn't work because it runs callable that doesn't have implementation.

@Command(
        name = "tool",
        mixinStandardHelpOptions = true,
        subcommands = [ListPlugins::class, RunJob::class, CommandLine.HelpCommand::class])
class Main : Callable<Int> {

    override fun call(): Int {
//        CommandLine.HelpCommand().run()
        return 0
    }
}
2

There are 2 answers

1
Remko Popma On BEST ANSWER

To rephrase the question: How to show help when the user didn’t specify a subcommand?

From picoli version 4.3, you can simply not implement Callable on the top-level command; this makes it mandatory for users to specify a subcommand. (This assumes that you use CommandLine.execute to parse the command line and run the business logic.)

If no subcommand is specified, a "Missing required subcommand" error message is displayed, followed by the usage help message.

See: https://picocli.info/#_required_subcommands for details.

Does that meet your requirements?

0
Sergey Zh. On

Yes, for example, you can make a fake call without command line parameters. (instead of args-> --help) And after it with the parameters, according to the instructions. For example, the code in Java:

CommandLine helpCL = new CommandLine(new Main());
helpCL.execute(new String[]{"--help"});
return new CommandLine(new Main()).execute(args);