4

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
    }
}
Progman
  • 16,827
  • 6
  • 33
  • 48
expert
  • 29,290
  • 30
  • 110
  • 214

2 Answers2

3

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?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0

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);
Sergey Zh.
  • 347
  • 2
  • 3
  • 13