For the latest stable version, please use Spring Shell 3.3.3!spring-doc.cn

Arity

Arity defines how many parameters option parsing takes.spring-doc.cn

There are limitations in a legacy annotation compared to annotation and programmatic use of arity settings. These are mentioned in notes in below samples.
CommandRegistration zeroOrOne() {
	return CommandRegistration.builder()
		.command("example")
		.withOption()
			.longNames("arg")
			.arity(OptionArity.ZERO_OR_ONE)
			.and()
		.build();
}
@Command(command = "example")
String zeroOrOne(
	@Option(arity = OptionArity.ZERO_OR_ONE) String arg)
{
	return String.format("Hi '%s'", arg);
}
@ShellMethod(key = "example")
String zeroOrOne(
	@ShellOption(arity = 1) String arg)
{
	return String.format("Hi '%s'", arg);
}
Table 1. OptionArity
Value min/max

ZEROspring-doc.cn

0 / 0spring-doc.cn

ZERO_OR_ONEspring-doc.cn

0 / 1spring-doc.cn

EXACTLY_ONEspring-doc.cn

1 / 1spring-doc.cn

ZERO_OR_MOREspring-doc.cn

0 / Integer MAXspring-doc.cn

ONE_OR_MOREspring-doc.cn

1 / Integer MAXspring-doc.cn

legacy annotation doesn’t support defining minimum arity.
CommandRegistration zeroOrOneWithMinMax() {
	return CommandRegistration.builder()
		.command("example")
		.withOption()
			.longNames("arg")
			.arity(0, 1)
			.and()
		.build();
}
@Command(command = "example")
String zeroOrOneWithMinMax(
	@Option(arityMin = 0, arityMax = 1) String arg)
{
	return String.format("Hi '%s'", arg);
}
@ShellMethod(key = "example")
String zeroOrOneWithMinMax(
	@ShellOption(arity = 1) String arg)
{
	return String.format("Hi '%s'", arg);
}

In below example we have option arg1 and it’s defined as type String[]. Arity defines that it needs at least 1 parameter and not more that 2. As seen in below spesific exceptions TooManyArgumentsOptionException and NotEnoughArgumentsOptionException are thrown to indicate arity mismatch.spring-doc.cn

shell:>e2e reg arity-errors --arg1
Not enough arguments --arg1 requires at least 1.

shell:>e2e reg arity-errors --arg1 one
Hello [one]

shell:>e2e reg arity-errors --arg1 one two
Hello [one, two]

shell:>e2e reg arity-errors --arg1 one two three
Too many arguments --arg1 requires at most 2.