ポジショナル

位置情報は、主にコマンドターゲットメソッドに関連しています。

CommandRegistration.builder()
	.withOption()
		.longNames("arg1")
		.position(0)
		.and()
	.build();
どのオプションにマッピングされているかがすぐにわかりにくくなる可能性があるため、位置パラメーターには注意してください。

通常、引数は、長いオプションであるか短いオプションであるかに関係なく、コマンドラインで定義されるときにオプションにマップされます。一般的に言えば、オプションオプション引数引数があり、後者は特定のオプションにマップされていないものです。

認識されない引数は、位置情報が重要な二次マッピングロジックを持つことができます。オプションの位置を使用すると、基本的にコマンド解析に、プレーンな生のあいまいな引数を解釈する方法を伝えます。

位置を定義しないとどうなるか見てみましょう。

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 が必要ですが、引数 one をどうするかについての情報がなく、オプションが見つからないためにエラーが発生しました。

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]