Skip to content

Fix use of default values #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.List;
Expand Down Expand Up @@ -313,6 +314,21 @@ ParserResults visit(List<List<String>> lexerResults, List<CommandOption> options
}
})
.collect(Collectors.toList());

// Check options which didn't get matched and add parser result
// for those having default value.
List<CommandOption> defaultValueOptionsToCheck = new ArrayList<>(options);
results.stream()
.forEach(pr -> {
if (pr.option != null) {
defaultValueOptionsToCheck.remove(pr.option);
}
});
defaultValueOptionsToCheck.stream()
.filter(co -> co.getDefaultValue() != null)
.forEach(co -> {
results.add(ParserResult.of(co, Collections.emptyList(), co.getDefaultValue(), null));
});
return ParserResults.of(results);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,25 @@ public void testDefaultValueAsNull() {
assertThat(pojo1.method4Arg1).isNull();
}

@Test
public void testDefaultValue() {
CommandRegistration r1 = CommandRegistration.builder()
.command("command1")
.withOption()
.longNames("arg1")
.defaultValue("defaultValue1")
// .position(0)
// .arity(OptionArity.EXACTLY_ONE)
.and()
.withTarget()
.method(pojo1, "method4")
.and()
.build();
execution.evaluate(r1, new String[]{});
assertThat(pojo1.method4Count).isEqualTo(1);
assertThat(pojo1.method4Arg1).isEqualTo("defaultValue1");
}

@Test
public void testRequiredArg() {
CommandRegistration r1 = CommandRegistration.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ else if (o.length() == stripped.length() + 1 && stripped.length() == 1) {
if (so.arity() > -1) {
optionSpec.arity(0, so.arity());
}
if (!ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NONE)
&& !ObjectUtils.nullSafeEquals(so.defaultValue(), ShellOption.NULL)) {
optionSpec.defaultValue(so.defaultValue());
}
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,40 @@ public void foo2() {
public void foo3() {
}
}

@Test
public void testOptionUseDefaultValue() {
shellContext.setInteractionMode(InteractionMode.NONINTERACTIVE);
applicationContext = new AnnotationConfigApplicationContext(DefaultValuesCommands.class);
registrar.setApplicationContext(applicationContext);
registrar.register(catalog);

assertThat(catalog.getRegistrations().get("foo1")).isNotNull();
assertThat(catalog.getRegistrations().get("foo1").getOptions()).hasSize(1);
assertThat(catalog.getRegistrations().get("foo1").getOptions().get(0).getDefaultValue()).isEqualTo("arg1Value");

assertThat(catalog.getRegistrations().get("foo2")).isNotNull();
assertThat(catalog.getRegistrations().get("foo2").getOptions()).hasSize(1);
assertThat(catalog.getRegistrations().get("foo2").getOptions().get(0).getDefaultValue()).isNull();

assertThat(catalog.getRegistrations().get("foo3")).isNotNull();
assertThat(catalog.getRegistrations().get("foo3").getOptions()).hasSize(1);
assertThat(catalog.getRegistrations().get("foo3").getOptions().get(0).getDefaultValue()).isNull();
}

@ShellComponent
public static class DefaultValuesCommands {

@ShellMethod(value = "foo1")
public void foo1(@ShellOption(defaultValue = "arg1Value") String arg1) {
}

@ShellMethod(value = "foo2")
public void foo2(@ShellOption(defaultValue = ShellOption.NONE) String arg1) {
}

@ShellMethod(value = "foo")
public void foo3(@ShellOption(defaultValue = ShellOption.NULL) String arg1) {
}
}
}