Add invocation-owned Cobra sessions

This commit is contained in:
2026-10-09 01:35:44 +00:00
parent 3ad355927c
commit a852301801
4 changed files with 179 additions and 58 deletions

View File

@@ -38,6 +38,30 @@ func BenchmarkPerformanceParse(b *testing.B) {
}
}
func BenchmarkPerformanceSessionParseAndHelp(b *testing.B) {
root := performanceFlatTree(8)
args := []string{"wide007"}
session := Prepare(root, ParseOptions{Context: context.Background(), Out: io.Discard, Err: io.Discard})
result := session.Parse(args)
if result.Failure != nil {
b.Fatalf("fixture parse failed: %s", result.Failure.Message)
}
if help, failure := session.RenderHelp(result.Path); failure != nil || help == "" {
b.Fatalf("fixture help failed: %q %v", help, failure)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := session.Parse(args)
if result.Failure != nil {
b.Fatalf("parse failed: %s", result.Failure.Message)
}
if help, failure := session.RenderHelp(result.Path); failure != nil || help == "" {
b.Fatalf("help failed: %q %v", help, failure)
}
}
}
func performanceFlatTree(count int) *Command {
root := &Command{Name: "app", Children: make([]*Command, count)}
for index := range root.Children {