-
Notifications
You must be signed in to change notification settings - Fork 7
subcommands
h908714124 edited this page Jun 11, 2021
·
18 revisions
The superCommand
attribute can be used to create a command/subcommand structure.
When superCommand
is true
, the parser will stop parsing after the last positional parameter was read,
and return the remaining tokens as an array of strings.
This array can be passed to another command parser.
Let's try to create a git-like interface with this.
@Command(name = "git", superCommand = true)
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 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();
}