Render help through command aliases

This commit is contained in:
2026-10-19 19:40:55 +00:00
parent 451a0c483f
commit a504efbad7
10 changed files with 391 additions and 11 deletions

View File

@@ -28,6 +28,13 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
if parsed.Failure != nil {
return current.present(Usage(errors.New(parsed.Failure.Message)), parsed.Path)
}
if current.route.kind == routeDiscovery {
if parsed.HelpRequested {
return current.present(current.renderHelp(parsed.Path), clonePath(current.route.enteredPath))
}
entered := current.route.enteredPath.String()
return current.present(Usage(fmt.Errorf("incomplete command path %q; use %q --help", entered, entered)), clonePath(current.route.enteredPath))
}
payload, ok := routedSnapshot.runners[parsed.Key]
if !ok || payload == nil {
return current.present(current.renderHelp(parsed.Path), parsed.Path)
@@ -139,14 +146,34 @@ func (current *execution) renderHelp(path []string) Outcome {
if current.app == nil {
return Failure(errors.New("cannot render help on a nil app"))
}
return current.app.renderHelpOutcomeWithSession(path, current.streams, current.presentationSession())
session := current.presentationSession()
view := current.app.helpView(current.route, current.snapshot)
return current.app.renderHelpViewOutcomeWithSession(view, current.streams, session)
}
func (current *execution) renderHelpTargets(targets []string) Outcome {
if current.app == nil {
return Failure(errors.New("cannot render help on a nil app"))
}
return current.app.renderHelpTargetsWithSession(targets, current.streams, current.presentationSession())
snapshot := current.app.routeSnapshot()
if snapshot == nil {
return Failure(errors.New("cannot render help without command metadata"))
}
route := resolveEnteredRoute(targets, snapshot)
if route.kind == routeUnknown {
session := internalcobra.Prepare(snapshot.metadata, current.options)
return current.app.renderHelpOutcomeWithSession(targets, current.streams, session)
}
options := current.options
switch route.kind {
case routeCanonical:
options.FlagAliases = snapshot.flagAliases[route.canonicalPath.Key()].neutralRecords()
case routeDiscovery:
options.FlagAliases = snapshot.discoveryFlags[route.discoveryPath.Key()].neutralRecords()
}
session := internalcobra.Prepare(snapshot.metadata, options)
view := current.app.helpView(route, snapshot)
return current.app.renderHelpViewOutcomeWithSession(view, current.streams, session)
}
func (current *execution) usageLine(path []string) string {