commons-cli 一点使用

背景: 因项目调用FMIS 接口API 获取数据,需要获取多个接口进行拉取数据。

1  总体上设计思想是: 

       部署到大数据平台JAR包,需要调用,有多少个表就调用多少次。

      Main方法: 入参使用  commons-cli  优雅传参方式。

 1.1 POM 包引用:

<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>

 1.2  Main 方法主要代码

private static Condition assembleOptions(String[] args) throws Exception {

Condition condition = new Condition();
// 构建参数
final Options options = new Options();
options.addOption(new Option("h", "help", false, "帮助"));
options.addOption(new Option("d", "date", true, "date(非必填)"));
options.addOption(new Option("a", "actionName", true, "action名称(必填)"));
options.addOption(new Option("k", "k", true, "扩展编号(非必填)"));

HelpFormatter hf = new HelpFormatter();
hf.setWidth(110);
CommandLine commandLine = null;
CommandLineParser parser = new PosixParser();
try {
commandLine = parser.parse(options, args);
if (commandLine.hasOption('h')) {
hf.printHelp("FmisApp", options, true);
}
if (commandLine.hasOption('d')) {
condition.setDate(commandLine.getOptionValue('d'));
}

if (commandLine.hasOption('a')) {
condition.setActionName(commandLine.getOptionValue('a'));
String t = ParamEnum.getParamkey(condition.getActionName(), "t");
if (StringUtils.isNotBlank(t)) {
condition.getParams().put(t, ActionEnum.getTableNameValue(condition.getActionName()));
}
}

// 添加参数
if (commandLine.hasOption('k')) {
String k = ParamEnum.getParamkey(condition.getActionName(), "k");
if (StringUtils.isNotBlank(k)) {
condition.getParams().put(k, commandLine.getOptionValue('k'));
}
}

} catch (Exception e) {
hf.printHelp("FmisApp", options, true);
logger.error(" 执行 [FmisApp 方法 assembleOptions] : args=[{}] : Exception=[{}]", FastJsonUtil.beanToJson(args), e.getMessage());
throw new Exception("执行 [FmisApp 方法 assembleOptions] 转换 args 出错,响应代码为:" + e.getMessage());
}
return condition;
}

   1.3   使用

 

1.3.1 往来单位 参数:         -h -a LSWLDW

1.3.2 要素类别 参数:         -h -a LSYSLB

1.3.3 要素台帐 参数:         -h -a LSYSZD  -k 52

1.3.4 费用要素字典 参数:  -h -a ZYCBZD

   

     

posted @   163博客  阅读(170)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示