Interpret command flag aliases in the parser

This commit is contained in:
2026-10-19 19:31:23 +00:00
parent 58b71c390f
commit 75fc45f072
8 changed files with 389 additions and 11 deletions

View File

@@ -80,7 +80,6 @@ func newExecution(app *App, ctx context.Context, streams Streams) *execution {
}
if app != nil {
current.snapshot = app.routeSnapshot()
current.session = internalcobra.Prepare(current.snapshot.metadata, current.options)
}
return current
}
@@ -89,9 +88,31 @@ func (current *execution) parse(args []string) commandmeta.Result {
if current.app == nil {
return commandmeta.Result{Failure: &commandmeta.Failure{Message: "cannot parse commands on a nil app"}}
}
if current.session == nil {
current.prepareSession()
}
return current.session.Parse(args)
}
func (current *execution) prepareSession() {
if current.app == nil {
return
}
if current.snapshot == nil {
current.snapshot = current.app.routeSnapshot()
}
options := current.options
if current.snapshot != nil {
switch current.route.kind {
case routeCanonical:
options.FlagAliases = current.snapshot.flagAliases[current.route.canonicalPath.Key()].neutralRecords()
case routeDiscovery:
options.FlagAliases = current.snapshot.discoveryFlags[current.route.discoveryPath.Key()].neutralRecords()
}
current.session = internalcobra.Prepare(current.snapshot.metadata, options)
}
}
func (current *execution) presentationSession() *internalcobra.Session {
if current.app == nil {
return nil
@@ -99,7 +120,8 @@ func (current *execution) presentationSession() *internalcobra.Session {
snapshot := current.app.routeSnapshot()
if current.session == nil || current.snapshot != snapshot {
current.snapshot = snapshot
current.session = internalcobra.Prepare(snapshot.metadata, current.options)
current.session = nil
current.prepareSession()
}
return current.session
}