使用 java 构建一个 cli 工具 (1) jcommander

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014087707/article/details/82530555

jcommander 是一个命令行参数解析的库. 

example

1. 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>org.java.common.cli</groupId>
    <artifactId>example-cli</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <main-class>example.cli.Application</main-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.beust</groupId>
            <artifactId>jcommander</artifactId>
            <version>1.72</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--打包成可执行的jar-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>executable</shadedClassifierName>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>${main-class}</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.skife.maven</groupId>
                <artifactId>really-executable-jar-maven-plugin</artifactId>
                <configuration>
                    <flags>-Xmx1G</flags>
                    <classifier>executable</classifier>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>really-executable-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2. 编码

public class Application {

    public static void main(String[] args) {
        ClientOptions co = new ClientOptions();
        JCommander jCommander = JCommander.newBuilder()
                .addObject(co)
                .build();

        jCommander.parse(args);

        if (co.isHelp()) {
            jCommander.usage();
            return;
        }

        System.out.println(co.getLevel());

    }
}

3. options

package example.cli;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;


@Parameters(separators = "=", commandDescription = "java cli 可执行程序")
public class ClientOptions {

    @Parameter(names = {"-level", "-l"}, description = "Level of verbosity", required = true)
    private Integer level = 2;

    @Parameter(names = "--help", help = true)
    private boolean help;

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public boolean isHelp() {
        return help;
    }

    public void setHelp(boolean help) {
        this.help = help;
    }
}

4. 执行 mvn clean install

5. 执行

1. java -jar example-cli-1.0-SNAPSHOT-executable.jar --help

输出

Usage: <main class> [options]
  Options:
    --help

  * -level, -l
      Level of verbosity
      Default: 2



2. java -jar example-cli-1.0-SNAPSHOT-executable.jar -level 4

输出

4

3 java -jar example-cli-1.0-SNAPSHOT-executable.jar -level=4

输出

4

以上可以解析命令行的传递的参数,更多请参考:   http://www.jcommander.org/  其实我们可以更简便一些. 可以构建像 

git 类似的可以直接运行的java 程序. 下篇见

猜你喜欢

转载自blog.csdn.net/u014087707/article/details/82530555