使用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); } }
原文地址:https://www.cnblogs.com/wenbronk/p/8440037.html
时间: 2024-11-13 06:49:06