跳到主要内容

定位

ChatGPT-4o-mini 中英对照 Positional

位置信息通常与命令目标方法相关:

CommandRegistration.builder()
.withOption()
.longNames("arg1")
.position(0)
.and()
.build();
java
备注

小心位置参数,因为很快就会变得混淆,这些参数映射到哪些选项。

通常,当命令行中定义了长选项或短选项时,参数会映射到一个选项。一般来说,有 选项选项参数参数,其中后者是没有映射到任何特定选项的参数。

未识别的参数可以具有二次映射逻辑,其中位置性信息很重要。通过选项 position,你基本上是在告诉命令解析如何解释普通的原始模糊参数。

让我们看看当我们不定义位置时会发生什么。

CommandRegistration.builder()
.command("arity-strings-1")
.withOption()
.longNames("arg1")
.required()
.type(String[].class)
.arity(0, 2)
.and()
.withTarget()
.function(ctx -> {
String[] arg1 = ctx.getOptionValue("arg1");
return "Hello " + Arrays.asList(arg1);
})
.and()
.build();
java

It seems like you're encountering an error related to a required option in your code. To help you better, could you please provide the relevant code snippet where this error occurs? This will allow me to assist you in resolving the issue effectively.

shell:>arity-strings-1 one
Missing mandatory option --arg1.
bash

现在让我们定义一个位置 0

CommandRegistration.builder()
.command("arity-strings-2")
.withOption()
.longNames("arg1")
.required()
.type(String[].class)
.arity(0, 2)
.position(0)
.and()
.withTarget()
.function(ctx -> {
String[] arg1 = ctx.getOptionValue("arg1");
return "Hello " + Arrays.asList(arg1);
})
.and()
.build();
java

参数会被处理,直到我们得到最多 2 个参数。

shell:>arity-strings-2 one
Hello [one]

shell:>arity-strings-2 one two
Hello [one, two]

shell:>arity-strings-2 one two three
Hello [one, two]
bash