Top Description Fields Constructors Methods
org.kohsuke.args4j.spi

public Class SubCommandHandler

extends OptionHandler<Object>
Class Inheritance
Imports
org.kohsuke.args4j.Argument, .CmdLineException, .CmdLineParser, .Option, .OptionDef, java.util.AbstractList, .ResourceBundle

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 (which is 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={@link SubCommandHandler}.class)
     @SubCommands({
         @SubCommand(name="checkout", impl=CheckoutCommand.class),
         @SubCommand(name="commit", impl=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.

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.

Author
Kohsuke Kawaguchi

Field Summary

Modifier and TypeField and Description
private final SubCommands
Inherited from org.kohsuke.args4j.spi.OptionHandler:
optionownersetter

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected CmdLineParser
protected int
fallback(String subCmd)

public String
getDefaultMetaVariable()

Implements abstract org.kohsuke.args4j.spi.OptionHandler.getDefaultMetaVariable.

Gets the default meta variable name used to print the usage screen.

public String
protected Object
public int
parseArguments(Parameters
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.
params
)

Implements abstract org.kohsuke.args4j.spi.OptionHandler.parseArguments.

Called if the option that this owner recognizes is found.

protected Object
subCommand(SubCommand c, final Parameters params)

Inherited from org.kohsuke.args4j.spi.OptionHandler:
getNameAndMetagetNameAndMetaprintprintDefaultValue

Field Detail

commandsback to summary
private final SubCommands commands

Constructor Detail

SubCommandHandlerback to summary
public SubCommandHandler(CmdLineParser parser, OptionDef option, Setter<Object> setter)

Method Detail

configureParserback to summary
protected CmdLineParser configureParser(Object subCmd, SubCommand c)
fallbackback to summary
protected int fallback(String subCmd) throws CmdLineException
getDefaultMetaVariableback to summary
public String getDefaultMetaVariable()

Implements abstract org.kohsuke.args4j.spi.OptionHandler.getDefaultMetaVariable.

Doc from org.kohsuke.args4j.spi.OptionHandler.getDefaultMetaVariable.

Gets the default meta variable name used to print the usage screen. The value returned by this method can be a reference in the ResourceBundle, if one was passed to CmdLineParser.

Returns:String

null to hide a meta variable.

Annotations
@Override
getMetaVariableback to summary
public String getMetaVariable(ResourceBundle rb)

Overrides org.kohsuke.args4j.spi.OptionHandler.getMetaVariable.

Annotations
@Override
instantiateback to summary
protected Object instantiate(SubCommand c)
parseArgumentsback to summary
public int parseArguments(Parameters params) throws CmdLineException

Implements abstract org.kohsuke.args4j.spi.OptionHandler.parseArguments.

Doc from org.kohsuke.args4j.spi.OptionHandler.parseArguments.

Called if the option that this owner recognizes is found.

Parameters
params:Parameters

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.

Returns:int

The number of arguments consumed. (For example, returns 0 if this option doesn't take any parameters.)

Annotations
@Override
subCommandback to summary
protected Object subCommand(SubCommand c, final Parameters params) throws CmdLineException