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(
        name = "git",
        description = "Git is software for tracking changes in any set of files.")
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. We can define a "subcommand" to parse it, which is just a normal @Command.

@Command(
        name = "git-add",
        description = "Add file contents to the index")
interface AddCommand {
    @VarargsParameter
    List<String> pathspec();

    // more parameters and options...
}
Clone this wiki locally