OptionArity NONE value

- Backport #644
- Fixes #645
This commit is contained in:
Janne Valkealahti
2023-01-27 09:41:03 +00:00
parent 1a1ddaadc7
commit 1c4608fb89
2 changed files with 53 additions and 3 deletions

View File

@@ -271,12 +271,41 @@ public interface CommandRegistration {
Builder and();
}
/**
* Enumeration of option arity values.
*/
public enum OptionArity {
/**
* Used to indicate that arity is not set. Exists as a workaround for a case
* where it's not possible to use null values.
*/
NONE,
/**
* Define min(0), max(0).
*/
ZERO,
/**
* Define min(0), max(1).
*/
ZERO_OR_ONE,
/**
* Define min(1), max(1).
*/
EXACTLY_ONE,
/**
* Define min(0), max(MAXINTEGER).
*/
ZERO_OR_MORE,
ONE_OR_MORE
/**
* Define min(1), max(MAXINTEGER).
*/
ONE_OR_MORE;
}
/**
@@ -829,6 +858,10 @@ public interface CommandRegistration {
@Override
public OptionSpec arity(OptionArity arity) {
switch (arity) {
case NONE:
this.arityMin = null;
this.arityMax = null;
break;
case ZERO:
this.arityMin = 0;
this.arityMax = 0;
@@ -850,8 +883,8 @@ public interface CommandRegistration {
this.arityMax = Integer.MAX_VALUE;
break;
default:
this.arityMin = 0;
this.arityMax = 0;
this.arityMin = null;
this.arityMax = null;
break;
}
return this;

View File

@@ -360,6 +360,23 @@ public class CommandRegistrationTests extends AbstractCommandTests {
assertThat(registration.getOptions().get(0).getArityMax()).isEqualTo(0);
}
@Test
public void testArityViaEnumSetNone() {
CommandRegistration registration = CommandRegistration.builder()
.command("command1")
.withOption()
.longNames("arg1")
.arity(OptionArity.NONE)
.and()
.withTarget()
.consumer(ctx -> {})
.and()
.build();
assertThat(registration.getOptions()).hasSize(1);
assertThat(registration.getOptions().get(0).getArityMin()).isEqualTo(-1);
assertThat(registration.getOptions().get(0).getArityMax()).isEqualTo(-1);
}
@Test
public void testAliases() {
CommandRegistration registration = CommandRegistration.builder()