MCPcopy
hub / github.com/spf13/cobra / GenYamlCustom

Function GenYamlCustom

doc/yaml_docs.go:93–147  ·  view source on GitHub ↗

GenYamlCustom creates custom yaml output.

(cmd *cobra.Command, w io.Writer, linkHandler func(string) string)

Source from the content-addressed store, hash-verified

91
92// GenYamlCustom creates custom yaml output.
93func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
94 cmd.InitDefaultHelpCmd()
95 cmd.InitDefaultHelpFlag()
96
97 yamlDoc := cmdDoc{}
98 yamlDoc.Name = cmd.CommandPath()
99
100 yamlDoc.Synopsis = forceMultiLine(cmd.Short)
101 yamlDoc.Description = forceMultiLine(cmd.Long)
102
103 if cmd.Runnable() {
104 yamlDoc.Usage = cmd.UseLine()
105 }
106
107 if len(cmd.Example) > 0 {
108 yamlDoc.Example = cmd.Example
109 }
110
111 flags := cmd.NonInheritedFlags()
112 if flags.HasFlags() {
113 yamlDoc.Options = genFlagResult(flags)
114 }
115 flags = cmd.InheritedFlags()
116 if flags.HasFlags() {
117 yamlDoc.InheritedOptions = genFlagResult(flags)
118 }
119
120 if hasSeeAlso(cmd) {
121 result := []string{}
122 if cmd.HasParent() {
123 parent := cmd.Parent()
124 result = append(result, parent.CommandPath()+" - "+parent.Short)
125 }
126 children := cmd.Commands()
127 sort.Sort(byName(children))
128 for _, child := range children {
129 if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
130 continue
131 }
132 result = append(result, child.CommandPath()+" - "+child.Short)
133 }
134 yamlDoc.SeeAlso = result
135 }
136
137 final, err := yaml.Marshal(&yamlDoc)
138 if err != nil {
139 fmt.Println(err)
140 os.Exit(1)
141 }
142
143 if _, err := w.Write(final); err != nil {
144 return err
145 }
146 return nil
147}
148
149func genFlagResult(flags *pflag.FlagSet) []cmdOption {
150 var result []cmdOption

Callers 2

GenYamlTreeCustomFunction · 0.85
GenYamlFunction · 0.85

Calls 15

forceMultiLineFunction · 0.85
genFlagResultFunction · 0.85
hasSeeAlsoFunction · 0.85
byNameTypeAlias · 0.85
InitDefaultHelpCmdMethod · 0.80
InitDefaultHelpFlagMethod · 0.80
CommandPathMethod · 0.80
RunnableMethod · 0.80
UseLineMethod · 0.80
NonInheritedFlagsMethod · 0.80
HasFlagsMethod · 0.80
InheritedFlagsMethod · 0.80

Tested by

no test coverage detected