java-构建jar带哟参数提示的
使用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(, output.length()-);
}
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;
} @Override
public 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> {
@Override
public 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 == -) {
name = value.substring(, index);
configPath = value.substring(index + );
} 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();
}
java-构建jar带哟参数提示的的更多相关文章
- java解决Url带中文参数乱码问题
首先打开Tomcat安装目录,打开conf文件,打开server.xml,找到这段代码: <Connector port="8080" protocol="HTTP ...
- Java反射创建带构造参数的类 并执行方法
部分代码 public void go(ServletRequest request,ServletResponse response){ String methodName = "inde ...
- [改善Java代码]避免带有变长参数的方法重载
建议4: 避免带有变长参数的方法重载 在项目和系统的开发中,为了提高方法的灵活度和可复用性,我们经常要传递不确定数量的参数到方法中,在Java 5之前常用的设计技巧就是把形参定义成Collection ...
- Java程序调用带参数的shell脚本返回值
Java程序调用带参数的shell脚本返回值 首先来看看linux中shell变量(\(#,\)@,$0,$1,\(2)的含义解释 变量说明: - \)$ Shell本身的PID(ProcessI ...
- java运行jar命令提示没有主清单属性和找不到主类
推荐一个java运行jar命令提示没有主清单属性的百度经验的链接:https://jingyan.baidu.com/article/db55b60990f6084ba30a2fb8.html jav ...
- jmeter接口测试-调用java的jar包-csv参数化请求-BeanShellPreProcessor生成验签作为请求验证参数-中文乱码----实战
背景及思路: 需求:要做 创建新卡 接口的测试,要求: 1. 不需要每次手动修改请求参数. 方案:文中先用excle将数据准备好,导出为csv格式,再用jmeter的csv请求进行参数化 2. 卡号需 ...
- Java中List转数组,必须带个参数
public static void main(String[] args) { List<String> lst = new ArrayList(); lst.add("赵云 ...
- java之jar命令详解
1. JAR 文件包 JAR 文件就是 Java Archive File,顾名思意,它的应用是与 Java 息息相关的,是 Java 的一种文档格式.JAR 文件非常类似 ZIP 文件——准确的说, ...
- Gradle 笔记——Java构建入门
Gradle是一个通用的构建工具,通过它的构建脚本你可以构建任何你想要实现的东西,不过前提是你需要先写好构建脚本的代码.而大部分的项目,它们的构建流程基本是一样的,我们不必为每一个工程都编写它的构建代 ...
随机推荐
- linux 查询搜索文件指令
一.which(寻找[执行档]) 二.whereis(由一些特定的目录中寻找文件文件名) 三.locate/updatedb 四.find 个人记录方便自用
- POJ1742--Coins(动态规划)
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony ...
- file新建文件及文件夹
1.获取包名的根目录:mRootPath = getFilesDir().getParent(); // ====mRootPath===/data/data/com.yoyu.file 获取SD卡 ...
- uniGUI 通过SessionList操作另外的登录用户
参照bbs,写了这个方法,检查是否有同名用户已经登录:procedure TUniMainModule.CheckSameUser(aUserLoginCode: string);var ASess ...
- 转:iOS9的新特性以及适配方案
2015年9月8日,苹果宣布iOS 9操作系统的正式版在太平洋时间9月16日正式推出,北京时间9月17日凌晨1点推送. 新的iOS 9系统比iOS8更稳定,功能更全面,而且还更加开放.iOS 9加入了 ...
- SQL SERVER 索引视图
创建: CREATE VIEW [dbo].[view_xxx] WITH SCHEMABINDING AS SELECT Table1.主键, Table1.外键 FROM Table1, Tabl ...
- ASP.Net Core 2.2 MVC入门到基本使用系列 (二)
本教程会对基本的.Net Core 进行一个大概的且不会太深入的讲解, 在您看完本系列之后, 能基本甚至熟练的使用.Net Core进行Web开发, 感受到.Net Core的魅力. 本教程知识点大体 ...
- Google guava cache源码解析1--构建缓存器(2)
此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. CacheBuilder-->maximumSize(long size) /** ...
- [学习笔记]Splay
其实就是一道题占坑啦 [NOI2005]维护数列 分析: 每次操作都要 \(Splay\) 一下 \(Insert\) 操作:重建一棵平衡树,把 \(l\) 变成根,\(l+2\) 变成右子树的根,那 ...
- C#6.0语言规范(十八) 不安全代码
前面章节中定义的核心C#语言与C和C ++的区别在于它省略了作为数据类型的指针.相反,C#提供了引用和创建由垃圾收集器管理的对象的能力.这种设计与其他功能相结合,使C#成为比C或C ++更安全的语言. ...