| 48 | } |
| 49 | |
| 50 | func printJSONHelp(cmd *cobra.Command) { |
| 51 | h := jsonHelp{ |
| 52 | Name: cmd.Name(), |
| 53 | Description: cmd.Short, |
| 54 | Usage: cmd.UseLine(), |
| 55 | } |
| 56 | |
| 57 | if v := cmd.Root().Version; v != "" && cmd == cmd.Root() { |
| 58 | h.Version = v |
| 59 | } |
| 60 | |
| 61 | for _, sub := range cmd.Commands() { |
| 62 | if sub.IsAvailableCommand() && sub.Name() != "help" { |
| 63 | h.Commands = append(h.Commands, jsonCommand{ |
| 64 | Name: sub.Name(), |
| 65 | Description: sub.Short, |
| 66 | Usage: sub.UseLine(), |
| 67 | }) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | cmd.Flags().VisitAll(func(f *pflag.Flag) { |
| 72 | if f.Hidden { |
| 73 | return |
| 74 | } |
| 75 | h.Flags = append(h.Flags, jsonFlag{ |
| 76 | Name: "--" + f.Name, |
| 77 | Shorthand: f.Shorthand, |
| 78 | Description: f.Usage, |
| 79 | Type: f.Value.Type(), |
| 80 | Default: f.DefValue, |
| 81 | }) |
| 82 | }) |
| 83 | |
| 84 | // Include inherited persistent flags (e.g. --json, --yes). |
| 85 | cmd.InheritedFlags().VisitAll(func(f *pflag.Flag) { |
| 86 | if f.Hidden { |
| 87 | return |
| 88 | } |
| 89 | h.Flags = append(h.Flags, jsonFlag{ |
| 90 | Name: "--" + f.Name, |
| 91 | Shorthand: f.Shorthand, |
| 92 | Description: f.Usage, |
| 93 | Type: f.Value.Type(), |
| 94 | Default: f.DefValue, |
| 95 | }) |
| 96 | }) |
| 97 | |
| 98 | printJSON(h) |
| 99 | } |
| 100 | |
| 101 | // printDefaultHelp renders Cobra's built-in plain-text help by temporarily |
| 102 | // clearing the custom help function on the command and all its ancestors. |