繁体   English   中英

如何用jargo忽略其他参数?

[英]How can I ignore extra arguments with jargo?

我试图让jargo忽略任何数量的“在这里垃圾”字符串。 我怎样才能做到这一点? 这是我想出的代码:

@Test
public void testUsage() throws Exception
{
    Argument<Integer> nrOfPotatoes = Arguments.integerArgument("-n").build();
    ParsedArguments parsedArguments = CommandLineParser.withArguments(nrOfPotatoes).parse("-n", "123", "junk-be-here");
    int potatoesToPlant = parsedArguments.get(nrOfPotatoes);
    System.out.println("Hold on, planting " + potatoesToPlant + " potatoes");
}

但是我得到:

se.softhouse.jargo.ArgumentExceptions$UnexpectedArgumentException: Unexpected argument: junk-be-here, previous argument: 123
at se.softhouse.jargo.ArgumentExceptions.forUnexpectedArgument(ArgumentExceptions.java:299)
at se.softhouse.jargo.CommandLineParserInstance.getDefinitionForCurrentArgument(CommandLineParserInstance.java:329)
at se.softhouse.jargo.CommandLineParserInstance.parseArguments(CommandLineParserInstance.java:262)
at se.softhouse.jargo.CommandLineParserInstance.parse(CommandLineParserInstance.java:234)
at se.softhouse.jargo.CommandLineParserInstance.parse(CommandLineParserInstance.java:228)
at se.softhouse.jargo.CommandLineParser.parse(CommandLineParser.java:224)
at

.....

您可以使用带索引的参数(通过不为参数指定名称),并设置variableArity (允许任意数量的参数)。

    @Test
public void testUsage() throws Exception
{
    Argument<List<String>> junk = Arguments.stringArgument().variableArity().build();
    Argument<Integer> nrOfPotatoes = Arguments.integerArgument("-n").build();
    ParsedArguments parsedArguments = CommandLineParser.withArguments(junk, nrOfPotatoes).parse("-n", "123", "junk-be-here");
    int potatoesToPlant = parsedArguments.get(nrOfPotatoes);
    System.out.println("Hold on, planting " + potatoesToPlant + " potatoes");
    System.out.println("Junk:" + parsedArguments.get(junk));
}

打印:

Hold on, planting 123 potatoes
Junk:[junk-be-here]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM