编写命令行工具

人盡茶涼 提交于 2020-04-04 03:25:26

1、使用common-cli编写命令行工具

commons-cli是Apache开源组织提供的用于解析命令行参数的包。

先引用common-cli依赖包:

<groupId>commons-cli</groupId><artifactId>commons-cli</artifactId><version>1.2</version>

命令定义:

private static final Options OPTIONS = new Options();

public void defineCommand() {
     OPTIONS.addOption("i", true, "the input directory where the proto files are");
     OPTIONS.addOption("o", true, "the output directory which is the output path");
     OPTIONS.addOption("c",true,"whether we use config.json or not");

     OPTIONS.addOption("groupId",true,"maven project related parameter");
     OPTIONS.addOption("artifactId",true,"maven project related parameter");
     OPTIONS.addOption("artifactVersion",true,"maven project related parameter");

     OPTIONS.addOption("h", true ,"get command input help");
     OPTIONS.addOption("help", true ,"get command input help");
}

命令解析:

CommandLine cli = parser.parse(OPTIONS, args);
if (cli.hasOption("c")) {
     //TODO:  
}

执行命令:

java -jar common-cli-demo-1.0-SNAPSHOT.jar -c c:\cli\demo1\config.json

config.json:

{
  "groupId": "common.cli.demo",
  "artifactId": "cli-demo",
  "artifactVersion": "demo-snapshot",
  "outputDirectory": "c"
}

config.json文件名可以随意,如果不指定就读取默认的配置。

2、使用mustache模版 

mustache参考地址:Github地址

模板文件generator.mustache:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>

    <entry key="base_package">{{base_package}}</entry>
    <!--配置实体包路径-->
    <entry key="base_entity_package">{{base_entity_package}}</entry>
    <!--配置mybatis Mapper包路径-->
    <entry key="base_mapper_package">{{base_mapper_package}}</entry>

</properties>

默认模板参数配置xmltemplate.json:

{
  "base_package": "com.demo.package",
  "base_entity_package": "com.demo.package.entity",
  "base_mapper_package": "com.demo.package.mapper"
}

自定义模板的参数配置xmltemplate.json:

{
  "base_package": "com.test.demo.package",
  "base_entity_package": "com.test.demo.package.entity.testDb",
  "base_mapper_package": "com.test.demo.package.mapper.testDb"
}

指定配置文件

执行命令:

java -jar common-cli-demo-1.1-SNAPSHOT.jar -c c:\cli\demo2\template.json -o c:\cli\demo2\

 填充模板生成的xml文件为:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>

    <entry key="base_package">com.test.demo.package</entry>
    <!--配置实体包路径-->
    <entry key="base_entity_package">com.test.demo.package.entity.testDb</entry>
    <!--配置mybatis Mapper包路径-->
    <entry key="base_mapper_package">com.test.demo.package.mapper.testDb</entry>

</properties>

不指定配置文件(使用默认的配置)

执行命令:

java -jar common-cli-demo-1.1-SNAPSHOT.jar -o c:\cli\demo2\

 填充模板生成的xml文件为:

<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>

    <entry key="base_package">com.demo.package</entry>
    <!--配置实体包路径-->
    <entry key="base_entity_package">com.demo.package.entity</entry>
    <!--配置mybatis Mapper包路径-->
    <entry key="base_mapper_package">com.demo.package.mapper</entry>

</properties>

示例:common-cli-demo 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!