Use CommandContext as method parameter
- With annotation(@Command) model it's now possible to just add CommandContext and it will get resolved and doesn't cause it to appear as an option. - Fixes #779
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023 the original author or authors.
|
||||
* Copyright 2023-2024 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.
|
||||
@@ -40,6 +40,7 @@ import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.AvailabilityProvider;
|
||||
import org.springframework.shell.Utils;
|
||||
import org.springframework.shell.command.CommandContext;
|
||||
import org.springframework.shell.command.CommandExceptionResolver;
|
||||
import org.springframework.shell.command.CommandHandlingResult;
|
||||
import org.springframework.shell.command.CommandRegistration;
|
||||
@@ -322,6 +323,10 @@ class CommandRegistrationFactoryBean implements FactoryBean<CommandRegistration>
|
||||
mp.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||
String longName = mp.getParameterName();
|
||||
Class<?> parameterType = mp.getParameterType();
|
||||
// skip known types which are coming in via parameter resolvers
|
||||
if (ClassUtils.isAssignable(parameterType, CommandContext.class)) {
|
||||
return;
|
||||
}
|
||||
if (longName != null) {
|
||||
log.debug("Using mp='{}' longName='{}' parameterType='{}'", mp, longName, parameterType);
|
||||
OptionSpec optionSpec = builder.withOption();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
* Copyright 2022-2024 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.
|
||||
@@ -43,6 +43,10 @@ public abstract class AbstractCommandTests {
|
||||
|
||||
public int method1Count;
|
||||
public CommandContext method1Ctx;
|
||||
public int method1Mixed1Count;
|
||||
public String method1Mixed1Arg1;
|
||||
public CommandContext method1Mixed1Ctx;
|
||||
public String method1Mixed1Arg2;
|
||||
public int method2Count;
|
||||
public int method3Count;
|
||||
public int method4Count;
|
||||
@@ -68,6 +72,13 @@ public abstract class AbstractCommandTests {
|
||||
method1Count++;
|
||||
}
|
||||
|
||||
public void method1Mixed1(String arg1, CommandContext ctx, String arg2) {
|
||||
method1Mixed1Arg1 = arg1;
|
||||
method1Mixed1Ctx = ctx;
|
||||
method1Mixed1Arg2 = arg2;
|
||||
method1Mixed1Count++;
|
||||
}
|
||||
|
||||
public String method2() {
|
||||
method2Count++;
|
||||
return "hi";
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
* Copyright 2022-2024 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.
|
||||
@@ -106,6 +106,81 @@ public class CommandExecutionTests extends AbstractCommandTests {
|
||||
assertThat(pojo1.method1Ctx).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedWithCtx1() {
|
||||
CommandRegistration r1 = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.description("help")
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.description("some arg1")
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg2")
|
||||
.description("some arg1")
|
||||
.and()
|
||||
.withTarget()
|
||||
.method(pojo1, "method1Mixed1")
|
||||
.and()
|
||||
.build();
|
||||
commandCatalog.register(r1);
|
||||
execution.evaluate(new String[] { "command1" });
|
||||
assertThat(pojo1.method1Mixed1Count).isEqualTo(1);
|
||||
assertThat(pojo1.method1Mixed1Arg1).isNull();
|
||||
assertThat(pojo1.method1Mixed1Ctx).isNotNull();
|
||||
assertThat(pojo1.method1Mixed1Arg2).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedWithCtx2() {
|
||||
CommandRegistration r1 = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.description("help")
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.description("some arg1")
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg2")
|
||||
.description("some arg1")
|
||||
.and()
|
||||
.withTarget()
|
||||
.method(pojo1, "method1Mixed1")
|
||||
.and()
|
||||
.build();
|
||||
commandCatalog.register(r1);
|
||||
execution.evaluate(new String[] { "command1", "--arg1", "myarg1value" });
|
||||
assertThat(pojo1.method1Mixed1Count).isEqualTo(1);
|
||||
assertThat(pojo1.method1Mixed1Arg1).isEqualTo("myarg1value");
|
||||
assertThat(pojo1.method1Mixed1Ctx).isNotNull();
|
||||
assertThat(pojo1.method1Mixed1Arg2).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedWithCtx3() {
|
||||
CommandRegistration r1 = CommandRegistration.builder()
|
||||
.command("command1")
|
||||
.description("help")
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.description("some arg1")
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg2")
|
||||
.description("some arg1")
|
||||
.and()
|
||||
.withTarget()
|
||||
.method(pojo1, "method1Mixed1")
|
||||
.and()
|
||||
.build();
|
||||
commandCatalog.register(r1);
|
||||
execution.evaluate(new String[] { "command1", "--arg1", "myarg1value", "--arg2", "myarg2value" });
|
||||
assertThat(pojo1.method1Mixed1Count).isEqualTo(1);
|
||||
assertThat(pojo1.method1Mixed1Arg1).isEqualTo("myarg1value");
|
||||
assertThat(pojo1.method1Mixed1Ctx).isNotNull();
|
||||
assertThat(pojo1.method1Mixed1Arg2).isEqualTo("myarg2value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethodArgWithoutValue() {
|
||||
CommandRegistration r1 = CommandRegistration.builder()
|
||||
|
||||
Reference in New Issue
Block a user