YAML documentation contains "Usage" (#1037)

This commit is contained in:
Alessandro (Ale) Segala 2020-06-15 15:51:03 -07:00 committed by GitHub
parent 1d3ac910d4
commit ed7b60e298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -37,6 +37,7 @@ type cmdDoc struct {
Name string Name string
Synopsis string `yaml:",omitempty"` Synopsis string `yaml:",omitempty"`
Description string `yaml:",omitempty"` Description string `yaml:",omitempty"`
Usage string `yaml:",omitempty"`
Options []cmdOption `yaml:",omitempty"` Options []cmdOption `yaml:",omitempty"`
InheritedOptions []cmdOption `yaml:"inherited_options,omitempty"` InheritedOptions []cmdOption `yaml:"inherited_options,omitempty"`
Example string `yaml:",omitempty"` Example string `yaml:",omitempty"`
@ -98,6 +99,10 @@ func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) str
yamlDoc.Synopsis = forceMultiLine(cmd.Short) yamlDoc.Synopsis = forceMultiLine(cmd.Short)
yamlDoc.Description = forceMultiLine(cmd.Long) yamlDoc.Description = forceMultiLine(cmd.Long)
if cmd.Runnable() {
yamlDoc.Usage = cmd.UseLine()
}
if len(cmd.Example) > 0 { if len(cmd.Example) > 0 {
yamlDoc.Example = cmd.Example yamlDoc.Example = cmd.Example
} }

View File

@ -57,6 +57,17 @@ func TestGenYamlTree(t *testing.T) {
} }
} }
func TestGenYamlDocRunnable(t *testing.T) {
// Testing a runnable command: should contain the "usage" field
buf := new(bytes.Buffer)
if err := GenYaml(rootCmd, buf); err != nil {
t.Fatal(err)
}
output := buf.String()
checkStringContains(t, output, "usage: "+rootCmd.Use)
}
func BenchmarkGenYamlToFile(b *testing.B) { func BenchmarkGenYamlToFile(b *testing.B) {
file, err := ioutil.TempFile("", "") file, err := ioutil.TempFile("", "")
if err != nil { if err != nil {