Java: Parsing command line arguments with JCommander

JCommander is a very small Java framework that makes it trivial to parse command line parameters.

Translation: JCommander is a very small Java framework that makes parsing command line arguments trivial.

Relevant information

rely

<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
<dependency>
    <groupId>com.beust</groupId>
    <artifactId>jcommander</artifactId>
    <version>1.82</version>
</dependency>

Complete pom.xml configuration

<?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.example</groupId>
    <artifactId>cmd-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
        <dependency>
            <groupId>com.beust</groupId>
            <artifactId>jcommander</artifactId>
            <version>1.82</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>app</finalName>

        <plugins>
            <!-- 跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>

            <!-- jar -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <!-- 生成的jar中不要包含pom.xml和pom.properties这两个文件 -->
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <!--项目的启动类-->
                            <mainClass>com.example.Demo</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!-- 依赖 dependencies -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-lib</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <includeScope>runtime</includeScope>
                            <useBaseVersion>true</useBaseVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

define parameters

package com.example;

import com.beust.jcommander.Parameter;

public class CommandOptions {
    
    
    @Parameter(names = {
    
    "--name", "-n"},
            description = "用户姓名")
    private String name;

    public String getName() {
    
    
        return name;
    }
}

Entrance

package com.example;

import com.beust.jcommander.JCommander;

public class Demo {
    
    
    public static void main(String[] args) {
    
    

        CommandOptions commandOptions = new CommandOptions();
        JCommander commander = JCommander.newBuilder()
                .addObject(commandOptions)
                .build();

        // 解析参数
        commander.parse(args);

        System.out.println(commandOptions.getName());
    }
}

# 打包
$ mvn clean package

# 执行
$ java -jar target/app.jar --name Tom
Tom

$ java -jar target/app.jar -n Tom
Tom

reference

  1. Use JCommander to parse command line arguments
  2. Three ways to generate Jar files using Maven

Guess you like

Origin blog.csdn.net/mouday/article/details/131391176