Compose discovery help through group aliases

This commit is contained in:
2026-10-20 00:25:33 +00:00
parent fa09edf8cc
commit c231a41665
4 changed files with 123 additions and 19 deletions

View File

@@ -17,7 +17,7 @@ func (a *App) helpView(route routeResult, snapshot *routingSnapshot) commandmeta
return view
}
if view.Discovery {
view.CommandAliases = discoveryHelpAliases(snapshot.commandAliases, route.discoveryPath)
view.CommandAliases = discoveryHelpAliases(snapshot, route)
view.FlagAliases = snapshot.discoveryFlags[route.discoveryPath.Key()].neutralRecords()
return view
}
@@ -30,17 +30,31 @@ func clonePath(path commandpath.Path) []string {
return append([]string(nil), path...)
}
func discoveryHelpAliases(aliases []commandmeta.CommandAlias, prefix commandpath.Path) []commandmeta.CommandAlias {
var result []commandmeta.CommandAlias
for _, alias := range aliases {
source := commandpath.FromTokens(alias.Source)
if len(source) <= len(prefix) || !source.HasPrefix(prefix) {
continue
func discoveryHelpAliases(snapshot *routingSnapshot, route routeResult) []commandmeta.CommandAlias {
bySource := make(map[string]commandmeta.CommandAlias)
for _, prefix := range []commandpath.Path{route.enteredPath, route.discoveryPath} {
for _, alias := range snapshot.commandAliases {
source := commandpath.FromTokens(alias.Source)
if len(source) <= len(prefix) || !source.HasPrefix(prefix) {
continue
}
suffix := source[len(prefix):]
entered := append(route.enteredPath.Clone(), suffix...)
target := snapshot.aliases.Resolve(entered)
if traceCanonicalPath(snapshot.canonical, target).exact == nil {
continue
}
bySource[suffix.Key()] = commandmeta.CommandAlias{Source: clonePath(suffix), Target: clonePath(target)}
}
result = append(result, commandmeta.CommandAlias{
Source: clonePath(source[len(prefix):]),
Target: append([]string(nil), alias.Target...),
})
}
keys := make([]string, 0, len(bySource))
for key := range bySource {
keys = append(keys, key)
}
sort.Strings(keys)
result := make([]commandmeta.CommandAlias, 0, len(keys))
for _, key := range keys {
result = append(result, bySource[key])
}
return result
}