Add support for flag parameters in descriptions

Fixes #55

Change one of standard command samples, to see effect on it
This commit is contained in:
Eric Bottard
2017-05-22 14:56:53 +02:00
parent 72f52c65d8
commit c7da298478
6 changed files with 78 additions and 62 deletions

View File

@@ -100,51 +100,17 @@ public class LegacyParameterResolver implements ParameterResolver {
.filter(key -> !key.isEmpty())
.map(key -> CLI_PREFIX + key)
.collect(Collectors.toList()));
Optional<String> defaultValue = defaultValueFor(option, result.keys());
if (defaultValue.isPresent()) {
result.defaultValue(defaultValue.get());
if (!option.mandatory()) {
result.defaultValue(CLI_OPTION_NULL.equals(option.unspecifiedDefaultValue()) ? "null" : option.unspecifiedDefaultValue());
}
if(!CLI_OPTION_NULL.equals(option.specifiedDefaultValue())) {
result.whenFlag(option.specifiedDefaultValue());
}
boolean containsEmptyKey = keys.contains("");
result.mandatoryKey(!containsEmptyKey);
return result;
}
private Optional<String> defaultValueFor(CliOption option, List<String> keys) {
// CliOption annotations have two default values, one for when the key is specified without a value,
// and one when the key isn't specified (e.g. "command --key" vs "command")
final boolean unspecifiedDefaultDeclared = !CLI_OPTION_NULL.equals(option.unspecifiedDefaultValue());
final boolean specifiedDefaultDeclared = !CLI_OPTION_NULL.equals(option.specifiedDefaultValue());
if (!unspecifiedDefaultDeclared && !specifiedDefaultDeclared) {
if (option.mandatory()) {
return Optional.empty();
} else {
// according to CliOption, is no default is declared, then null will be presented to non-primitive
// arguments
return Optional.of("null");
}
}
final StringBuilder defaultValue = new StringBuilder();
if (unspecifiedDefaultDeclared) {
defaultValue.append(option.unspecifiedDefaultValue());
}
if (specifiedDefaultDeclared) {
if (unspecifiedDefaultDeclared) {
defaultValue.append(", or ");
}
defaultValue.append(option.specifiedDefaultValue());
defaultValue.append(" if used as ");
defaultValue.append(keys.stream().collect(Collectors.joining(" or ")));
}
return Optional.of(defaultValue.toString());
}
@Override
public List<CompletionProposal> complete(MethodParameter parameter, CompletionContext context) {
return null;
@@ -175,7 +141,7 @@ public class LegacyParameterResolver implements ParameterResolver {
for (String key : option.key()) {
if (values.containsKey(key)) {
String value = values.get(key);
if (value == null && !"__NULL__".equals(option.specifiedDefaultValue())) {
if (value == null && !CLI_OPTION_NULL.equals(option.specifiedDefaultValue())) {
value = option.specifiedDefaultValue();
}
Class<?> parameterType = methodParameter.getParameterType();

View File

@@ -56,7 +56,10 @@ public class LegacyCommands implements CommandMarker {
}
@CliCommand(value = "sum", help = "adds two numbers")
public int sum(@CliOption(key = "v1", unspecifiedDefaultValue = "38") int a, @CliOption(key = "v2", specifiedDefaultValue = "42") int b) {
public int sum(
@CliOption(key = "v1", unspecifiedDefaultValue = "38") int a,
@CliOption(key = "v2", specifiedDefaultValue = "42") int b
) {
return a + b;
}
@@ -68,7 +71,7 @@ public class LegacyCommands implements CommandMarker {
@CliCommand(value = "someMethod", help = "Method used for testing purposes")
public String someMethod(
@CliOption(key = "key", mandatory = false, help = "The optional parameter") String parameter,
@CliOption(key = "option", help = "an option", specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", mandatory = true) boolean option) {
@CliOption(key = "option", help = "an option", specifiedDefaultValue = "true", unspecifiedDefaultValue = "false") boolean option) {
return parameter + ", " + option;
}

View File

@@ -179,7 +179,8 @@ public class LegacyParameterResolverTest {
assertThat(description.keys()).containsExactly("--option");
assertThat(description.formal()).isEqualTo(boolean.class.getName());
assertThat(description.defaultValue().get()).isEqualTo("false, or true if used as --option");
assertThat(description.defaultValue().get()).isEqualTo("false");
assertThat(description.defaultValueWhenFlag().get()).isEqualTo("true");
assertThat(description.mandatoryKey()).isTrue();
String expectedHelp = methodParameter.getParameterAnnotation(CliOption.class).help();
@@ -194,7 +195,8 @@ public class LegacyParameterResolverTest {
assertThat(description.keys()).containsExactly("--v2");
assertThat(description.formal()).isEqualTo(int.class.getName());
assertThat(description.defaultValue().get()).isEqualTo("42 if used as --v2");
assertThat(description.defaultValue().get()).isEqualTo("null");
assertThat(description.defaultValueWhenFlag().get()).isEqualTo("42");
assertThat(description.mandatoryKey()).isTrue();
String expectedHelp = methodParameter.getParameterAnnotation(CliOption.class).help();