Partition flag occurrences once per invocation

This commit is contained in:
2026-11-22 22:23:07 +00:00
parent 98e3958dfb
commit 3415d9337d
3 changed files with 67 additions and 6 deletions

View File

@@ -48,7 +48,7 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
payload != nil && payload.hasRun() && !routedSnapshot.builtins[parsed.Key]
if parsed.Failure != nil {
outcome = Usage(parsed.Failure.Message)
} else if err := current.prepareFeatures(parsed, featureRequired); err != nil {
} else if err := current.prepareFeatures(&parsed, featureRequired); err != nil {
outcome = classifyInputError(err)
} else if current.route.kind == routeDiscovery {
if parsed.HelpRequested {
@@ -83,17 +83,34 @@ func (a *App) Execute(ctx context.Context, args []string, streams Streams) Outco
return current.finalize(outcome, path, handlerRan)
}
func (current *execution) prepareFeatures(parsed commandmeta.Result, required bool) error {
func (current *execution) prepareFeatures(parsed *commandmeta.Result, required bool) error {
if current.snapshot == nil {
return nil
}
// Partition the canonical occurrence stream once. Positions within each owner
// remain dense and preserve alias/interleaved binding order.
owners := make(map[string]int, len(current.snapshot.featureFlags))
counts := make([]int, len(current.snapshot.features))
for i, feature := range current.snapshot.features {
flags := feature.payload.bridgeFlags()
counts[i] = len(flags)
for _, flag := range flags {
owners[flag.Name] = i + 1
}
}
occurrences := make([][]commandmeta.FlagOccurrence, len(counts)+1)
for _, occurrence := range parsed.FlagOccurrences {
owner := owners[occurrence.Name]
occurrences[owner] = append(occurrences[owner], occurrence)
}
parsed.FlagOccurrences = occurrences[0]
offset := 0
for _, feature := range current.snapshot.features {
count := len(feature.payload.bridgeFlags())
for i, feature := range current.snapshot.features {
count := counts[i]
if offset+count > len(parsed.InvocationFlagValues) || offset+count > len(parsed.InvocationFlagSpellings) {
return fmt.Errorf("missing invocation feature flag values")
}
start, err := feature.payload.prepare(parsed.InvocationFlagValues[offset:offset+count], parsed.InvocationFlagSpellings[offset:offset+count], parsed.FlagOccurrences, false)
start, err := feature.payload.prepare(parsed.InvocationFlagValues[offset:offset+count], parsed.InvocationFlagSpellings[offset:offset+count], occurrences[i+1], false)
if err != nil {
return err
}