已复制
全屏展示
复制代码

Java命令行解析jcommander总结

· 2 min read

一. 完整示例

jcommander  官网地址 http://jcommander.org/

  • pom.xml
<dependency>
    <groupId>com.beust</groupId>
    <artifactId>jcommander</artifactId>
    <version>1.78</version>
</dependency>
  • 完整代码
    下面给出了一个完整的代码逻辑,还有更多的用法见后面小节。
package com.yuchaoshui.app;

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

public class Application {
    public static void main(String[] args) {
        args = new String[]{"-h"};
        ArgsParser options = new ArgsParser().parse(args);
        process(options);
    }

    public static void process(ArgsParser options) {
        System.out.println("options.version -> " + options.version);
        System.out.println("options.address -> " + options.address);
        System.out.println("options.namenode.list -> " + options.namenode.list);
    }
}

class ArgsParser {
    @Parameter(names = {"-h", "--help"}, description = "Show this help", order = 0)
    boolean help = false;

    // version default is false
    @Parameter(names = {"-v", "--version"}, description = "Program version", order = 0)
    boolean version = false;

    // proxy default is true
    @Parameter(names = {"-p", "--proxy"}, description = "Use proxy", arity = 1, order = 1)
    boolean proxy = true;

    @Parameter(names = {"-a", "--address"}, description = "Company address", order = 1)
    String address = "beijing";

    CommandDataNode datanode = new CommandDataNode();
    CommandNameNode namenode = new CommandNameNode();

    // subcommand
    static class CommandDemo {
        boolean given = false;
    }

    // subcommand
    @Parameters(commandDescription = "datanode command and options")
    static class CommandDataNode extends CommandDemo {
        @Parameter(names = {"--email"}, description = "Email")
        Boolean email = false;

        @Parameter(names = {"--author"}, description = "Author")
        String author = "unnamed";
    }

    // subcommand
    @Parameters(commandDescription = "namenode command and options")
    static class CommandNameNode extends CommandDemo {
        @Parameter(names = {"--list"}, description = "list file detail")
        String list = ".";
    }

    public ArgsParser parse(String[] args) {
        ArgsParser options = new ArgsParser();
        JCommander jc = JCommander.newBuilder().addObject(options)
                .addCommand("datanode", options.datanode)
                .addCommand("namenode", options.namenode)
                .build();
        try {
            jc.parse(args);
            if (options.help) {
                jc.usage();
                System.exit(0);
            } else if (options.version) {
                System.out.println("0.0.1");
                System.exit(0);
            } else if (jc.getParsedCommand() != null) {
                if (jc.getParsedCommand().equals("datanode")) {
                    options.datanode.given = true;
                } else if (jc.getParsedCommand().equals("namenode")) {
                    options.namenode.given = true;
                }
            }
        } catch (ParameterException parameterException) {
            System.out.println(parameterException.toString());
            jc.usage();
            System.exit(0);
        }
        return options;
    }
}

二. 更多用法

  • 传入多个值: java Main -host host1 -debug -host host2
@Parameter(names = "-host", description = "The host")
List<String> hosts = new ArrayList<>();
  • 密码字段:可以以交互式的方式提供密码 Connection password:
# 不显示输入的密码
@Parameter(names = "-password", description = "Connection password", password = true)
String password;

# 显示输入的密码
@Parameter(names = "-password", description = "Connection password", password = true, echoInput = true)
String password;
🔗

文章推荐