Add ValueProvider for command names, use in "help"
Also, add some cases to samples Fixes #42 Add tests
This commit is contained in:
@@ -38,6 +38,7 @@ import org.springframework.shell2.MethodTarget;
|
||||
import org.springframework.shell2.ParameterDescription;
|
||||
import org.springframework.shell2.ParameterResolver;
|
||||
import org.springframework.shell2.Shell;
|
||||
import org.springframework.shell2.standard.CommandValueProvider;
|
||||
import org.springframework.shell2.standard.ShellComponent;
|
||||
import org.springframework.shell2.standard.ShellMethod;
|
||||
import org.springframework.shell2.standard.ShellOption;
|
||||
@@ -68,6 +69,7 @@ public class Help {
|
||||
@ShellMethod(help = "Display help about available commands.", prefix = "-")
|
||||
public CharSequence help(
|
||||
@ShellOption(defaultValue = ShellOption.NULL,
|
||||
valueProvider = CommandValueProvider.class,
|
||||
value = {"-C", "--command"},
|
||||
help = "The command to obtain help for.") String command) throws IOException {
|
||||
if (command == null) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell2.standard;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell2.CompletionContext;
|
||||
import org.springframework.shell2.CompletionProposal;
|
||||
import org.springframework.shell2.Shell;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* A {@link ValueProvider} that can be used to auto-complete names of shell commands.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
@Component
|
||||
public class CommandValueProvider extends ValueProviderSupport {
|
||||
|
||||
private final Shell shell;
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
public CommandValueProvider(Shell shell) {
|
||||
this.shell = shell;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext completionContext, String[] hints) {
|
||||
return shell.listCommands().keySet().stream()
|
||||
.map(CompletionProposal::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell2.standard;
|
||||
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.shell2.CompletionContext;
|
||||
import org.springframework.shell2.CompletionProposal;
|
||||
import org.springframework.shell2.MethodTarget;
|
||||
import org.springframework.shell2.Shell;
|
||||
import org.springframework.shell2.Utils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CommandValueProvider}.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class CommandValueProviderTest {
|
||||
|
||||
@Mock
|
||||
private Shell shell;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValues() {
|
||||
CommandValueProvider valueProvider = new CommandValueProvider(shell);
|
||||
|
||||
Method help = ReflectionUtils.findMethod(Command.class, "help", String.class);
|
||||
MethodParameter methodParameter = Utils.createMethodParameter(help, 0);
|
||||
CompletionContext completionContext = new CompletionContext(Arrays.asList("help", "m"), 0, 0);
|
||||
boolean supports = valueProvider.supports(methodParameter, completionContext);
|
||||
|
||||
assertThat(supports).isEqualTo(true);
|
||||
|
||||
Map<String, MethodTarget> commands = new HashMap<>();
|
||||
commands.put("me", null);
|
||||
commands.put("meow", null);
|
||||
commands.put("yourself", null);
|
||||
when(shell.listCommands()).thenReturn(commands);
|
||||
List<CompletionProposal> proposals = valueProvider.complete(methodParameter, completionContext, new String[0]);
|
||||
|
||||
assertThat(proposals).extracting("value", String.class)
|
||||
.contains("me", "meow", "yourself");
|
||||
}
|
||||
|
||||
|
||||
public static class Command {
|
||||
|
||||
public void help(@ShellOption(valueProvider = CommandValueProvider.class) String command) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testParses() throws Exception {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
|
||||
assertThat(resolver.resolve(
|
||||
Utils.createMethodParameter(method, 0),
|
||||
@@ -78,7 +78,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testParameterSpecifiedTwiceViaDifferentAliases() throws Exception {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Named parameter has been specified multiple times via '--bar, --baz'");
|
||||
@@ -91,7 +91,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testParameterSpecifiedTwiceViaSameKey() throws Exception {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("Parameter for '--baz' has already been specified");
|
||||
@@ -104,7 +104,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testTooMuchInput() throws Exception {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
|
||||
thrown.expect(IllegalArgumentException.class);
|
||||
thrown.expectMessage("the following could not be mapped to parameters: 'leftover'");
|
||||
@@ -117,7 +117,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testIncompleteCommandResolution() throws Exception {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "shutdown", org.springframework.shell2.standard.Remote.Delay.class);
|
||||
Method method = findMethod(Remote.class, "shutdown", Remote.Delay.class);
|
||||
|
||||
thrown.expect(UnfinishedParameterResolutionException.class);
|
||||
thrown.expectMessage("Error trying to resolve '--delay delay' using [--delay]");
|
||||
@@ -130,7 +130,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testIncompleteCommandResolutionBigArity() throws Exception {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "add", List.class);
|
||||
Method method = findMethod(Remote.class, "add", List.class);
|
||||
|
||||
thrown.expect(UnfinishedParameterResolutionException.class);
|
||||
thrown.expectMessage("Error trying to resolve '--numbers list list list' using [--numbers 1 2]");
|
||||
@@ -143,7 +143,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testUnresolvableArg() throws Exception {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
|
||||
thrown.expect(ParameterMissingResolutionException.class);
|
||||
thrown.expectMessage("Parameter '--name string' should be specified");
|
||||
@@ -158,7 +158,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testParameterKeyNotYetSetAppearsInProposals() {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
List<String> completions = resolver.complete(
|
||||
Utils.createMethodParameter(method, 1),
|
||||
contextFor("")
|
||||
@@ -173,7 +173,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testParameterKeyNotFullySpecified() {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
List<String> completions = resolver.complete(
|
||||
Utils.createMethodParameter(method, 1),
|
||||
contextFor("--na")
|
||||
@@ -188,7 +188,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testNoMoreAvailableParameters() {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
Method method = findMethod(Remote.class, "zap", boolean.class, String.class, String.class, String.class);
|
||||
List<String> completions = resolver.complete(
|
||||
Utils.createMethodParameter(method, 2), // trying to complete --foo
|
||||
contextFor("--name ") // but input is currently focused on --name
|
||||
@@ -199,7 +199,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
@Test
|
||||
public void testNotTheRightTimeToCompleteThatParameter() {
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "shutdown", org.springframework.shell2.standard.Remote.Delay.class);
|
||||
Method method = findMethod(Remote.class, "shutdown", Remote.Delay.class);
|
||||
List<String> completions = resolver.complete(
|
||||
Utils.createMethodParameter(method, 0),
|
||||
contextFor("--delay 323")
|
||||
@@ -213,7 +213,7 @@ public class StandardParameterResolverTest {
|
||||
|
||||
resolver.setValueProviders(Arrays.asList(new Remote.NumberValueProvider()));
|
||||
|
||||
Method method = findMethod(org.springframework.shell2.standard.Remote.class, "add", List.class);
|
||||
Method method = findMethod(Remote.class, "add", List.class);
|
||||
List<String> completions = resolver.complete(
|
||||
Utils.createMethodParameter(method, 0),
|
||||
contextFor("--numbers ")
|
||||
|
||||
@@ -21,11 +21,13 @@ import java.lang.reflect.Method;
|
||||
import org.springframework.shell.core.CommandMarker;
|
||||
import org.springframework.shell.core.annotation.CliCommand;
|
||||
import org.springframework.shell.core.annotation.CliOption;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Created by ericbottard on 09/12/15.
|
||||
*/
|
||||
@Component
|
||||
public class LegacyCommands implements CommandMarker {
|
||||
|
||||
public static final Method REGISTER_METHOD = ReflectionUtils.findMethod(LegacyCommands.class, "register", String.class, ArtifactType.class, String.class, boolean.class);
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.shell2.samples.standard;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.shell2.standard.ShellComponent;
|
||||
import org.springframework.shell2.standard.ShellMethod;
|
||||
|
||||
@@ -46,4 +49,9 @@ public class Commands {
|
||||
public int add(int ahbahdisdonc, int b, int c) {
|
||||
return ahbahdisdonc + b + c;
|
||||
}
|
||||
|
||||
@ShellMethod(help = "Fails with an exception")
|
||||
public void fail(ElementType elementType) {
|
||||
throw new IllegalArgumentException("You said " + elementType);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user