Remove uses of "--" as constant prefix

Code now uses the prefixForMethod(...)
added sample usage of prefix in a @ShellMethod
This commit is contained in:
camilojc
2017-06-18 20:56:36 +01:00
committed by Eric Bottard
parent 8398b140ae
commit 7fd5390c29
4 changed files with 27 additions and 8 deletions

View File

@@ -44,8 +44,13 @@ public class Commands {
}
@ShellMethod(help = "it's cool")
public void foo(String bar) {
public String foo(String bar) {
return bar;
}
@ShellMethod(help = "it's cool with a prefix", prefix = "-")
public String fooPrefix(String bar) {
return bar;
}
@ShellMethod(help = "Shows support for boolean parameters, with arity=0")

View File

@@ -105,7 +105,7 @@ public class StandardParameterResolver implements ParameterResolver {
@Override
public Object resolve(MethodParameter methodParameter, List<String> words) {
String prefix = prefixForMethod(methodParameter);
String prefix = prefixForMethod(methodParameter.getMethod());
CacheKey cacheKey = new CacheKey(methodParameter.getMethod(), words);
Map<Parameter, ParameterRawValue> resolved = parameterCache.computeIfAbsent(cacheKey, (k) -> {
@@ -195,7 +195,7 @@ public class StandardParameterResolver implements ParameterResolver {
}
private Set<String> gatherAllPossibleKeys(Method method) {
final String prefix = "--";
final String prefix = prefixForMethod(method);
return Arrays.stream(method.getParameters())
.flatMap(p -> {
ShellOption option = p.getAnnotation(ShellOption.class);
@@ -208,8 +208,8 @@ public class StandardParameterResolver implements ParameterResolver {
}).collect(Collectors.toSet());
}
private String prefixForMethod(MethodParameter methodParameter) {
return methodParameter.getMethod().getAnnotation(ShellMethod.class).prefix();
private String prefixForMethod(Method method) {
return method.getAnnotation(ShellMethod.class).prefix();
}
private Optional<String> defaultValueFor(Parameter parameter) {
@@ -380,7 +380,7 @@ public class StandardParameterResolver implements ParameterResolver {
* or from the actual parameter name.
*/
private Stream<String> getKeysForParameter(Method method, int index) {
String prefix = "--";
String prefix = prefixForMethod(method);
Parameter p = method.getParameters()[index];
ShellOption option = p.getAnnotation(ShellOption.class);
if (option != null && option.value().length > 0) {

View File

@@ -45,12 +45,16 @@ public class Remote {
String name,
@ShellOption(defaultValue="defoolt") String foo,
@ShellOption(value = {"--bar", "--baz"}, defaultValue = "last") String bar) {
}
@ShellMethod(help = "bye bye")
public void shutdown(@ShellOption Delay delay) {
}
@ShellMethod(help = "a different prefix", prefix = "-")
public void prefixTest(@ShellOption String message) {
}
@ShellMethod(help = "add 3 numbers together")

View File

@@ -74,6 +74,16 @@ public class StandardParameterResolverTest {
)).isEqualTo("last");
}
@Test
public void testParsesWithMethodPrefix() throws Exception {
Method method = findMethod(Remote.class, "prefixTest", String.class);
assertThat(resolver.resolve(
Utils.createMethodParameter(method, 0),
asList("-message abc".split(" "))
)).isEqualTo("abc");
}
@Test
public void testParameterSpecifiedTwiceViaDifferentAliases() throws Exception {