gpt4 book ai didi

java - apache CommandLineParser 示例的 junit 测试

转载 作者:行者123 更新时间:2023-12-01 13:14:53 25 4
gpt4 key购买 nike

这里是使用 apache CommandLineParser 的示例代码

public class Foo {

public static void main(final String[] args) throws Exception {

Options options = new Options();

options.addOption("x",
true, "comment for param x");

options.addOption("y",
true, "comment for param y");


CommandLine commandLine = null;

CommandLineParser parser = new PosixParser();

try {
commandLine = parser.parse(options, args);
} catch (ParseException e) {
throw new RuntimeException("Error parsing arguments!");
}

if (!commandLine.hasOption("x")) {
throw new IllegalArgumentException("x"
+ " option is missing!");
}

String numberOfColumns = commandLine.getOptionValue("x");
:
:

}

JUNIT测试代码:

@Test
public void testFoo() throws Exception {

args = new String[2];

args[0] = "x" + "=" + "hello";

args[1] = "y" + "=" + "world";

Foo.main(args);

}

我的问题/问题:

CommandLineParser 不断提示“x 选项丢失!”。所以我相信,我将参数及其值传递给命令行解析器的方式是错误的。我也尝试过其他方法。

    args[0] = "-x" + "=" + "hello";
args[1] = "-y" + "=" + "world";

还有

    args[0] = "x" 
args[1] = "hello";
args[2] = "y"
args[3] = "world";

有人可以告诉我传递参数及其值的正确格式吗?

提前致谢。

最佳答案

据我所知,参数必须以减号开头。

args[0] = "-x";
args[1] = "hello";
args[2] = "-y";
args[3] = "world";

关于java - apache CommandLineParser 示例的 junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22545116/

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