buildCmdExampleCatalog walks the live cobra command tree and records every command path (minus the "lark-cli" root prefix) with its accepted flags and whether it is a parent group. This is the same Build() the binary uses, so the catalog can never drift from the real commands.
()
| 87 | // whether it is a parent group. This is the same Build() the binary uses, so |
| 88 | // the catalog can never drift from the real commands. |
| 89 | func buildCmdExampleCatalog() *catalog { |
| 90 | root := cmd.Build(context.Background(), cmdutil.InvocationContext{}) |
| 91 | cat := newCatalog() |
| 92 | var walk func(c *cobra.Command) |
| 93 | walk = func(c *cobra.Command) { |
| 94 | path := strings.TrimSpace(strings.TrimPrefix(c.CommandPath(), "lark-cli")) |
| 95 | var flags []string |
| 96 | add := func(fl *pflag.Flag) { |
| 97 | flags = append(flags, "--"+fl.Name) |
| 98 | if fl.Shorthand != "" { |
| 99 | flags = append(flags, "-"+fl.Shorthand) |
| 100 | } |
| 101 | } |
| 102 | c.Flags().VisitAll(add) |
| 103 | c.InheritedFlags().VisitAll(add) |
| 104 | c.PersistentFlags().VisitAll(add) // root's own persistent flags (e.g. --profile) |
| 105 | cat.addCommand(path, flags) |
| 106 | cat.setGroup(path, c.HasSubCommands()) |
| 107 | for _, sub := range c.Commands() { |
| 108 | walk(sub) |
| 109 | } |
| 110 | } |
| 111 | walk(root) |
| 112 | return cat |
| 113 | } |
no test coverage detected