Fix alias usage in help command

- Add info about aliases into model structure used by help
  command templating.
- Add some tests to models.
- Change help commands list to group command and its aliases together.
- Change help command to show aliases.
- Fixes #458
- Fixes #426
This commit is contained in:
Janne Valkealahti
2022-07-14 16:31:14 +01:00
parent d88849dfa9
commit 06d9fd51b2
10 changed files with 265 additions and 13 deletions

View File

@@ -33,13 +33,15 @@ import org.springframework.util.StringUtils;
class CommandInfoModel {
private String name;
private List<String> aliases;
private String description;
private List<CommandParameterInfoModel> parameters;
private CommandAvailabilityInfoModel availability;
CommandInfoModel(String name, String description, List<CommandParameterInfoModel> parameters,
CommandInfoModel(String name, List<String> aliases, String description, List<CommandParameterInfoModel> parameters,
CommandAvailabilityInfoModel availability) {
this.name = name;
this.aliases = aliases;
this.description = description;
this.parameters = parameters;
this.availability = availability;
@@ -68,6 +70,9 @@ class CommandInfoModel {
})
.collect(Collectors.toList());
List<String> aliases = registration.getAliases().stream().map(ca -> ca.getCommand())
.collect(Collectors.toList());
String description = registration.getDescription();
boolean available = true;
String availReason = "";
@@ -77,7 +82,7 @@ class CommandInfoModel {
availReason = a.getReason();
}
CommandAvailabilityInfoModel availModel = CommandAvailabilityInfoModel.of(available, availReason);
return new CommandInfoModel(name, description, parameters, availModel);
return new CommandInfoModel(name, aliases, description, parameters, availModel);
}
private static String commandOptionType(CommandOption o) {
@@ -93,6 +98,10 @@ class CommandInfoModel {
return name;
}
public List<String> getAliases() {
return this.aliases;
}
public String getDescription() {
return description;
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.shell.standard.commands;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -54,14 +55,18 @@ class GroupsInfoModel {
* @return a groups info model
*/
static GroupsInfoModel of(boolean showGroups, Map<String, CommandRegistration> registrations) {
// throw away registrations aliases as those are then handled in a model
// collect commands into groups with sorting
SortedMap<String, Map<String, CommandRegistration>> commandsByGroupAndName = registrations.entrySet().stream()
HashSet<CommandRegistration> regsWithoutAliases = new HashSet<>(registrations.values());
SortedMap<String, Map<String, CommandRegistration>> commandsByGroupAndName = regsWithoutAliases.stream()
.collect(Collectors.toMap(r -> r.getCommand(), r -> r)).entrySet().stream()
.collect(Collectors.groupingBy(
e -> StringUtils.hasText(e.getValue().getGroup()) ? e.getValue().getGroup() : "Default",
TreeMap::new,
Collectors.toMap(Entry::getKey, Entry::getValue)
));
// build model
List<GroupCommandInfoModel> gcims = commandsByGroupAndName.entrySet().stream()
.map(e -> {

View File

@@ -66,6 +66,14 @@ availability(availability) ::= <<
<endif>
>>
// ALIASES
aliases(aliases) ::= <<
<if(aliases)>
<("ALSO KNOWN AS"); format="style-highlight">
<(aliases); separator=", ">
<endif>
>>
// main
main(model) ::= <<
<name(model.name, model.description)>
@@ -74,4 +82,5 @@ main(model) ::= <<
<options(model.parameters)>
<availability(model.availability)>
<aliases(model.aliases)>
>>

View File

@@ -16,8 +16,12 @@ availabilityDesc(hasUnavailableCommands) ::= <<
<endif>
>>
commandName(command) ::= <%
<[command.name, command.aliases]; format="style-highlight", separator=", ">
%>
command(command) ::= <<
<availability(command.availability)><(command.name); format="style-highlight"><(":"); format="style-highlight"> <command.description>
<availability(command.availability)><commandName(command); format="style-highlight"><(":"); format="style-highlight"> <command.description>
>>