commons-cli使用筆記

背景

項目

需要做一個小工具,不想依賴太多的東西就沒有創建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!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章