Java CLI:无法解析参数

Java CLI : Cant parse arguments

我正在尝试将命令行参数解析为

Options options = new Options();
        options.addOption("c", "count", false, "number of message to be generated");
        options.addOption("s", "size", false, "size of each messages in bytes");
        options.addOption("t", "threads", false, "number of threads");
        options.addOption("r", "is random", false, "is random");
        CommandLine cli = new DefaultParser().parse(options, args);

        int count = Integer.parseInt(cli.getOptionValue("c", "20000000"));
//        int count = Integer.parseInt(cli.getOptionValue("c", "100"));
        int recordSize = Integer.parseInt(cli.getOptionValue("s", "512"));
        int threads = Integer.parseInt(cli.getOptionValue("t","4"));
        boolean isRandom = Boolean.valueOf(cli.getOptionValue("r", "true"));
        System.out.println(" threads "+threads);
        System.out.println(" count "+count);

我运行它在eclipse中与

t 6 c 7

但我总是得到

threads 4
count 20000000

我错过了什么?

You should use true for addOption method when the option takes an argument.来自 Javadoc:

  • @param hasArg flag signally if an argument is required after this option
    options.addOption("c", "count", true, "number of message to be generated");
    options.addOption("s", "size", true, "size of each messages in bytes");
    options.addOption("t", "threads", true, "number of threads");
    options.addOption("r", "is random", false, "is random");

是,短选项规范(例如-t 4)需要前​​导-,长选项规范(例如--threads 4)需要前​​导-- ).