学习rocketmq的时候,官方实例中有一些 operation
就是将命令行输入的参数,转化为java程序的入参
其实很有意思的,咱们看下option的构造参数
public Option(String opt, String longOpt, boolean hasArg, String description)
throws IllegalArgumentException
{
argName = "arg";
numberOfArgs = -1;
values = new ArrayList();
OptionValidator.validateOption(opt);
this.opt = opt;
this.longOpt = longOpt;
if(hasArg)
numberOfArgs = 1;
this.description = description;
}
public static CommandLine buildCommandline(String[] args) {
final Options options = new Options();
//
Option opt = new Option("h", "help", false, "Print help");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("g", "consumerGroup", true, "Consumer Group Name");
opt.setRequired(true);
options.addOption(opt);
opt = new Option("t", "topic", true, "Topic Name");
opt.setRequired(true);
options.addOption(opt);
opt = new Option("s", "subscription", true, "subscription");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("f", "returnFailedHalf", true, "return failed result, for half message");
opt.setRequired(true);
options.addOption(opt);
//
PosixParser parser = new PosixParser();
HelpFormatter hf = new HelpFormatter();
hf.setWidth(110);
CommandLine commandLine = null;
try {
commandLine = parser.parse(options, args);
if (commandLine.hasOption('h')) {
hf.printHelp("producer", options, true);
return null;
}
} catch (ParseException e) {
hf.printHelp("producer", options, true);
return null;
}
return commandLine;
}