位置的
位置信息主要与指令目标方法相关:
CommandRegistration.builder()
.withOption()
.longNames("arg1")
.position(0)
.and()
.build();
| 位置参数可能很快会被注意 让它们映射到哪些选项变得混淆。 |
通常参数在定义于一个选项时会被映射到 无论是长还是短,命令行都行。一般而言 有选项、选项论证和后者 是那些没有映射到任何特定选项的。
未识别的参数则可以有一个次级映射逻辑,其中 位置信息很重要。用期权位置你是 本质上就是告诉命令解析如何解释纯RAW。 模糊的论点。
让我们看看当我们不定义一个立场时会发生什么。
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();
必须选项 arg1,但没有关于如何处理论点的信息一导致缺少选项的错误。
shell:>arity-strings-1 one
Missing mandatory option --arg1.
现在我们定义一个位置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();
争论会被处理到最多有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]