Add command group and group output of help command

Resolves #135
This commit is contained in:
Roland Weisleder
2017-09-23 15:33:57 +02:00
parent b21a1f16f1
commit e4e6471f27
6 changed files with 93 additions and 27 deletions

View File

@@ -37,16 +37,22 @@ public class MethodTarget {
private final String help;
private final String group;
/**
* If not null, returns whether or not the command is currently available. Implementations must be idempotent.
*/
private final Supplier<Availability> availabilityIndicator;
public MethodTarget(Method method, Object bean, String help) {
this(method, bean, help, null);
this(method, bean, help, null, null);
}
public MethodTarget(Method method, Object bean, String help, Supplier<Availability> availabilityIndicator) {
this(method, bean, help, null, availabilityIndicator);
}
public MethodTarget(Method method, Object bean, String help, String group, Supplier<Availability> availabilityIndicator) {
Assert.notNull(method, "Method cannot be null");
Assert.notNull(bean, "Bean cannot be null");
Assert.hasText(help, String.format("Help cannot be blank when trying to define command based on '%s'", method));
@@ -54,6 +60,7 @@ public class MethodTarget {
this.method = method;
this.bean = bean;
this.help = help;
this.group = group != null ? group : "";
this.availabilityIndicator = availabilityIndicator != null ? availabilityIndicator : () -> Availability.available();
}
@@ -62,21 +69,29 @@ public class MethodTarget {
* in case of overloaded method.
*/
public static MethodTarget of(String name, Object bean, String help) {
return of(name, bean, help, null);
return of(name, bean, help, null, null);
}
/**
* Construct a MethodTarget for the unique method named {@literal name} on the given object. Fails with an exception
* in case of overloaded method.
*/
public static MethodTarget of(String name, Object bean, String help, Supplier<Availability> availabilityIndicator) {
public static MethodTarget of(String name, Object bean, String help, String group) {
return of(name, bean, help, group, null);
}
/**
* Construct a MethodTarget for the unique method named {@literal name} on the given object. Fails with an exception
* in case of overloaded method.
*/
public static MethodTarget of(String name, Object bean, String help, String group, Supplier<Availability> availabilityIndicator) {
Set<Method> found = new HashSet<>();
ReflectionUtils.doWithMethods(bean.getClass(), found::add, m -> m.getName().equals(name));
if (found.size() != 1) {
throw new IllegalArgumentException(String.format("Could not find unique method named '%s' on object of class %s. Found %s",
name, bean.getClass(), found));
}
return new MethodTarget(found.iterator().next(), bean, help, availabilityIndicator);
return new MethodTarget(found.iterator().next(), bean, help, group, availabilityIndicator);
}
public Method getMethod() {
@@ -91,6 +106,10 @@ public class MethodTarget {
return help;
}
public String getGroup() {
return group;
}
public Availability getAvailability() {
return availabilityIndicator.get();
}
@@ -104,6 +123,7 @@ public class MethodTarget {
if (!method.equals(that.method)) return false;
if (!bean.equals(that.bean)) return false;
if (!group.equals(that.group)) return false;
return help.equals(that.help);
}
@@ -113,6 +133,7 @@ public class MethodTarget {
int result = method.hashCode();
result = 31 * result + bean.hashCode();
result = 31 * result + help.hashCode();
result = 31 * result + group.hashCode();
return result;
}