Skip to content

Commit d1c482c

Browse files
committed
OptionArity NONE value
- Fixes spring-projects#644
1 parent 03b3438 commit d1c482c

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,41 @@ public interface OptionSpec {
271271
Builder and();
272272
}
273273

274+
/**
275+
* Enumeration of option arity values.
276+
*/
274277
public enum OptionArity {
278+
279+
/**
280+
* Used to indicate that arity is not set. Exists as a workaround for a case
281+
* where it's not possible to use null values.
282+
*/
283+
NONE,
284+
285+
/**
286+
* Define min(0), max(0).
287+
*/
275288
ZERO,
289+
290+
/**
291+
* Define min(0), max(1).
292+
*/
276293
ZERO_OR_ONE,
294+
295+
/**
296+
* Define min(1), max(1).
297+
*/
277298
EXACTLY_ONE,
299+
300+
/**
301+
* Define min(0), max(MAXINTEGER).
302+
*/
278303
ZERO_OR_MORE,
279-
ONE_OR_MORE
304+
305+
/**
306+
* Define min(1), max(MAXINTEGER).
307+
*/
308+
ONE_OR_MORE;
280309
}
281310

282311
/**
@@ -829,6 +858,10 @@ public OptionSpec arity(int min, int max) {
829858
@Override
830859
public OptionSpec arity(OptionArity arity) {
831860
switch (arity) {
861+
case NONE:
862+
this.arityMin = null;
863+
this.arityMax = null;
864+
break;
832865
case ZERO:
833866
this.arityMin = 0;
834867
this.arityMax = 0;
@@ -850,8 +883,8 @@ public OptionSpec arity(OptionArity arity) {
850883
this.arityMax = Integer.MAX_VALUE;
851884
break;
852885
default:
853-
this.arityMin = 0;
854-
this.arityMax = 0;
886+
this.arityMin = null;
887+
this.arityMax = null;
855888
break;
856889
}
857890
return this;

spring-shell-core/src/test/java/org/springframework/shell/command/CommandRegistrationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,23 @@ public void testArityViaEnum() {
360360
assertThat(registration.getOptions().get(0).getArityMax()).isEqualTo(0);
361361
}
362362

363+
@Test
364+
public void testArityViaEnumSetNone() {
365+
CommandRegistration registration = CommandRegistration.builder()
366+
.command("command1")
367+
.withOption()
368+
.longNames("arg1")
369+
.arity(OptionArity.NONE)
370+
.and()
371+
.withTarget()
372+
.consumer(ctx -> {})
373+
.and()
374+
.build();
375+
assertThat(registration.getOptions()).hasSize(1);
376+
assertThat(registration.getOptions().get(0).getArityMin()).isEqualTo(-1);
377+
assertThat(registration.getOptions().get(0).getArityMax()).isEqualTo(-1);
378+
}
379+
363380
@Test
364381
public void testAliases() {
365382
CommandRegistration registration = CommandRegistration.builder()

0 commit comments

Comments
 (0)