gpt4 book ai didi

Java CLI : Cant parse arguments

转载 作者:行者123 更新时间:2023-12-01 09:53:11 24 4
gpt4 key购买 nike

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

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

我错过了什么?

最佳答案

当选项带有参数时,您应该对 addOption 方法使用 true。来自 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)。

关于Java CLI : Cant parse arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com