feat: improve docs output

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
pull/612/head
Henrique Dias 2019-01-06 18:36:47 +00:00
parent a2d32438f1
commit ebaed3c390
1 changed files with 16 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import (
"sort"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/cobra"
)
@ -110,20 +111,29 @@ func generateMarkdown(cmd *cobra.Command, w io.Writer) {
checkErr(err)
}
func generateFlagsTable(fs *pflag.FlagSet, buf *bytes.Buffer) {
buf.WriteString("| Name | Shorthand | Usage |\n")
buf.WriteString("|------|-----------|-------|\n")
fs.VisitAll(func(f *pflag.Flag) {
buf.WriteString("|" + f.Name + "|" + f.Shorthand + "|" + f.Usage + "|\n")
})
}
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) {
flags := cmd.NonInheritedFlags()
flags.SetOutput(buf)
if flags.HasAvailableFlags() {
buf.WriteString("## Options\n\n```\n")
flags.PrintDefaults()
buf.WriteString("```\n\n")
buf.WriteString("## Options\n\n")
generateFlagsTable(flags, buf)
buf.WriteString("\n")
}
parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(buf)
if parentFlags.HasAvailableFlags() {
buf.WriteString("### Inherited\n\n```\n")
parentFlags.PrintDefaults()
buf.WriteString("```\n")
buf.WriteString("### Inherited\n\n")
generateFlagsTable(parentFlags, buf)
buf.WriteString("\n")
}
}