背景
项目
需要做一个小工具,不想依赖太多的东西就没有创建SpringBoot项目,但是启动的时候又需要传入一些参数,于是就借助于commons-cli来读取参数
commons-cli
这个工具有两个版本,根据官网说cli有很多bug,而cli2又在开发阶段(不知道还在开发不)。但是我的功能不是很复杂,就仍然基于cli来开发。
代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.onlyedu</groupId>
<artifactId>response-comparator</artifactId>
<version>1.0-SNAPSHOT</version>
<name>response-comparator</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Configuration.java
/**
* @author qbit
*/
public class Configuration {
public static final String CONTROL_SERVICE = "controlService";
public static final String EXPERIMENTAL_SERVICE = "experimentalService";
public static final String POLICY = "policy";
private Configuration(){}
static Configuration fromCLI(String[] args){
CommandLineParser parser=new DefaultParser();
Options options=new Options()
.addOption(CONTROL_SERVICE,true,"作为参照的服务")
.addOption(EXPERIMENTAL_SERVICE,true,"待验证的服务")
.addOption(POLICY,true,"策略")
;
CommandLine commandLine=null;
try{
commandLine=parser.parse(options,args);
}catch (ParseException e){
System.err.println("Parsing failed.Reason: "+e.getMessage());
System.exit(-1);
}
for(String requiredArg:new String[]{
CONTROL_SERVICE,EXPERIMENTAL_SERVICE}){
if(!commandLine.hasOption(requiredArg)){
System.err.println(requiredArg+" is required!");
System.exit(-1);
}
}
System.out.println("configuration is validated");
return new Configuration();
}
public static void main(String[] args) {
fromCLI(args);
}
}
测试
直接运行不传入任何参数会打印下面一句提醒
controlService is required!