簡體   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