Skip to content

subcommands

h908714124 edited this page Apr 16, 2023 · 18 revisions

The SuperCommand annotation can be used to create a command/subcommand structure. A @SuperCommand parser is similar to a @Command parser, but it stops parsing after the last positional parameter was read. The remaining tokens can then be passed on to another command line parser.

@SuperCommand
interface GitCommand {

  @Parameter(index = 0)
  String command();
}

The generated parser will return something that contains both a GitCommand and a String[] of the remaining tokens:

SuperResult<GitCommand> result = new GitCommandParser().parseOrExit(new String[]{"add", "foo"});
GitCommand gitCommand = result.getCommand(); // "add"
String[] rest = result.getRest();            // ["foo"]

The rest is a String array again. It is the input of the subcommand. We are free to choose any command line parser to deal with it.

Clone this wiki locally