CommandRegistration should return same options
- Cache created CommandOption(s) to return same instance. - Fixes #648
This commit is contained in:
@@ -1161,6 +1161,7 @@ public interface CommandRegistration {
|
||||
private boolean hidden;
|
||||
private String description;
|
||||
private Supplier<Availability> availability;
|
||||
private List<CommandOption> options;
|
||||
private List<DefaultOptionSpec> optionSpecs;
|
||||
private DefaultTargetSpec targetSpec;
|
||||
private List<DefaultAliasSpec> aliasSpecs;
|
||||
@@ -1218,7 +1219,10 @@ public interface CommandRegistration {
|
||||
|
||||
@Override
|
||||
public List<CommandOption> getOptions() {
|
||||
List<CommandOption> options = optionSpecs.stream()
|
||||
if (options != null) {
|
||||
return options;
|
||||
}
|
||||
options = optionSpecs.stream()
|
||||
.map(o -> {
|
||||
String[] longNames = o.getLongNames();
|
||||
Function<String, String> modifier = o.getOptionNameModifier();
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.shell.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -600,4 +601,21 @@ public class CommandRegistrationTests extends AbstractCommandTests {
|
||||
assertThat(option.getLongNames()).isEqualTo(new String[] { "xarg1" });
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void optionShouldBeSameInstance() {
|
||||
CommandRegistration registration = CommandRegistration.builder()
|
||||
.defaultOptionNameModifier(name -> "x" + name)
|
||||
.command("command1")
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.and()
|
||||
.withTarget()
|
||||
.consumer(ctx -> {})
|
||||
.and()
|
||||
.build();
|
||||
List<CommandOption> options1 = registration.getOptions();
|
||||
List<CommandOption> options2 = registration.getOptions();
|
||||
assertThat(options1).isEqualTo(options2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user