子命令
此示例显示如何为给定命令定义子命令,以及如何轻松关联命令处理程序。此示例使用 get
和 set
子命令定义 thing
命令。
package things;
import io.dropwizard.cli.Command;
import io.dropwizard.setup.Bootstrap;
import net.sourceforge.argparse4j.inf.FeatureControl;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
public class ThingCommand extends Command {
private static final String THING_COMMAND_NAME = "thing";
private static final String THING_SUBCOMMAND_GET = "get";
private static final String THING_SUBCOMMAND_SET = "set";
public ThingCommand() {
super(THING_COMMAND_NAME, "Thing management.");
}
@Override
public void configure(Subparser subparser) {
addSubCommand(subparser.addSubparsers().addParser(THING_SUBCOMMAND_GET)
.help("Gets a thing."), new GetCommand());
addSubCommand(subparser.addSubparsers().addParser(THING_SUBCOMMAND_SET)
.help("Sets a thing."), new SetCommand());
}
private void addSubCommand(Subparser parser, Command cmd) {
// associate the subcommand with a Command object
parser.addArgument("--subcommand").help(FeatureControl.SUPPRESS).setDefault(cmd);
parser.description(cmd.getDescription());
cmd.configure(parser);
}
@Override
public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception {
Command cmd = namespace.get("subcommand");
assert cmd != null;
cmd.run(bootstrap, namespace);
}
class GetCommand extends Command {
public GetCommand() {
super(THING_SUBCOMMAND_GET, "Gets a thing.");
}
@Override
public void configure(Subparser cmd) {
super.configure(cmd);
cmd.addArgument("--name").type(String.class).required(true).help("Name of the thing");
}
@Override
public void run(Bootstrap<?> bootstrap, Namespace namespace) throws Exception {
String thingName = namespace.getString("name");
System.out.println("Getting the thing named: " + thingName);
}
}
class SetCommand extends Command {
// ...
}
}
这是一个命令的示例调用(假设一个名为 myapp
的应用程序):
$ bin/myapp thing get --name mything
Getting the thing named: mything