-
Notifications
You must be signed in to change notification settings - Fork 7
subcommands
h908714124 edited this page Apr 3, 2022
·
18 revisions
The SuperCommand
annotation can be used to create a command/subcommand structure.
The parser generated by this annotation will stop parsing after the last positional parameter was read,
and return the remaining tokens as an array of strings.
This array can then be passed on to another command line parser.
@SuperCommand
abstract class GitCommand {
@Parameter(index = 0)
abstract String command();
}
The generated parser will return something that contains both a GitCommand
and a String[]
of the remaining tokens:
GitCommandParser.GitCommandWithRest result = new GitCommandParser().parseOrExit(args);
GitCommand gitCommand = result.getResult();
String[] rest = result.getRest();
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.
Suppose we also use jbock, then the main method could continue like this:
if ("add".equals(gitCommand.command())) {
GitAddCommand addCommand = new GitAddCommand_Parser().parseOrExit(rest);
// ...
}
where the "subcommand" has a Command
annotation:
@Command(name = "git-add")
abstract class GitAddCommand {
@VarargsParameter
abstract List<String> pathspec();
}