Fix method argument without ShellOption

- For annotated methods with arguments, change default arity
  to zero with booleans and one everything else regardless
  if @ShellOption is defined or not.
- OptionArity.ZERO_OR_ONE had wrong upperbound value, change
  from MAX to 1.
- These modification should take us a bit closer to old
  shell functionality and what ShellOption documents for arity.
- For old functionality I'm referring to method
  `add(int a, int b)` and/or having @ShellOption and/or without
  arity setting.
- Fixes #446
This commit is contained in:
Janne Valkealahti
2022-07-14 20:21:52 +01:00
parent c9cea37cf0
commit 5ba8e185bc
2 changed files with 25 additions and 3 deletions

View File

@@ -608,7 +608,7 @@ public interface CommandRegistration {
break;
case ZERO_OR_ONE:
this.arityMin = 0;
this.arityMax = Integer.MAX_VALUE;
this.arityMax = 1;
break;
case EXACTLY_ONE:
this.arityMin = 1;

View File

@@ -42,6 +42,7 @@ import org.springframework.shell.Utils;
import org.springframework.shell.command.CommandCatalog;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.command.CommandRegistration.Builder;
import org.springframework.shell.command.CommandRegistration.OptionArity;
import org.springframework.shell.command.CommandRegistration.OptionSpec;
import org.springframework.shell.completion.CompletionResolver;
import org.springframework.shell.standard.ShellOption.NoValueProvider;
@@ -131,8 +132,9 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App
}
if (!longNames.isEmpty() || !shortNames.isEmpty()) {
log.debug("Registering longNames='{}' shortNames='{}'", longNames, shortNames);
Class<?> parameterType = mp.getParameterType();
OptionSpec optionSpec = builder.withOption()
.type(mp.getParameterType())
.type(parameterType)
.longNames(longNames.toArray(new String[0]))
.shortNames(shortNames.toArray(new Character[0]))
.position(mp.getParameterIndex())
@@ -140,6 +142,17 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App
if (so.arity() > -1) {
optionSpec.arity(0, so.arity());
}
else {
if (ClassUtils.isAssignable(boolean.class, parameterType)) {
optionSpec.arity(OptionArity.ZERO);
}
else if (ClassUtils.isAssignable(Boolean.class, parameterType)) {
optionSpec.arity(OptionArity.ZERO);
}
else {
optionSpec.arity(OptionArity.EXACTLY_ONE);
}
}
if (!ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NONE)
&& !ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NULL)) {
optionSpec.defaultValue(so.defaultValue());
@@ -163,11 +176,20 @@ public class StandardMethodTargetRegistrar implements MethodTargetRegistrar, App
Class<?> parameterType = mp.getParameterType();
if (longName != null) {
log.debug("Using mp='{}' longName='{}' parameterType='{}'", mp, longName, parameterType);
builder.withOption()
OptionSpec optionSpec = builder.withOption()
.longNames(longName)
.type(parameterType)
.required()
.position(mp.getParameterIndex());
if (ClassUtils.isAssignable(boolean.class, parameterType)) {
optionSpec.arity(OptionArity.ZERO);
}
else if (ClassUtils.isAssignable(Boolean.class, parameterType)) {
optionSpec.arity(OptionArity.ZERO);
}
else {
optionSpec.arity(OptionArity.EXACTLY_ONE);
}
}
}
}