Build returns a cmdhelp Document without serializing it. Callers that need to inspect or further mutate the document (tests, dynamic enum resolvers in TASK-936) should use this and serialize themselves.
(target, root *cobra.Command, opts Options)
| 81 | // need to inspect or further mutate the document (tests, dynamic enum |
| 82 | // resolvers in TASK-936) should use this and serialize themselves. |
| 83 | func Build(target, root *cobra.Command, opts Options) *Document { |
| 84 | binary := opts.Binary |
| 85 | if binary == "" { |
| 86 | binary = root.Name() |
| 87 | } |
| 88 | |
| 89 | doc := &Document{ |
| 90 | CmdhelpVersion: Version, |
| 91 | Binary: binary, |
| 92 | Version: opts.Version, |
| 93 | Summary: firstLine(root.Short), |
| 94 | Homepage: opts.Homepage, |
| 95 | Commands: map[string]Command{}, |
| 96 | } |
| 97 | |
| 98 | if globals := collectFlags(root.PersistentFlags()); len(globals) > 0 { |
| 99 | doc.GlobalFlags = globals |
| 100 | } |
| 101 | |
| 102 | walk(target, root, doc, 0, opts.MaxDepth) |
| 103 | |
| 104 | // Splice dynamic workspace facts after the static walk so callers |
| 105 | // can inspect Build's output as either pre- or post-resolution. |
| 106 | opts.Resolver.Apply(doc) |
| 107 | |
| 108 | return doc |
| 109 | } |
| 110 | |
| 111 | func walk(cur, root *cobra.Command, doc *Document, depth, maxDepth int) { |
| 112 | if cur == nil || cur.Hidden { |