From ecd27de09adedc54edaf230d405ad2f4d3841653 Mon Sep 17 00:00:00 2001 From: Eric Bottard Date: Fri, 2 Jun 2017 10:45:16 +0200 Subject: [PATCH] Fix implied boolean arity, even if annotation is used Fixes #83 --- .../springframework/shell2/standard/ShellOption.java | 10 ++++++++-- .../shell2/standard/StandardParameterResolver.java | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/ShellOption.java b/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/ShellOption.java index 62dea8c9..6978feeb 100644 --- a/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/ShellOption.java +++ b/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/ShellOption.java @@ -37,6 +37,11 @@ public @interface ShellOption { String NONE = "__NONE__"; + /** + * Marker value to indicate that heuristics should be used to derive arity. + */ + int ARITY_USE_HEURISTICS = -1; + /** * The key(s) (without the {@link ShellMethod#prefix()}) by which this parameter can be referenced * when using named parameters. If none is specified, the actual method parameter name will be used. @@ -44,9 +49,10 @@ public @interface ShellOption { String[] value() default {}; /** - * Return the number of input "words" this parameter consumes. + * Return the number of input "words" this parameter consumes. Default is 1, except when parameter type is boolean, + * in which case it is 0. */ - int arity() default 1; + int arity() default ARITY_USE_HEURISTICS; /** * The textual (pre-conversion) value to assign to this parameter if no value is provided by the user. diff --git a/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/StandardParameterResolver.java b/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/StandardParameterResolver.java index d06ada64..3e4adf90 100644 --- a/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/StandardParameterResolver.java +++ b/spring-shell2-standard/src/main/java/org/springframework/shell2/standard/StandardParameterResolver.java @@ -371,7 +371,7 @@ public class StandardParameterResolver implements ParameterResolver { private int getArity(Parameter parameter) { ShellOption option = parameter.getAnnotation(ShellOption.class); int inferred = (parameter.getType() == boolean.class || parameter.getType() == Boolean.class) ? 0 : 1; - return option != null ? option.arity() : inferred; + return option != null && option.arity() != ShellOption.ARITY_USE_HEURISTICS ? option.arity() : inferred; } /**