From ed7b60e2987d2cd54d6a391379e33effdacdadf9 Mon Sep 17 00:00:00 2001 From: "Alessandro (Ale) Segala" <43508+ItalyPaleAle@users.noreply.github.com> Date: Mon, 15 Jun 2020 15:51:03 -0700 Subject: [PATCH] YAML documentation contains "Usage" (#1037) --- doc/yaml_docs.go | 5 +++++ doc/yaml_docs_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/doc/yaml_docs.go b/doc/yaml_docs.go index ea00af07..96e6ad72 100644 --- a/doc/yaml_docs.go +++ b/doc/yaml_docs.go @@ -37,6 +37,7 @@ type cmdDoc struct { Name string Synopsis string `yaml:",omitempty"` Description string `yaml:",omitempty"` + Usage string `yaml:",omitempty"` Options []cmdOption `yaml:",omitempty"` InheritedOptions []cmdOption `yaml:"inherited_options,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.Description = forceMultiLine(cmd.Long) + if cmd.Runnable() { + yamlDoc.Usage = cmd.UseLine() + } + if len(cmd.Example) > 0 { yamlDoc.Example = cmd.Example } diff --git a/doc/yaml_docs_test.go b/doc/yaml_docs_test.go index c5a63594..d08fa4f8 100644 --- a/doc/yaml_docs_test.go +++ b/doc/yaml_docs_test.go @@ -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) { file, err := ioutil.TempFile("", "") if err != nil {