Support option label

- This adds `label` to `CommandOption` which is then used
  in a Help instead of type.
- Fixes #424
This commit is contained in:
Janne Valkealahti
2022-06-08 07:39:44 +01:00
parent 8548828873
commit fa65a2308e
6 changed files with 113 additions and 9 deletions

View File

@@ -22,6 +22,7 @@ import java.util.stream.Stream;
import org.springframework.shell.command.CommandOption;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* Model encapsulating info about {@code command}.
@@ -51,7 +52,7 @@ class CommandInfoModel {
List<CommandOption> options = registration.getOptions();
List<CommandParameterInfoModel> parameters = options.stream()
.map(o -> {
String type = o.getType() == null ? "String" : ClassUtils.getShortName(o.getType().getRawClass());
String type = commandOptionType(o);
List<String> arguments = Stream.concat(
Stream.of(o.getLongNames()).map(a -> "--" + a),
Stream.of(o.getShortNames()).map(s -> "-" + s))
@@ -67,6 +68,15 @@ class CommandInfoModel {
return new CommandInfoModel(name, description, parameters);
}
private static String commandOptionType(CommandOption o) {
if (StringUtils.hasText(o.getLabel())) {
return o.getLabel();
}
else {
return o.getType() == null ? "String" : ClassUtils.getShortName(o.getType().getRawClass());
}
}
public String getName() {
return name;
}