Skip to content

Commit ed7b60e

Browse files
authored
YAML documentation contains "Usage" (#1037)
1 parent 1d3ac91 commit ed7b60e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

doc/yaml_docs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type cmdDoc struct {
3737
Name string
3838
Synopsis string `yaml:",omitempty"`
3939
Description string `yaml:",omitempty"`
40+
Usage string `yaml:",omitempty"`
4041
Options []cmdOption `yaml:",omitempty"`
4142
InheritedOptions []cmdOption `yaml:"inherited_options,omitempty"`
4243
Example string `yaml:",omitempty"`
@@ -98,6 +99,10 @@ func GenYamlCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) str
9899
yamlDoc.Synopsis = forceMultiLine(cmd.Short)
99100
yamlDoc.Description = forceMultiLine(cmd.Long)
100101

102+
if cmd.Runnable() {
103+
yamlDoc.Usage = cmd.UseLine()
104+
}
105+
101106
if len(cmd.Example) > 0 {
102107
yamlDoc.Example = cmd.Example
103108
}

doc/yaml_docs_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ func TestGenYamlTree(t *testing.T) {
5757
}
5858
}
5959

60+
func TestGenYamlDocRunnable(t *testing.T) {
61+
// Testing a runnable command: should contain the "usage" field
62+
buf := new(bytes.Buffer)
63+
if err := GenYaml(rootCmd, buf); err != nil {
64+
t.Fatal(err)
65+
}
66+
output := buf.String()
67+
68+
checkStringContains(t, output, "usage: "+rootCmd.Use)
69+
}
70+
6071
func BenchmarkGenYamlToFile(b *testing.B) {
6172
file, err := ioutil.TempFile("", "")
6273
if err != nil {

0 commit comments

Comments
 (0)