-
Notifications
You must be signed in to change notification settings - Fork 7
subcommands
H90 edited this page May 12, 2021
·
18 revisions
The @SuperCommand
annotation can be used to create a command/subcommand structure.
The parser that's created by a @SuperCommand
will stop parsing after the last @Parameter
was read,
and return the remaining tokens as a String[]
array. This array can then be passed to a @Command
or even another @SuperCommand
.
Let's try to do a git-like interface with this.
@SuperCommand(name = "git")
abstract class GitCommand {
@Parameter(index = 0)
abstract String command();
}
The generated parser will return something that returns both a GitCommand
and a String[]
of the remaining tokens:
GitCommand_Parser.GitCommandWithRest result = new GitCommand_Parser().parseOrExit(args);
GitCommand gitCommand = result.getResult();
String[] rest = result.getRest();
The command
can be inspected, in order to decide what to do with the rest
:
if ("add".equals(gitCommand.command())) {
GitAddCommand addCommand = new GitAddCommand_Parser().parseOrExit(rest);
// ...
}
where GitAddCommand
could look like this
@Command(name = "git-add")
abstract class GitAddCommand {
@Parameters
abstract List<String> pathspec();
}