使用command的cli包构建带有参数提示的jar包
需要引入command cli的依赖
<commons.version>1.2</commons.version>
<dependency><groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>${commons.version}</version></dependency>
package com.wenbronk.storm.test.optional;import org.apache.commons.cli.*; import org.apache.storm.shade.org.apache.commons.lang.StringUtils;import java.util.function.IntPredicate;public class OptionTest {/*** Options使用* @return*/private final static Options buildOptions() {Options options = new Options();options.addOption("intype", true, "[options] txt(default)");options.addOption("inn", true, "[required] input original data");options.addOption("out", true, "[required] output directory");return options;}private final static void printUsage(Options options) {HelpFormatter helpFormatter = new HelpFormatter();helpFormatter.printHelp("OptionsUsage", options);}public static void main(String[] args) throws ParseException {Options options = buildOptions();BasicParser basicParser = new BasicParser();CommandLine commandLine = basicParser.parse(options, args);if (!commandLine.hasOption("intype") && !commandLine.hasOption("out")) {printUsage(options);return;}String in = commandLine.getOptionValue("inn");if (StringUtils.isNotEmpty(in) && !in.endsWith("txt")) {printUsage(options);return;}System.out.println("in " + in);// 默认txt格式String intype = commandLine.getOptionValue("intype", "txt");System.out.println("intype " + intype);String output = commandLine.getOptionValue("out");if(output.endsWith("/")){output = output.substring(0, output.length()-1);}System.out.println("output " + output);}}
或者使用commandline
package com.babytree.ask.conf;import com.beust.jcommander.Parameter;import java.util.List;public class CmdConfig {@Parameter(names = {"-h", "--help"}, help = true)private boolean help;@Parameter(names = "--dev", description = "enable dev mode")private boolean dev;@Parameter(names = {"-c", "--config"}, description = "config file path")private String configFile;@Parameter(names = "-m", description = "启动的模块", converter = ModuleInfoConverter.class)private List<ModuleInfo> moduleInfos;public boolean isHelp() {return help;}public CmdConfig setHelp(boolean help) {this.help = help;return this;}public boolean isDev() {return dev;}public CmdConfig setDev(boolean dev) {this.dev = dev;return this;}public String getConfigFile() {return configFile;}public CmdConfig setConfigFile(String configFile) {this.configFile = configFile;return this;}@Overridepublic String toString() {return "CmdConfig{" +", help=" + help +", dev=" + dev +", configFile='" + configFile + '\'' +'}';} }
package com.babytree.ask.conf;import com.beust.jcommander.IStringConverter; import org.apache.commons.lang.StringUtils;public class ModuleInfoConverter implements IStringConverter<ModuleInfo> {@Overridepublic ModuleInfo convert(String value) {if (StringUtils.isBlank(value)) {throw new IllegalArgumentException("module info is empty");}String name = value;String configPath = null;int index = value.indexOf(':');if (index == -1) {name = value.substring(0, index);configPath = value.substring(index + 1);}ModuleInfo info = new ModuleInfo();info.setConfigPath(configPath);info.setName(name);return info;} }
CommandInfo main = new CommandInfo();JCommander jCommander = JCommander.newBuilder().addObject(main).build();jCommander.parse(args);if (main.isHelp()) {jCommander.usage();System.exit(0);}