public class SubCommandHandler extends OptionHandler<java.lang.Object>
OptionHandler used with Argument for parsing typical "sub-command" pattern.
The "sub-command" pattern refers to the design of the command line like git and svn, where the first argument to the command designates a sub-command (say "git checkout"), then everything that follows afterward are parsed by this sub-command (and are usually different depending on which sub-command was selected.)
This OptionHandler models this design pattern with the SubCommands annotation.
See the following example:
class Git {
@Argument(handler=SubCommandHandler.class)
@SubCommands({
@SubCommand(name="checkout",CheckoutCommand.class),
@SubCommand(name="commit",CommitCommand.class),
...
})
Command cmd;
@Option(name="-r")
boolean recursive;
public static void main(String[] args) {
Git git = new Git();
new CmdLineParser(git).parseArgument(args);
git.cmd.execute();
}
}
class CheckoutCommand {
@Option(name="-a")
boolean all;
...
}
An example of legal command line option for this is "-r checkout -a".
SubCommand only works with Argument and not with Option.
SubCommands that specify possible sub-commands.
Options that you define in the Git class above can parse options that appear
prior to the sub-command name. This is useful for defining global options that work across sub-commands.
CmdLineParser will be created to parse its annotations.
CmdLineParser
This class defines a number of protected methods that allow subtypes to customize various parts of the behaviours. This should also serve as an example if you want to combine this with more sophisticated sub-command lookup, such as through META-INF/services, sezpoz, or annotation indexer.
option, owner, setter| Constructor and Description |
|---|
SubCommandHandler(CmdLineParser parser,
OptionDef option,
Setter<java.lang.Object> setter) |
| Modifier and Type | Method and Description |
|---|---|
protected CmdLineParser |
configureParser(java.lang.Object subCmd,
SubCommand c) |
protected int |
fallback(java.lang.String subCmd) |
java.lang.String |
getDefaultMetaVariable()
Gets the default meta variable name used to print the usage screen.
|
protected java.lang.Object |
instantiate(SubCommand c) |
int |
parseArguments(Parameters params)
Called if the option that this owner recognizes is found.
|
protected java.lang.Object |
subCommand(SubCommand c,
Parameters params) |
getMetaVariable, getNameAndMetapublic SubCommandHandler(CmdLineParser parser, OptionDef option, Setter<java.lang.Object> setter)
public int parseArguments(Parameters params) throws CmdLineException
OptionHandlerparseArguments in class OptionHandler<java.lang.Object>params - The rest of the arguments. This method can use this
object to access the arguments of the option if necessary.
The object is valid only during the method call.CmdLineExceptionprotected int fallback(java.lang.String subCmd)
throws CmdLineException
CmdLineExceptionprotected java.lang.Object subCommand(SubCommand c, Parameters params) throws CmdLineException
CmdLineExceptionprotected CmdLineParser configureParser(java.lang.Object subCmd, SubCommand c)
protected java.lang.Object instantiate(SubCommand c)
public java.lang.String getDefaultMetaVariable()
OptionHandlergetDefaultMetaVariable in class OptionHandler<java.lang.Object>Copyright © 2003-2013 Kohsuke Kawaguchi. All Rights Reserved.