已复制
全屏展示
复制代码

Java命令行解析commons-cli完整示例

· 2 min read

pom依赖

<dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.4</version>
</dependency>

完整示例

  • Config用于存最终的参数值
  • .longOpt  指定长参数名称
  • .hasArg  参数后面是否有值
  • .required  是否必传参数
  • .desc  描述信息
  • OptionGroup groupAction = new OptionGroup();创建一个组,组内的参数是互斥的,只能传一个。

示例代码

package com.yuchaoshui;

import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.ParseException;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws Exception {
        Options options = new Options();
        CommandLineParser parser = new DefaultParser();
        Config config = new Config();
        CommandLine cmdAgs = null;

        options.addOption(Option.builder("h")
                .longOpt("help").hasArg(false).required(false)
                .desc("show this help message then exit.").build());
        options.addOption(Option.builder("m")
                .longOpt("mode").hasArg(true).required(true)
                .desc("select a mode: r, w, rw.").build());
        options.addOption(Option.builder("disable")
                .hasArg(false).required(false)
                .desc("disable auto restart.").build());

        OptionGroup groupAction = new OptionGroup();
        groupAction.addOption(Option.builder("move")
                .hasArg(false).required(false)
                .desc("move to destination.").build());
        groupAction.addOption(Option.builder("copy")
                .hasArg(false).required(false)
                .desc("copy to destination.").build());
        groupAction.setRequired(false);
        options.addOptionGroup(groupAction);

        if (Arrays.asList(args).contains("-h")
                || Arrays.asList(args).contains("--help")) {
            config.printUsage(options);
            System.exit(0);
        }

        try {
            cmdAgs = parser.parse(options, args);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
            config.printUsage(options);
            System.exit(1);
        }

        String mode = cmdAgs.getOptionValue("m", "r");
        String[] choice = {"r", "w", "rw"};
        if (!Arrays.asList(choice).contains(mode)) {
            System.err.println("Exception: mode must be: r, w, rw");
            config.printUsage(options);
            System.exit(1);
        }
        config.mode = mode;
        config.disable = cmdAgs.hasOption("disable");
        config.copy = cmdAgs.hasOption("copy");
        config.move = cmdAgs.hasOption("move");

        System.out.printf("mode: %s\n", config.mode);
        System.out.printf("disable: %s\n", config.disable);
        System.out.printf("copy: %s\n", config.copy);
        System.out.printf("move: %s\n", config.move);
    }
}

class Config {
    String mode;
    Boolean disable;
    Boolean copy;
    Boolean move;
    private HelpFormatter formatter = new HelpFormatter();

    void printUsage(Options options) {
        formatter.printHelp("java -jar netflow-1.0.jar", options, true);
    }
}
🔗

文章推荐